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: fix typo Created March 19, 2018, 12:33 p.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') | no next file with comments »
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..9c6fc5da6f8b9fba8c9814448e7f2ada136acff4 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);
+ }
+
+ /**
+ * Destroys internally held `AsyncExecutor`, any subsequent calls of
+ * `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') | no next file with comments »

Powered by Google App Engine
This is Rietveld