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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | include/AdblockPlus/Platform.h » ('j') | include/AdblockPlus/Platform.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/AdblockPlus/AsyncExecutor.h
diff --git a/include/AdblockPlus/AsyncExecutor.h b/include/AdblockPlus/AsyncExecutor.h
index ebbc137c40d87b3920ab9aa0ef22a1dbcc1a8de0..acbed96fc71c2ca0867c8c9401a8771ce85bbada 100644
--- a/include/AdblockPlus/AsyncExecutor.h
+++ b/include/AdblockPlus/AsyncExecutor.h
@@ -62,4 +62,54 @@ namespace AdblockPlus
SyncThreads threads;
ActiveObject threadCollector;
};
+
+ /**
+ * This class provides the same interface as `AsyncExecutor` but allows in
+ * a thread-safe manner invalidate internally held implementation of
+ * `AsyncExecutor`. Any subsequent calls of `Dispatch` have no effect,
+ * what allows to safely share `AsyncExecutor` but control it's operability.
+ */
+ class OptionalAsyncExecutor
+ {
+ public:
+ /**
+ * Contructor.
+ *
+ * Initially constructed the class behaves as `AsyncExecutor`.
+ */
+ OptionalAsyncExecutor()
+ : executor(new AsyncExecutor())
+ {
+ }
+
+ /**
+ * Creates a new thread in which the `call` will be executed.
+ * @param call is a function object which is called within a worker thread,
+ * different from the caller thread. There is no effect if `call` is
+ * empty or if `Invalidate` had been already called.
+ */
+ void Dispatch(const std::function<void()>& call)
+ {
+ std::lock_guard<std::mutex> lock(asyncExecutorMutex);
+ if (!executor)
+ return;
+ executor->Dispatch(call);
+ }
+
+ /**
+ * 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.
+ * `Dispatch` have no effect.
+ */
+ void Invalidate()
+ {
+ std::unique_ptr<AsyncExecutor> tmp;
+ {
+ std::lock_guard<std::mutex> lock(asyncExecutorMutex);
+ tmp = move(executor);
+ }
+ }
+ private:
+ std::mutex asyncExecutorMutex;
+ std::unique_ptr<AsyncExecutor> executor;
+ };
}
« 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