|
20 | 20 |
|
21 | 21 | namespace mozilla {
|
22 | 22 |
|
| 23 | +class SimpleTaskQueue { |
| 24 | + public: |
| 25 | + SimpleTaskQueue() = default; |
| 26 | + virtual ~SimpleTaskQueue() = default; |
| 27 | + |
| 28 | + void AddTask(already_AddRefed<nsIRunnable> aRunnable) { |
| 29 | + if (!mTasks) { |
| 30 | + mTasks.emplace(); |
| 31 | + } |
| 32 | + mTasks->push(std::move(aRunnable)); |
| 33 | + } |
| 34 | + |
| 35 | + void DrainTasks() { |
| 36 | + if (!mTasks) { |
| 37 | + return; |
| 38 | + } |
| 39 | + auto& queue = mTasks.ref(); |
| 40 | + while (!queue.empty()) { |
| 41 | + nsCOMPtr<nsIRunnable> r = std::move(queue.front()); |
| 42 | + queue.pop(); |
| 43 | + r->Run(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + bool HaveTasks() const { return mTasks && !mTasks->empty(); } |
| 48 | + |
| 49 | + private: |
| 50 | + // We use a Maybe<> because (a) when used for DirectTasks it often doesn't get |
| 51 | + // anything put into it, and (b) the std::queue implementation in GNU |
| 52 | + // libstdc++ does two largish heap allocations when creating a new std::queue. |
| 53 | + Maybe<std::queue<nsCOMPtr<nsIRunnable>>> mTasks; |
| 54 | +}; |
| 55 | + |
23 | 56 | /*
|
24 | 57 | * A classic approach to cross-thread communication is to dispatch asynchronous
|
25 | 58 | * runnables to perform updates on other threads. This generally works well, but
|
@@ -88,23 +121,12 @@ class AutoTaskDispatcher : public TaskDispatcher {
|
88 | 121 | }
|
89 | 122 | }
|
90 | 123 |
|
91 |
| - bool HaveDirectTasks() const { |
92 |
| - return mDirectTasks.isSome() && !mDirectTasks->empty(); |
93 |
| - } |
| 124 | + bool HaveDirectTasks() const { return mDirectTasks.HaveTasks(); } |
94 | 125 |
|
95 |
| - void DrainDirectTasks() override { |
96 |
| - while (HaveDirectTasks()) { |
97 |
| - nsCOMPtr<nsIRunnable> r = mDirectTasks->front(); |
98 |
| - mDirectTasks->pop(); |
99 |
| - r->Run(); |
100 |
| - } |
101 |
| - } |
| 126 | + void DrainDirectTasks() override { mDirectTasks.DrainTasks(); } |
102 | 127 |
|
103 | 128 | void AddDirectTask(already_AddRefed<nsIRunnable> aRunnable) override {
|
104 |
| - if (mDirectTasks.isNothing()) { |
105 |
| - mDirectTasks.emplace(); |
106 |
| - } |
107 |
| - mDirectTasks->push(std::move(aRunnable)); |
| 129 | + mDirectTasks.AddTask(std::move(aRunnable)); |
108 | 130 | }
|
109 | 131 |
|
110 | 132 | void AddStateChangeTask(AbstractThread* aThread,
|
@@ -245,11 +267,7 @@ class AutoTaskDispatcher : public TaskDispatcher {
|
245 | 267 | return thread->Dispatch(r.forget(), reason);
|
246 | 268 | }
|
247 | 269 |
|
248 |
| - // Direct tasks. We use a Maybe<> because (a) this class is hot, (b) |
249 |
| - // mDirectTasks often doesn't get anything put into it, and (c) the |
250 |
| - // std::queue implementation in GNU libstdc++ does two largish heap |
251 |
| - // allocations when creating a new std::queue. |
252 |
| - mozilla::Maybe<std::queue<nsCOMPtr<nsIRunnable>>> mDirectTasks; |
| 270 | + SimpleTaskQueue mDirectTasks; |
253 | 271 |
|
254 | 272 | // Task groups, organized by thread.
|
255 | 273 | nsTArray<UniquePtr<PerThreadTaskGroup>> mTaskGroups;
|
|
0 commit comments