OLD | NEW |
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 Loading... |
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 } | 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 * Destroys internally held `AsyncExecutor`, any subsequent calls of |
| 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 }; |
| 115 } |
OLD | NEW |