Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: include/AdblockPlus/SynchronizedCollection.h

Issue 29706560: Issue 5179 - Implement asynchronous executor with a controllable lifetime (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: address comments Created March 1, 2018, 11:14 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/AdblockPlus/Platform.h ('k') | libadblockplus.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #pragma once
18 #include <mutex>
19 #include <condition_variable>
20
21 namespace AdblockPlus
22 {
23 /**
24 * Wrapper around `Container` providing few generic methods which ensure
25 * that the underlying container is accessed only by one thread at the same
26 * time.
27 */
28 template<typename TContainer>
29 class SynchronizedCollection
30 {
31 protected:
32 typedef TContainer Container;
33 public:
34 /**
35 * The `value_type` represents the type of stored values.
36 */
37 typedef typename Container::value_type value_type;
38
39 /**
40 * Adds `value` normally to the end.
41 * @param value which is stored.
42 */
43 void push_back(const value_type& value)
44 {
45 {
46 std::lock_guard<std::mutex> lock(mutex);
47 collection.push_back(value);
48 }
49 conditionVar.notify_one();
50 }
51 void push_back(value_type&& value)
52 {
53 {
54 std::lock_guard<std::mutex> lock(mutex);
55 collection.push_back(std::move(value));
56 }
57 conditionVar.notify_one();
58 }
59
60 /**
61 * Extracts the first stored element and returns it.
62 * Pay attention that the call of this method blocks the execution until
63 * there is at least one element added to the collection.
64 */
65 value_type pop_front()
66 {
67 std::unique_lock<std::mutex> lock(mutex);
68 conditionVar.wait(lock, [this]()->bool
69 {
70 return !collection.empty();
71 });
72 value_type retValue = collection.front();
73 collection.pop_front();
74 return retValue;
75 }
76 protected:
77 Container collection;
78 std::mutex mutex;
79 std::condition_variable conditionVar;
80 };
81 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/Platform.h ('k') | libadblockplus.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld