From ad30b9265193219b7050092aefb3dabc18a38faa Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Sun, 16 Sep 2018 10:22:11 +0200 Subject: Stop using the MainThreadTaskQueue from service workers. --- dom/workers/ServiceWorkerClient.cpp | 2 +- dom/workers/ServiceWorkerClients.cpp | 8 ++++---- dom/workers/ServiceWorkerEvents.cpp | 20 ++++---------------- dom/workers/ServiceWorkerPrivate.cpp | 15 +++++++-------- dom/workers/ServiceWorkerRegistration.cpp | 8 ++++---- dom/workers/ServiceWorkerWindowClient.cpp | 4 ++-- dom/workers/WorkerScope.cpp | 2 +- 7 files changed, 23 insertions(+), 36 deletions(-) diff --git a/dom/workers/ServiceWorkerClient.cpp b/dom/workers/ServiceWorkerClient.cpp index 660512a5f..6ed3a6ea4 100644 --- a/dom/workers/ServiceWorkerClient.cpp +++ b/dom/workers/ServiceWorkerClient.cpp @@ -224,7 +224,7 @@ ServiceWorkerClient::PostMessage(JSContext* aCx, JS::Handle aMessage, return; } - aRv = workerPrivate->DispatchToMainThread(runnable.forget()); + aRv = NS_DispatchToMainThread(runnable); if (NS_WARN_IF(aRv.Failed())) { return; } diff --git a/dom/workers/ServiceWorkerClients.cpp b/dom/workers/ServiceWorkerClients.cpp index 11f864443..38f49e355 100644 --- a/dom/workers/ServiceWorkerClients.cpp +++ b/dom/workers/ServiceWorkerClients.cpp @@ -753,7 +753,7 @@ ServiceWorkerClients::Get(const nsAString& aClientId, ErrorResult& aRv) RefPtr r = new GetRunnable(promiseProxy, aClientId); - MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); return promise.forget(); } @@ -789,7 +789,7 @@ ServiceWorkerClients::MatchAll(const ClientQueryOptions& aOptions, new MatchAllRunnable(promiseProxy, NS_ConvertUTF16toUTF8(scope), aOptions.mIncludeUncontrolled); - MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); return promise.forget(); } @@ -830,7 +830,7 @@ ServiceWorkerClients::OpenWindow(const nsAString& aUrl, RefPtr r = new OpenWindowRunnable(promiseProxy, aUrl, scope); - MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); return promise.forget(); } @@ -859,6 +859,6 @@ ServiceWorkerClients::Claim(ErrorResult& aRv) RefPtr runnable = new ClaimRunnable(promiseProxy, NS_ConvertUTF16toUTF8(scope)); - MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(runnable.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable)); return promise.forget(); } diff --git a/dom/workers/ServiceWorkerEvents.cpp b/dom/workers/ServiceWorkerEvents.cpp index 1f79e2c92..8b375c635 100644 --- a/dom/workers/ServiceWorkerEvents.cpp +++ b/dom/workers/ServiceWorkerEvents.cpp @@ -400,13 +400,7 @@ void RespondWithCopyComplete(void* aClosure, nsresult aStatus) data->mScriptSpec, data->mResponseURLSpec); } - // In theory this can happen after the worker thread is terminated. - WorkerPrivate* worker = GetCurrentThreadWorkerPrivate(); - if (worker) { - MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(event.forget())); - } else { - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event.forget())); - } + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event)); } namespace { @@ -734,13 +728,7 @@ RespondWithHandler::CancelRequest(nsresult aStatus) { nsCOMPtr runnable = new CancelChannelRunnable(mInterceptedChannel, mRegistration, aStatus); - // Note, this may run off the worker thread during worker termination. - WorkerPrivate* worker = GetCurrentThreadWorkerPrivate(); - if (worker) { - MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(runnable.forget())); - } else { - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable.forget())); - } + NS_DispatchToMainThread(runnable); mRequestWasHandled = true; } @@ -873,8 +861,8 @@ public: mColumn = column; } - MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread( - NewRunnableMethod(this, &WaitUntilHandler::ReportOnMainThread))); + MOZ_ALWAYS_SUCCEEDS( + NS_DispatchToMainThread(NewRunnableMethod(this, &WaitUntilHandler::ReportOnMainThread))); } void diff --git a/dom/workers/ServiceWorkerPrivate.cpp b/dom/workers/ServiceWorkerPrivate.cpp index 24b2e11e6..23ae3b366 100644 --- a/dom/workers/ServiceWorkerPrivate.cpp +++ b/dom/workers/ServiceWorkerPrivate.cpp @@ -214,7 +214,7 @@ private: mDone = true; #endif mCallback->SetResult(aResult); - MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback)); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(mCallback)); } }; @@ -500,7 +500,7 @@ public: if (mRegistration) { nsCOMPtr runnable = new RegistrationUpdateRunnable(mRegistration, true /* time check */); - aWorkerPrivate->DispatchToMainThread(runnable.forget()); + NS_DispatchToMainThread(runnable.forget()); } ExtendableEventWorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult); @@ -541,7 +541,7 @@ public: Cancel() override { mCallback->SetResult(false); - MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback)); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(mCallback)); return WorkerRunnable::Cancel(); } @@ -637,7 +637,7 @@ public: mDone = true; mCallback->SetResult(aResult); - nsresult rv = mWorkerPrivate->DispatchToMainThread(mCallback); + nsresult rv = NS_DispatchToMainThread(mCallback); if (NS_WARN_IF(NS_FAILED(rv))) { NS_RUNTIMEABORT("Failed to dispatch life cycle event handler."); } @@ -768,7 +768,6 @@ public: void Report(uint16_t aReason = nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) { - WorkerPrivate* workerPrivate = mWorkerPrivate; mWorkerPrivate->AssertIsOnWorkerThread(); mWorkerPrivate = nullptr; @@ -780,7 +779,7 @@ public: NewRunnableMethod(this, &PushErrorReporter::ReportOnMainThread, aReason); MOZ_ALWAYS_TRUE(NS_SUCCEEDED( - workerPrivate->DispatchToMainThread(runnable.forget()))); + NS_DispatchToMainThread(runnable.forget()))); } void ReportOnMainThread(uint16_t aReason) @@ -1434,7 +1433,7 @@ public: Cancel() override { nsCOMPtr runnable = new ResumeRequest(mInterceptedChannel); - if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable))) { + if (NS_FAILED(NS_DispatchToMainThread(runnable))) { NS_WARNING("Failed to resume channel on FetchEventRunnable::Cancel()!\n"); } WorkerRunnable::Cancel(); @@ -1554,7 +1553,7 @@ private: NS_ERROR_INTERCEPTION_FAILED); } - MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(runnable.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable)); } RefPtr waitUntilPromise = event->GetPromise(); diff --git a/dom/workers/ServiceWorkerRegistration.cpp b/dom/workers/ServiceWorkerRegistration.cpp index 451bd2be9..b3cda2c10 100644 --- a/dom/workers/ServiceWorkerRegistration.cpp +++ b/dom/workers/ServiceWorkerRegistration.cpp @@ -1107,7 +1107,7 @@ ServiceWorkerRegistrationWorkerThread::Update(ErrorResult& aRv) } RefPtr r = new UpdateRunnable(proxy, mScope); - MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); return promise.forget(); } @@ -1139,7 +1139,7 @@ ServiceWorkerRegistrationWorkerThread::Unregister(ErrorResult& aRv) } RefPtr r = new StartUnregisterRunnable(proxy, mScope); - MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); return promise.forget(); } @@ -1161,7 +1161,7 @@ ServiceWorkerRegistrationWorkerThread::InitListener() nsCOMPtr r = NewRunnableMethod(mListener, &WorkerListener::StartListeningForEvents); - MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); } void @@ -1183,7 +1183,7 @@ ServiceWorkerRegistrationWorkerThread::ReleaseListener() nsCOMPtr r = NewRunnableMethod(mListener, &WorkerListener::StopListeningForEvents); - MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); mListener = nullptr; mWorkerPrivate = nullptr; diff --git a/dom/workers/ServiceWorkerWindowClient.cpp b/dom/workers/ServiceWorkerWindowClient.cpp index 2ce0603cf..bae747214 100644 --- a/dom/workers/ServiceWorkerWindowClient.cpp +++ b/dom/workers/ServiceWorkerWindowClient.cpp @@ -183,7 +183,7 @@ ServiceWorkerWindowClient::Focus(ErrorResult& aRv) const if (promiseProxy) { RefPtr r = new ClientFocusRunnable(mWindowId, promiseProxy); - MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); } else { promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR); } @@ -549,7 +549,7 @@ ServiceWorkerWindowClient::Navigate(const nsAString& aUrl, ErrorResult& aRv) if (promiseProxy) { RefPtr r = new ClientNavigateRunnable(mWindowId, aUrl, scope, promiseProxy); - MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); } else { promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR); } diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 54252e53b..ace1a09fc 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -717,7 +717,7 @@ ServiceWorkerGlobalScope::SkipWaiting(ErrorResult& aRv) new WorkerScopeSkipWaitingRunnable(promiseProxy, NS_ConvertUTF16toUTF8(mScope)); - MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(runnable.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable)); return promise.forget(); } -- cgit v1.2.3 From b2cbc7b654d51543d9b5e75f0397bdc8c8b5fc5b Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Sun, 16 Sep 2018 10:26:02 +0200 Subject: Stop using the worker MainThreadTaskQueue from dom/fetch. --- dom/fetch/Fetch.cpp | 2 +- dom/fetch/FetchConsumer.cpp | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp index e2c139426..f944352e3 100644 --- a/dom/fetch/Fetch.cpp +++ b/dom/fetch/Fetch.cpp @@ -260,7 +260,7 @@ FetchRequest(nsIGlobalObject* aGlobal, const RequestOrUSVString& aInput, } RefPtr run = new MainThreadFetchRunnable(resolver, r); - worker->DispatchToMainThread(run.forget()); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(run)); } return p.forget(); diff --git a/dom/fetch/FetchConsumer.cpp b/dom/fetch/FetchConsumer.cpp index 42dfcbaba..e82e5ec51 100644 --- a/dom/fetch/FetchConsumer.cpp +++ b/dom/fetch/FetchConsumer.cpp @@ -339,11 +339,7 @@ FetchBodyConsumer::Create(nsIGlobalObject* aGlobal, nsCOMPtr r = new BeginConsumeBodyRunnable(consumer); - if (workerPrivate) { - aRv = workerPrivate->DispatchToMainThread(r.forget()); - } else { - aRv = NS_DispatchToMainThread(r.forget()); - } + aRv = NS_DispatchToMainThread(r.forget()); if (NS_WARN_IF(aRv.Failed())) { return nullptr; @@ -655,9 +651,7 @@ FetchBodyConsumer::ShutDownMainThreadConsuming() nsCOMPtr r = NS_NewRunnableFunction( [self] () { self->ShutDownMainThreadConsuming(); }); - WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); - MOZ_ASSERT(workerPrivate); - workerPrivate->DispatchToMainThread(r.forget()); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r.forget())); return; } -- cgit v1.2.3 From 0384dc34be59a77d74cc9a1f8e754694b95dd13a Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Sun, 16 Sep 2018 11:43:06 +0200 Subject: Send worker-runnables destined for the main thread actually to the main thread. A case of "one queue too many" here. Instead of worker runnables being sent to the main thread where they are supposed to run, they are put in a task queue per-worker. This is devastating for performance if many workers are running. --- dom/workers/WorkerPrivate.cpp | 10 +++++----- dom/workers/WorkerRunnable.cpp | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index 612090027..6bc5c4f96 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -404,7 +404,7 @@ private: RefPtr runnable = new MainThreadReleaseRunnable(doomed, loadGroupToCancel); - if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable.forget()))) { + if (NS_FAILED(NS_DispatchToMainThread(runnable))) { NS_WARNING("Failed to dispatch, going to leak!"); } @@ -4078,7 +4078,7 @@ WorkerDebugger::PostMessageToDebugger(const nsAString& aMessage) RefPtr runnable = new PostDebuggerMessageRunnable(this, aMessage); - if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable.forget()))) { + if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) { NS_WARNING("Failed to post message to debugger on main thread!"); } } @@ -4103,7 +4103,7 @@ WorkerDebugger::ReportErrorToDebugger(const nsAString& aFilename, RefPtr runnable = new ReportDebuggerErrorRunnable(this, aFilename, aLineno, aMessage); - if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable.forget()))) { + if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) { NS_WARNING("Failed to report error to debugger on main thread!"); } } @@ -4862,7 +4862,7 @@ WorkerPrivate::MaybeDispatchLoadFailedRunnable() return; } - MOZ_ALWAYS_SUCCEEDS(DispatchToMainThread(runnable.forget())); + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable.forget())); } nsIEventTarget* @@ -5083,7 +5083,7 @@ WorkerPrivate::ScheduleDeletion(WorkerRanOrNot aRanOrNot) else { RefPtr runnable = new TopLevelWorkerFinishedRunnable(this); - if (NS_FAILED(DispatchToMainThread(runnable.forget()))) { + if (NS_FAILED(NS_DispatchToMainThread(runnable))) { NS_WARNING("Failed to dispatch runnable!"); } } diff --git a/dom/workers/WorkerRunnable.cpp b/dom/workers/WorkerRunnable.cpp index 9e6d4cfb8..60d69d4e4 100644 --- a/dom/workers/WorkerRunnable.cpp +++ b/dom/workers/WorkerRunnable.cpp @@ -117,7 +117,10 @@ WorkerRunnable::DispatchInternal() return NS_SUCCEEDED(parent->Dispatch(runnable.forget())); } - return NS_SUCCEEDED(mWorkerPrivate->DispatchToMainThread(runnable.forget())); + nsCOMPtr mainThread = do_GetMainThread(); + MOZ_ASSERT(mainThread); + + return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL)); } void @@ -553,7 +556,10 @@ WorkerControlRunnable::DispatchInternal() return NS_SUCCEEDED(parent->DispatchControlRunnable(runnable.forget())); } - return NS_SUCCEEDED(mWorkerPrivate->DispatchToMainThread(runnable.forget())); + nsCOMPtr mainThread = do_GetMainThread(); + MOZ_ASSERT(mainThread); + + return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL)); } NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable) @@ -580,7 +586,10 @@ WorkerMainThreadRunnable::Dispatch(Status aFailStatus, ErrorResult& aRv) return; } - DebugOnly rv = mWorkerPrivate->DispatchToMainThread(this); + RefPtr runnable(this); + + DebugOnly rv = + NS_DispatchToMainThread(runnable.forget(), NS_DISPATCH_NORMAL); MOZ_ASSERT(NS_SUCCEEDED(rv), "Should only fail after xpcom-shutdown-threads and we're gone by then"); @@ -670,7 +679,7 @@ WorkerProxyToMainThreadRunnable::Dispatch() return false; } - if (NS_WARN_IF(NS_FAILED(mWorkerPrivate->DispatchToMainThread(this)))) { + if (NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(this)))) { ReleaseWorker(); RunBackOnWorkerThread(); return false; -- cgit v1.2.3