Skip to content

Commit a56b255

Browse files
author
Jean-Yves Avenard
committed
Bug 1644009 - P13. Don't have AutoTaskGuard inherit from AutoTaskDispatcher. r=bholley.
This prevent being able to assert in the AutoTaskDispatcher that we are using it from the right thread/taskqueue as the SimpleTaskQueue object isn't thread-safe. We want to assert that all nsIDirectTaskDispatcher methods are only ever accessed on the underlying thread. To do so require that the scope of AutoTaskDispatcher to terminate prior the AutoTaskGuard one. Differential Revision: https://phabricator.services.mozilla.com/D79096
1 parent a943b8c commit a56b255

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

xpcom/threads/TaskQueue.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <queue>
1111

12+
#include "mozilla/Maybe.h"
1213
#include "mozilla/Monitor.h"
1314
#include "mozilla/MozPromise.h"
1415
#include "mozilla/RefPtr.h"
@@ -150,17 +151,16 @@ class TaskQueue : public AbstractThread, public nsIDirectTaskDispatcher {
150151
Atomic<PRThread*> mRunningThread;
151152

152153
// RAII class that gets instantiated for each dispatched task.
153-
class AutoTaskGuard : public AutoTaskDispatcher {
154+
class AutoTaskGuard {
154155
public:
155156
explicit AutoTaskGuard(TaskQueue* aQueue)
156-
: AutoTaskDispatcher(aQueue,
157-
/* aIsTailDispatcher = */ true),
158-
mQueue(aQueue),
159-
mLastCurrentThread(nullptr) {
157+
: mQueue(aQueue), mLastCurrentThread(nullptr) {
160158
// NB: We don't hold the lock to aQueue here. Don't do anything that
161159
// might require it.
162160
MOZ_ASSERT(!mQueue->mTailDispatcher);
163-
mQueue->mTailDispatcher = this;
161+
mTaskDispatcher.emplace(aQueue,
162+
/* aIsTailDispatcher = */ true);
163+
mQueue->mTailDispatcher = mTaskDispatcher.ptr();
164164

165165
mLastCurrentThread = sCurrentThreadTLS.get();
166166
sCurrentThreadTLS.set(aQueue);
@@ -170,7 +170,8 @@ class TaskQueue : public AbstractThread, public nsIDirectTaskDispatcher {
170170
}
171171

172172
~AutoTaskGuard() {
173-
DrainDirectTasks();
173+
mTaskDispatcher->DrainDirectTasks();
174+
mTaskDispatcher.reset();
174175

175176
MOZ_ASSERT(mQueue->mRunningThread == PR_GetCurrentThread());
176177
mQueue->mRunningThread = nullptr;
@@ -180,6 +181,7 @@ class TaskQueue : public AbstractThread, public nsIDirectTaskDispatcher {
180181
}
181182

182183
private:
184+
Maybe<AutoTaskDispatcher> mTaskDispatcher;
183185
TaskQueue* mQueue;
184186
AbstractThread* mLastCurrentThread;
185187
};

0 commit comments

Comments
 (0)