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

Side by Side Diff: include/AdblockPlus/AsyncExecutor.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/ActiveObject.h ('k') | include/AdblockPlus/Platform.h » ('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 "ActiveObject.h"
19
20 namespace AdblockPlus
21 {
22 /**
23 * Spawns a new thread for each task and waits for finishing of all spawned
24 * threads in the destructor.
25 */
26 class AsyncExecutor
27 {
28 // This class not only serializes access to the list of threads but also
29 // ensures that internals of std::thread are valid in another (collecting)
30 // thread. The latter is about sentries protecting A and B because
31 // otherwise it can happen that a worker thread has already finished the
32 // call, passed info to the collector thread and the collector is trying
33 // to get information from iterator (B) but the assignment (A) has not
34 // happened yet.
35 class SyncThreads
36 {
37 typedef std::list<std::thread> Threads;
38 public:
39 typedef Threads::iterator iterator;
40 void SpawnThread(std::function<void(iterator)>&& task);
41 std::thread TakeOut(iterator pos);
42 void WaitUtilEmpty();
43 protected:
44 Threads collection;
45 std::mutex mutex;
46 std::condition_variable conditionVar;
47 };
48 public:
49 /**
50 * Destructor, it waits for finishing of all already dispatched tasks.
51 */
52 ~AsyncExecutor();
53
54 /**
55 * Creates a new thread in which the `call` will be executed.
56 * @param call is a function object which is called within a worker thread,
57 * different from the caller thread. There is no effect if `call` is
58 * empty.
59 */
60 void Dispatch(const std::function<void()>& call);
61 private:
62 SyncThreads threads;
63 ActiveObject threadCollector;
64 };
65 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/ActiveObject.h ('k') | include/AdblockPlus/Platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld