From 43a1cd6c169ae44913b7d5eebd11855cbc9311fd Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 8 Jun 2016 07:12:05 +0200 Subject: [PATCH] Bug 1277226 - Implement MainThreadWorkerRunnable, r=sicking --- dom/base/WebSocket.cpp | 21 +--------- dom/bindings/BindingUtils.cpp | 17 +------- dom/fetch/Fetch.cpp | 70 +++---------------------------- dom/notification/Notification.cpp | 21 +--------- dom/workers/WorkerRunnable.cpp | 7 ---- dom/workers/WorkerRunnable.h | 34 ++++++++++++++- 6 files changed, 45 insertions(+), 125 deletions(-) diff --git a/dom/base/WebSocket.cpp b/dom/base/WebSocket.cpp index 87b9b77186f1..ce2f41c57f32 100644 --- a/dom/base/WebSocket.cpp +++ b/dom/base/WebSocket.cpp @@ -2607,11 +2607,11 @@ WebSocketImpl::GetStatus(nsresult* aStatus) namespace { -class CancelRunnable final : public WorkerRunnable +class CancelRunnable final : public MainThreadWorkerRunnable { public: CancelRunnable(WorkerPrivate* aWorkerPrivate, WebSocketImpl* aImpl) - : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) + : MainThreadWorkerRunnable(aWorkerPrivate) , mImpl(aImpl) { } @@ -2629,23 +2629,6 @@ public: aWorkerPrivate->ModifyBusyCountFromWorker(false); } - bool - PreDispatch(WorkerPrivate* aWorkerPrivate) override - { - // We don't call WorkerRunnable::PreDispatch because it would assert the - // wrong thing about which thread we're on. - AssertIsOnMainThread(); - return true; - } - - void - PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override - { - // We don't call WorkerRunnable::PostDispatch because it would assert the - // wrong thing about which thread we're on. - AssertIsOnMainThread(); - } - private: RefPtr mImpl; }; diff --git a/dom/bindings/BindingUtils.cpp b/dom/bindings/BindingUtils.cpp index 2f9c5669792d..87e35cd605f4 100644 --- a/dom/bindings/BindingUtils.cpp +++ b/dom/bindings/BindingUtils.cpp @@ -3358,14 +3358,14 @@ private: void ReleaseWorker() { - class ReleaseRunnable final : public WorkerRunnable + class ReleaseRunnable final : public MainThreadWorkerRunnable { RefPtr mRunnable; public: ReleaseRunnable(WorkerPrivate* aWorkerPrivate, DeprecationWarningRunnable* aRunnable) - : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) + : MainThreadWorkerRunnable(aWorkerPrivate) , mRunnable(aRunnable) {} @@ -3378,19 +3378,6 @@ private: aWorkerPrivate->RemoveFeature(mRunnable); return true; } - - virtual bool - PreDispatch(WorkerPrivate* aWorkerPrivate) override - { - AssertIsOnMainThread(); - return true; - } - - virtual void - PostDispatch(WorkerPrivate* aWorkerPrivate, - bool aDispatchResult) override - { - } }; RefPtr runnable = diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp index e05e3be95a34..2eabc87505a9 100644 --- a/dom/fetch/Fetch.cpp +++ b/dom/fetch/Fetch.cpp @@ -262,7 +262,7 @@ MainThreadFetchResolver::~MainThreadFetchResolver() NS_ASSERT_OWNINGTHREAD(MainThreadFetchResolver); } -class WorkerFetchResponseRunnable final : public WorkerRunnable +class WorkerFetchResponseRunnable final : public MainThreadWorkerRunnable { RefPtr mResolver; // Passed from main thread to worker thread after being initialized. @@ -271,7 +271,7 @@ public: WorkerFetchResponseRunnable(WorkerPrivate* aWorkerPrivate, WorkerFetchResolver* aResolver, InternalResponse* aResponse) - : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) + : MainThreadWorkerRunnable(aWorkerPrivate) , mResolver(aResolver) , mInternalResponse(aResponse) { @@ -296,25 +296,6 @@ public: } return true; } - - bool - PreDispatch(WorkerPrivate* aWorkerPrivate) override - { - // Don't call default PreDispatch() since it incorrectly asserts we are - // dispatching from the parent worker thread. We always dispatch from - // the main thread. - AssertIsOnMainThread(); - return true; - } - - void - PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override - { - // Don't call default PostDispatch() since it incorrectly asserts we are - // dispatching from the parent worker thread. We always dispatch from - // the main thread. - AssertIsOnMainThread(); - } }; class WorkerFetchResponseEndBase @@ -336,13 +317,12 @@ public: } }; -class WorkerFetchResponseEndRunnable final : public WorkerRunnable +class WorkerFetchResponseEndRunnable final : public MainThreadWorkerRunnable , public WorkerFetchResponseEndBase { public: explicit WorkerFetchResponseEndRunnable(PromiseWorkerProxy* aPromiseProxy) - : WorkerRunnable(aPromiseProxy->GetWorkerPrivate(), - WorkerThreadUnchangedBusyCount) + : MainThreadWorkerRunnable(aPromiseProxy->GetWorkerPrivate()) , WorkerFetchResponseEndBase(aPromiseProxy) { } @@ -362,25 +342,6 @@ public: Run(); return WorkerRunnable::Cancel(); } - - bool - PreDispatch(WorkerPrivate* aWorkerPrivate) override - { - // Don't call default PreDispatch() since it incorrectly asserts we are - // dispatching from the parent worker thread. We always dispatch from - // the main thread. - AssertIsOnMainThread(); - return true; - } - - void - PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override - { - // Don't call default PostDispatch() since it incorrectly asserts we are - // dispatching from the parent worker thread. We always dispatch from - // the main thread. - AssertIsOnMainThread(); - } }; class WorkerFetchResponseEndControlRunnable final : public MainThreadWorkerControlRunnable @@ -643,7 +604,7 @@ namespace { * Called on successfully reading the complete stream. */ template -class ContinueConsumeBodyRunnable final : public WorkerRunnable +class ContinueConsumeBodyRunnable final : public MainThreadWorkerRunnable { // This has been addrefed before this runnable is dispatched, // released in WorkerRun(). @@ -655,7 +616,7 @@ class ContinueConsumeBodyRunnable final : public WorkerRunnable public: ContinueConsumeBodyRunnable(FetchBody* aFetchBody, nsresult aStatus, uint32_t aLength, uint8_t* aResult) - : WorkerRunnable(aFetchBody->mWorkerPrivate, WorkerThreadUnchangedBusyCount) + : MainThreadWorkerRunnable(aFetchBody->mWorkerPrivate) , mFetchBody(aFetchBody) , mStatus(aStatus) , mLength(aLength) @@ -670,25 +631,6 @@ public: mFetchBody->ContinueConsumeBody(mStatus, mLength, mResult); return true; } - - bool - PreDispatch(WorkerPrivate* aWorkerPrivate) override - { - // Don't call default PreDispatch() since it incorrectly asserts we are - // dispatching from the parent worker thread. We always dispatch from - // the main thread. - AssertIsOnMainThread(); - return true; - } - - void - PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override - { - // Don't call default PostDispatch() since it incorrectly asserts we are - // dispatching from the parent worker thread. We always dispatch from - // the main thread. - AssertIsOnMainThread(); - } }; // OnStreamComplete always adopts the buffer, utility class to release it in diff --git a/dom/notification/Notification.cpp b/dom/notification/Notification.cpp index 938810081c0a..4eaafe53f621 100644 --- a/dom/notification/Notification.cpp +++ b/dom/notification/Notification.cpp @@ -361,31 +361,14 @@ CheckScope(nsIPrincipal* aPrincipal, const nsACString& aScope) // Subclass that can be directly dispatched to child workers from the main // thread. -class NotificationWorkerRunnable : public WorkerRunnable +class NotificationWorkerRunnable : public MainThreadWorkerRunnable { protected: explicit NotificationWorkerRunnable(WorkerPrivate* aWorkerPrivate) - : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) + : MainThreadWorkerRunnable(aWorkerPrivate) { } - bool - PreDispatch(WorkerPrivate* aWorkerPrivate) override - { - // We don't call WorkerRunnable::PreDispatch because it would assert the - // wrong thing about which thread we're on. - AssertIsOnMainThread(); - return true; - } - - void - PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override - { - // We don't call WorkerRunnable::PostDispatch because it would assert the - // wrong thing about which thread we're on. - AssertIsOnMainThread(); - } - bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override { diff --git a/dom/workers/WorkerRunnable.cpp b/dom/workers/WorkerRunnable.cpp index 67d85ec5365e..cdb55125d2df 100644 --- a/dom/workers/WorkerRunnable.cpp +++ b/dom/workers/WorkerRunnable.cpp @@ -603,13 +603,6 @@ WorkerControlRunnable::DispatchInternal() return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL)); } -void -MainThreadWorkerControlRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate, - bool aDispatchResult) -{ - AssertIsOnMainThread(); -} - NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable) WorkerMainThreadRunnable::WorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate, diff --git a/dom/workers/WorkerRunnable.h b/dom/workers/WorkerRunnable.h index 318780fd870f..9d018fb2596e 100644 --- a/dom/workers/WorkerRunnable.h +++ b/dom/workers/WorkerRunnable.h @@ -300,6 +300,35 @@ private: using WorkerRunnable::Cancel; }; +// A convenience class for WorkerRunnables that originate on the main +// thread. +class MainThreadWorkerRunnable : public WorkerRunnable +{ +protected: + explicit MainThreadWorkerRunnable(WorkerPrivate* aWorkerPrivate) + : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount) + { + AssertIsOnMainThread(); + } + + virtual ~MainThreadWorkerRunnable() + {} + + virtual bool + PreDispatch(WorkerPrivate* aWorkerPrivate) override + { + AssertIsOnMainThread(); + return true; + } + + virtual void + PostDispatch(WorkerPrivate* aWorkerPrivate, + bool aDispatchResult) override + { + AssertIsOnMainThread(); + } +}; + // A convenience class for WorkerControlRunnables that originate on the main // thread. class MainThreadWorkerControlRunnable : public WorkerControlRunnable @@ -320,7 +349,10 @@ protected: } virtual void - PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override; + PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override + { + AssertIsOnMainThread(); + } }; // A WorkerRunnable that should be dispatched from the worker to itself for