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; |
+ }; |
} |