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

Side by Side Diff: src/AsyncExecutor.cpp

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
18 #include <AdblockPlus/AsyncExecutor.h>
19
20 using namespace AdblockPlus;
21
22 void AsyncExecutor::SyncThreads::SpawnThread(std::function<void(iterator)>&& tas k)
23 {
24 std::lock_guard<std::mutex> lock(mutex);
25 auto threadItertor = collection.emplace(collection.end());
26 *threadItertor = /* A */ std::thread(std::move(task), threadItertor);
27 // no need to notify here with the current usage of that class.
28 }
29
30 std::thread AsyncExecutor::SyncThreads::TakeOut(iterator pos)
31 {
32 std::thread retValue;
33 {
34 std::lock_guard<std::mutex> lock(mutex);
35 retValue = /* B */ std::move(*pos);
36 collection.erase(pos);
37 }
38 conditionVar.notify_one();
39 return retValue;
40 }
41
42 void AsyncExecutor::SyncThreads::WaitUtilEmpty()
43 {
44 std::unique_lock<std::mutex> lock(mutex);
45 conditionVar.wait(lock, [this]()->bool
46 {
47 return collection.empty();
48 });
49 }
50
51 AsyncExecutor::~AsyncExecutor()
52 {
53 threads.WaitUtilEmpty();
54 }
55
56 void AsyncExecutor::Dispatch(const std::function<void()>& call)
57 {
58 if (!call)
59 return;
60 threads.SpawnThread([this, call](SyncThreads::iterator threadIterator)
61 {
62 call();
63 threadCollector.Post([this, threadIterator]
64 {
65 threads.TakeOut(threadIterator).join();
66 });
67 });
68 }
OLDNEW

Powered by Google App Engine
This is Rietveld