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

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

Issue 29727558: Issue 6489 - fix initialization moment of the default scheduler of DefaultPlatformBuilder (Closed) Base URL: https://github.com/adblockplus/libadblockplus@cd1cd70ea3122fb4b8a96c40d96ce9b1ae5a3ae9
Patch Set: Created March 19, 2018, 10:17 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 | « no previous file | include/AdblockPlus/Platform.h » ('j') | include/AdblockPlus/Platform.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 * Creates a new thread in which the `call` will be executed. 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, 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 57 * different from the caller thread. There is no effect if `call` is
58 * empty. 58 * empty.
59 */ 59 */
60 void Dispatch(const std::function<void()>& call); 60 void Dispatch(const std::function<void()>& call);
61 private: 61 private:
62 SyncThreads threads; 62 SyncThreads threads;
63 ActiveObject threadCollector; 63 ActiveObject threadCollector;
64 }; 64 };
65
66 /**
67 * This class provides the same interface as `AsyncExecutor` but allows in
68 * a thread-safe manner invalidate internally held implementation of
69 * `AsyncExecutor`. Any subsequent calls of `Dispatch` have no effect,
70 * what allows to safely share `AsyncExecutor` but control it's operability.
71 */
72 class OptionalAsyncExecutor
73 {
74 public:
75 /**
76 * Contructor.
77 *
78 * Initially constructed the class behaves as `AsyncExecutor`.
79 */
80 OptionalAsyncExecutor()
81 : executor(new AsyncExecutor())
82 {
83 }
84
85 /**
86 * Creates a new thread in which the `call` will be executed.
87 * @param call is a function object which is called within a worker thread,
88 * different from the caller thread. There is no effect if `call` is
89 * empty or if `Invalidate` had been already called.
90 */
91 void Dispatch(const std::function<void()>& call)
92 {
93 std::lock_guard<std::mutex> lock(asyncExecutorMutex);
94 if (!executor)
95 return;
96 executor->Dispatch(call);
97 }
98
99 /**
100 * Destroyes internally held `AsyncExecutor`, any subsequent calls of
hub 2018/03/19 11:48:43 nit: "Destroys"
sergei 2018/03/19 12:34:26 Done.
101 * `Dispatch` have no effect.
102 */
103 void Invalidate()
104 {
105 std::unique_ptr<AsyncExecutor> tmp;
106 {
107 std::lock_guard<std::mutex> lock(asyncExecutorMutex);
108 tmp = move(executor);
109 }
110 }
111 private:
112 std::mutex asyncExecutorMutex;
113 std::unique_ptr<AsyncExecutor> executor;
114 };
65 } 115 }
OLDNEW
« no previous file with comments | « no previous file | include/AdblockPlus/Platform.h » ('j') | include/AdblockPlus/Platform.h » ('J')

Powered by Google App Engine
This is Rietveld