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

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

Issue 29706560: Issue 5179 - Implement asynchronous executor with a controllable lifetime (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Feb. 23, 2018, 10:29 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
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 <list>
19 #include <thread>
20 #include <functional>
21 #include "SynchronizedCollection.h"
22
23 namespace AdblockPlus
24 {
25 /**
26 * The implementation of active object pattern, simply put it sequentially
27 * executes posted callable objects in a single background thread.
28 * In the destructor it waits for the finishing of all already posted calls.
29 */
30 class ActiveObject {
31 public:
32 /**
33 * A callable object type, in fact wrapping std::function.
34 */
35 typedef std::function<void()> Call;
36
37 /**
38 * Constructor, the background thread is started after finishing this call.
39 */
40 ActiveObject();
41
42 /**
43 * Destructor, it waits for finishing of all already posted calls.
44 */
45 ~ActiveObject();
46 /**
47 * Adds the `call`, which should be executed in the worker thread to the
48 * end of the internally held list.
49 * @param call object.
50 */
51 void Post(const Call& call);
52 void Post(Call&& call);
53 private:
54 void ThreadFunc();
55 private:
56 bool isRunning;
57 SynchronizedCollection<std::list<Call>> calls;
58 std::thread thread;
59 };
60 }
OLDNEW

Powered by Google App Engine
This is Rietveld