2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-11-15 05:47:30 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WorkerThread.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
#include "EventQueue.h"
|
|
|
|
#include "mozilla/ThreadEventQueue.h"
|
2014-11-15 05:47:30 +03:00
|
|
|
#include "nsIThreadInternal.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-01-31 10:20:07 +03:00
|
|
|
using namespace workers;
|
2014-11-15 05:47:30 +03:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The C stack size. We use the same stack size on all platforms for
|
|
|
|
// consistency.
|
|
|
|
const uint32_t kWorkerStackSize = 256 * sizeof(size_t) * 1024;
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
WorkerThreadFriendKey::WorkerThreadFriendKey()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(WorkerThreadFriendKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerThreadFriendKey::~WorkerThreadFriendKey()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(WorkerThreadFriendKey);
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerThread::Observer final
|
2014-11-15 05:47:30 +03:00
|
|
|
: public nsIThreadObserver
|
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Observer(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
private:
|
|
|
|
~Observer()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_NSITHREADOBSERVER
|
|
|
|
};
|
|
|
|
|
|
|
|
WorkerThread::WorkerThread()
|
2017-10-20 10:25:33 +03:00
|
|
|
: nsThread(MakeNotNull<ThreadEventQueue<mozilla::EventQueue>*>(
|
|
|
|
MakeUnique<mozilla::EventQueue>()),
|
Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm)
This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows:
Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage.
Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM.
There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities.
The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together.
One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization.
MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
|
|
|
nsThread::NOT_MAIN_THREAD,
|
|
|
|
kWorkerStackSize)
|
2017-07-07 02:20:23 +03:00
|
|
|
, mLock("WorkerThread::mLock")
|
2014-11-17 22:55:37 +03:00
|
|
|
, mWorkerPrivateCondVar(mLock, "WorkerThread::mWorkerPrivateCondVar")
|
|
|
|
, mWorkerPrivate(nullptr)
|
2015-01-15 00:50:03 +03:00
|
|
|
, mOtherThreadsDispatchingViaEventTarget(0)
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2014-11-15 05:47:30 +03:00
|
|
|
, mAcceptingNonWorkerRunnables(true)
|
2016-02-26 18:52:06 +03:00
|
|
|
#endif
|
2014-11-15 05:47:30 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerThread::~WorkerThread()
|
|
|
|
{
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
2015-01-15 00:50:03 +03:00
|
|
|
MOZ_ASSERT(!mOtherThreadsDispatchingViaEventTarget);
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(mAcceptingNonWorkerRunnables);
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<WorkerThread>
|
2014-11-17 22:55:37 +03:00
|
|
|
WorkerThread::Create(const WorkerThreadFriendKey& /* aKey */)
|
2014-11-15 05:47:30 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerThread> thread = new WorkerThread();
|
2018-01-25 02:20:27 +03:00
|
|
|
if (NS_FAILED(thread->Init(NS_LITERAL_CSTRING("DOM Worker")))) {
|
2014-11-15 05:47:30 +03:00
|
|
|
NS_WARNING("Failed to create new thread!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-17 22:55:37 +03:00
|
|
|
WorkerThread::SetWorker(const WorkerThreadFriendKey& /* aKey */,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
2014-11-15 05:47:30 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == mThread);
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
if (aWorkerPrivate) {
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(mAcceptingNonWorkerRunnables);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
mWorkerPrivate = aWorkerPrivate;
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2014-11-17 22:55:37 +03:00
|
|
|
mAcceptingNonWorkerRunnables = false;
|
2016-02-26 18:52:06 +03:00
|
|
|
#endif
|
2014-11-17 22:55:37 +03:00
|
|
|
}
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
mObserver = new Observer(aWorkerPrivate);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(AddObserver(mObserver));
|
2014-11-17 22:55:37 +03:00
|
|
|
} else {
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(RemoveObserver(mObserver));
|
2014-11-15 05:47:30 +03:00
|
|
|
mObserver = nullptr;
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(!mAcceptingNonWorkerRunnables);
|
2015-01-15 00:50:03 +03:00
|
|
|
MOZ_ASSERT(!mOtherThreadsDispatchingViaEventTarget,
|
2014-11-17 22:55:37 +03:00
|
|
|
"XPCOM Dispatch hapenning at the same time our thread is "
|
|
|
|
"being unset! This should not be possible!");
|
|
|
|
|
2015-01-15 00:50:03 +03:00
|
|
|
while (mOtherThreadsDispatchingViaEventTarget) {
|
2014-11-17 22:55:37 +03:00
|
|
|
mWorkerPrivateCondVar.Wait();
|
|
|
|
}
|
|
|
|
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2014-11-17 22:55:37 +03:00
|
|
|
mAcceptingNonWorkerRunnables = true;
|
2016-02-26 18:52:06 +03:00
|
|
|
#endif
|
2014-11-17 22:55:37 +03:00
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
2014-11-17 22:55:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WorkerThread::DispatchPrimaryRunnable(const WorkerThreadFriendKey& /* aKey */,
|
2016-06-01 03:04:54 +03:00
|
|
|
already_AddRefed<nsIRunnable> aRunnable)
|
2014-11-17 22:55:37 +03:00
|
|
|
{
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() != mThread);
|
2015-07-10 06:21:46 +03:00
|
|
|
MOZ_ASSERT(runnable);
|
2014-11-17 22:55:37 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(mAcceptingNonWorkerRunnables);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
nsresult rv = nsThread::Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2014-11-17 22:55:37 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-07-10 06:21:46 +03:00
|
|
|
WorkerThread::DispatchAnyThread(const WorkerThreadFriendKey& /* aKey */,
|
2016-06-01 03:04:54 +03:00
|
|
|
already_AddRefed<WorkerRunnable> aWorkerRunnable)
|
2014-11-17 22:55:37 +03:00
|
|
|
{
|
|
|
|
// May be called on any thread!
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
const bool onWorkerThread = PR_GetCurrentThread() == mThread;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(!mAcceptingNonWorkerRunnables);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
if (onWorkerThread) {
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aWorkerRunnable);
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
nsresult rv = nsThread::Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2014-11-17 22:55:37 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
// We don't need to notify the worker's condition variable here because we're
|
|
|
|
// being called from worker-controlled code and it will make sure to wake up
|
|
|
|
// the worker thread if needed.
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThread, nsThread)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-07-10 06:21:46 +03:00
|
|
|
WorkerThread::DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
|
|
|
return Dispatch(runnable.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-06-01 03:04:54 +03:00
|
|
|
WorkerThread::Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags)
|
2014-11-15 05:47:30 +03:00
|
|
|
{
|
|
|
|
// May be called on any thread!
|
2015-07-10 06:21:46 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable); // in case we exit early
|
2014-11-15 05:47:30 +03:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
// Workers only support asynchronous dispatch.
|
|
|
|
if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
const bool onWorkerThread = PR_GetCurrentThread() == mThread;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-07-10 06:21:46 +03:00
|
|
|
if (runnable && !onWorkerThread) {
|
|
|
|
nsCOMPtr<nsICancelableRunnable> cancelable = do_QueryInterface(runnable);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// Only enforce cancelable runnables after we've started the worker loop.
|
|
|
|
if (!mAcceptingNonWorkerRunnables) {
|
|
|
|
MOZ_ASSERT(cancelable,
|
|
|
|
"Only nsICancelableRunnable may be dispatched to a worker!");
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
WorkerPrivate* workerPrivate = nullptr;
|
|
|
|
if (onWorkerThread) {
|
|
|
|
// No need to lock here because it is only modified on this thread.
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
workerPrivate = mWorkerPrivate;
|
|
|
|
} else {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2015-01-15 00:50:03 +03:00
|
|
|
MOZ_ASSERT(mOtherThreadsDispatchingViaEventTarget < UINT32_MAX);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
workerPrivate = mWorkerPrivate;
|
|
|
|
|
2015-01-15 00:50:03 +03:00
|
|
|
// Incrementing this counter will make the worker thread sleep if it
|
|
|
|
// somehow tries to unset mWorkerPrivate while we're using it.
|
|
|
|
mOtherThreadsDispatchingViaEventTarget++;
|
2014-11-17 22:55:37 +03:00
|
|
|
}
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (runnable && onWorkerThread) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> workerRunnable = workerPrivate->MaybeWrapAsWorkerRunnable(runnable.forget());
|
2015-07-10 06:21:46 +03:00
|
|
|
rv = nsThread::Dispatch(workerRunnable.forget(), NS_DISPATCH_NORMAL);
|
2014-11-17 22:55:37 +03:00
|
|
|
} else {
|
2015-07-10 06:21:46 +03:00
|
|
|
rv = nsThread::Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2014-11-15 05:47:30 +03:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
if (!onWorkerThread && workerPrivate) {
|
|
|
|
// We need to wake the worker thread if we're not already on the right
|
|
|
|
// thread and the dispatch succeeded.
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
MutexAutoLock workerLock(workerPrivate->mMutex);
|
|
|
|
|
|
|
|
workerPrivate->mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now unset our waiting flag.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2015-01-15 00:50:03 +03:00
|
|
|
MOZ_ASSERT(mOtherThreadsDispatchingViaEventTarget);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
2015-01-15 00:50:03 +03:00
|
|
|
if (!--mOtherThreadsDispatchingViaEventTarget) {
|
|
|
|
mWorkerPrivateCondVar.Notify();
|
|
|
|
}
|
2014-11-17 22:55:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 05:47:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
NS_IMETHODIMP
|
2016-06-01 03:04:54 +03:00
|
|
|
WorkerThread::DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t)
|
2016-05-13 01:15:43 +03:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
uint32_t
|
|
|
|
WorkerThread::RecursionDepth(const WorkerThreadFriendKey& /* aKey */) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == mThread);
|
|
|
|
|
2015-04-24 23:04:50 +03:00
|
|
|
return mNestedEventLoopDepth;
|
2014-12-17 09:26:15 +03:00
|
|
|
}
|
|
|
|
|
2014-11-15 05:47:30 +03:00
|
|
|
NS_IMPL_ISUPPORTS(WorkerThread::Observer, nsIThreadObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-07-07 02:05:28 +03:00
|
|
|
WorkerThread::Observer::OnDispatchedEvent()
|
2014-11-15 05:47:30 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("OnDispatchedEvent() should never be called!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThread::Observer::OnProcessNextEvent(nsIThreadInternal* /* aThread */,
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
bool aMayWait)
|
2014-11-15 05:47:30 +03:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// If the PBackground child is not created yet, then we must permit
|
2016-06-30 23:54:06 +03:00
|
|
|
// blocking event processing to support
|
2017-08-03 12:21:53 +03:00
|
|
|
// BackgroundChild::GetOrCreateCreateForCurrentThread(). If this occurs
|
2016-06-30 23:54:06 +03:00
|
|
|
// then we are spinning on the event queue at the start of
|
2014-11-15 05:47:30 +03:00
|
|
|
// PrimaryWorkerRunnable::Run() and don't want to process the event in
|
|
|
|
// mWorkerPrivate yet.
|
|
|
|
if (aMayWait) {
|
2016-09-14 16:47:32 +03:00
|
|
|
MOZ_ASSERT(CycleCollectedJSContext::Get()->RecursionDepth() == 2);
|
2014-11-15 05:47:30 +03:00
|
|
|
MOZ_ASSERT(!BackgroundChild::GetForCurrentThread());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
mWorkerPrivate->OnProcessNextEvent();
|
2014-11-15 05:47:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThread::Observer::AfterProcessNextEvent(nsIThreadInternal* /* aThread */,
|
|
|
|
bool /* aEventWasProcessed */)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 16:10:46 +03:00
|
|
|
mWorkerPrivate->AfterProcessNextEvent();
|
2014-11-15 05:47:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|