2016-11-07 23:30:17 +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: */
|
|
|
|
/* 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 "ThrottledEventQueue.h"
|
|
|
|
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.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 "mozilla/EventQueue.h"
|
2016-11-07 23:30:17 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/Unused.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 "nsThreadUtils.h"
|
2016-11-07 23:30:17 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace {} // anonymous namespace
|
|
|
|
|
|
|
|
// The ThrottledEventQueue is designed with inner and outer objects:
|
|
|
|
//
|
2018-04-07 00:44:42 +03:00
|
|
|
// XPCOM code base event target
|
2016-11-07 23:30:17 +03:00
|
|
|
// | |
|
2018-04-07 00:44:42 +03:00
|
|
|
// v v
|
|
|
|
// +-------+ +--------+
|
|
|
|
// | Outer | +-->|executor|
|
|
|
|
// +-------+ | +--------+
|
|
|
|
// | | |
|
2016-11-07 23:30:17 +03:00
|
|
|
// | +-------+ |
|
|
|
|
// +-->| Inner |<--+
|
|
|
|
// +-------+
|
|
|
|
//
|
|
|
|
// Client code references the outer nsIEventTarget which in turn references
|
2018-04-07 00:44:42 +03:00
|
|
|
// an inner object, which actually holds the queue of runnables.
|
2016-11-07 23:30:17 +03:00
|
|
|
//
|
2018-04-07 00:44:42 +03:00
|
|
|
// Whenever the queue is non-empty (and not paused), it keeps an "executor"
|
|
|
|
// runnable dispatched to the base event target. Each time the executor is run,
|
|
|
|
// it draws the next event from Inner's queue and runs it. If that queue has
|
|
|
|
// more events, the executor is dispatched to the base again.
|
2016-11-07 23:30:17 +03:00
|
|
|
//
|
2018-04-07 00:44:42 +03:00
|
|
|
// The executor holds a strong reference to the Inner object. This means that if
|
|
|
|
// the outer object is dereferenced and destroyed, the Inner object will remain
|
|
|
|
// live for as long as the executor exists - that is, until the Inner's queue is
|
|
|
|
// empty.
|
2016-11-07 23:30:17 +03:00
|
|
|
//
|
2018-10-23 09:21:10 +03:00
|
|
|
// A Paused ThrottledEventQueue does not enqueue an executor when new events are
|
|
|
|
// added. Any executor previously queued on the base event target draws no
|
|
|
|
// events from a Paused ThrottledEventQueue, and returns without re-enqueueing
|
|
|
|
// itself. Since there is no executor keeping the Inner object alive until its
|
|
|
|
// queue is empty, dropping a Paused ThrottledEventQueue may drop the Inner
|
|
|
|
// while it still owns events. This is the correct behavior: if there are no
|
|
|
|
// references to it, it will never be Resumed, and thus it will never dispatch
|
|
|
|
// events again.
|
|
|
|
//
|
|
|
|
// Resuming a ThrottledEventQueue must dispatch an executor, so calls to Resume
|
|
|
|
// are fallible for the same reasons as calls to Dispatch.
|
|
|
|
//
|
2018-04-07 00:44:42 +03:00
|
|
|
// The xpcom shutdown process drains the main thread's event queue several
|
|
|
|
// times, so if a ThrottledEventQueue is being driven by the main thread, it
|
|
|
|
// should get emptied out by the time we reach the "eventq shutdown" phase.
|
|
|
|
class ThrottledEventQueue::Inner final : public nsISupports {
|
2016-11-07 23:30:17 +03:00
|
|
|
// The runnable which is dispatched to the underlying base target. Since
|
|
|
|
// we only execute one event at a time we just re-use a single instance
|
|
|
|
// of this class while there are events left in the queue.
|
2019-03-05 19:47:05 +03:00
|
|
|
class Executor final : public Runnable, public nsIRunnablePriority {
|
2018-04-07 00:44:42 +03:00
|
|
|
// The Inner whose runnables we execute. mInner->mExecutor points
|
|
|
|
// to this executor, forming a reference loop.
|
2016-11-07 23:30:17 +03:00
|
|
|
RefPtr<Inner> mInner;
|
|
|
|
|
2019-03-05 19:47:05 +03:00
|
|
|
~Executor() = default;
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
public:
|
|
|
|
explicit Executor(Inner* aInner)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("ThrottledEventQueue::Inner::Executor"), mInner(aInner) {}
|
2016-11-07 23:30:17 +03:00
|
|
|
|
2019-03-05 19:47:05 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
NS_IMETHODIMP
|
2017-01-19 01:01:15 +03:00
|
|
|
Run() override {
|
2016-11-07 23:30:17 +03:00
|
|
|
mInner->ExecuteRunnable();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-01-19 01:01:15 +03:00
|
|
|
|
2019-03-05 19:47:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetPriority(uint32_t* aPriority) override {
|
|
|
|
*aPriority = mInner->mPriority;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
#ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2017-01-19 01:01:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
GetName(nsACString& aName) override { return mInner->CurrentName(aName); }
|
2018-03-23 21:53:55 +03:00
|
|
|
#endif
|
2016-11-07 23:30:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
mutable Mutex mMutex;
|
|
|
|
mutable CondVar mIdleCondVar;
|
|
|
|
|
2018-04-07 00:44:42 +03:00
|
|
|
// As-of-yet unexecuted runnables queued on this ThrottledEventQueue.
|
2018-10-23 09:21:10 +03:00
|
|
|
//
|
|
|
|
// Used from any thread; protected by mMutex. Signals mIdleCondVar when
|
|
|
|
// emptied.
|
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
|
|
|
EventQueue mEventQueue;
|
2016-11-07 23:30:17 +03:00
|
|
|
|
2018-04-07 00:44:42 +03:00
|
|
|
// The event target we dispatch our events (actually, just our Executor) to.
|
2018-10-23 09:21:10 +03:00
|
|
|
//
|
|
|
|
// Written only during construction. Readable by any thread without locking.
|
2017-06-13 23:40:00 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mBaseTarget;
|
2016-11-07 23:30:17 +03:00
|
|
|
|
2018-04-07 00:44:42 +03:00
|
|
|
// The Executor that we dispatch to mBaseTarget to draw runnables from our
|
|
|
|
// queue. mExecutor->mInner points to this Inner, forming a reference loop.
|
2018-10-23 09:21:10 +03:00
|
|
|
//
|
|
|
|
// Used from any thread; protected by mMutex.
|
2016-11-07 23:30:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mExecutor;
|
|
|
|
|
2019-05-13 21:12:24 +03:00
|
|
|
const char* mName;
|
|
|
|
|
2019-03-05 19:47:05 +03:00
|
|
|
const uint32_t mPriority;
|
|
|
|
|
2018-10-23 09:21:10 +03:00
|
|
|
// True if this queue is currently paused.
|
|
|
|
// Used from any thread; protected by mMutex.
|
|
|
|
bool mIsPaused;
|
|
|
|
|
2019-05-13 21:12:24 +03:00
|
|
|
explicit Inner(nsISerialEventTarget* aBaseTarget, const char* aName,
|
|
|
|
uint32_t aPriority)
|
2016-11-07 23:30:17 +03:00
|
|
|
: mMutex("ThrottledEventQueue"),
|
|
|
|
mIdleCondVar(mMutex, "ThrottledEventQueue:Idle"),
|
|
|
|
mBaseTarget(aBaseTarget),
|
2019-05-13 21:12:24 +03:00
|
|
|
mName(aName),
|
2019-03-05 19:47:05 +03:00
|
|
|
mPriority(aPriority),
|
2019-05-13 21:12:24 +03:00
|
|
|
mIsPaused(false) {
|
2019-05-25 20:46:15 +03:00
|
|
|
MOZ_ASSERT(mName, "Must pass a valid name!");
|
|
|
|
}
|
2016-11-07 23:30:17 +03:00
|
|
|
|
|
|
|
~Inner() {
|
2018-04-07 00:44:42 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-10-23 09:21:10 +03:00
|
|
|
|
|
|
|
// As long as an executor exists, it had better keep us alive, since it's
|
|
|
|
// going to call ExecuteRunnable on us.
|
2016-11-07 23:30:17 +03:00
|
|
|
MOZ_ASSERT(!mExecutor);
|
2018-10-23 09:21:10 +03:00
|
|
|
|
|
|
|
// If we have any events in our queue, there should be an executor queued
|
|
|
|
// for them, and that should have kept us alive. The exception is that, if
|
|
|
|
// we're paused, we don't enqueue an executor.
|
|
|
|
MOZ_ASSERT(mEventQueue.IsEmpty(lock) || IsPaused(lock));
|
|
|
|
|
|
|
|
// Some runnables are only safe to drop on the main thread, so if our queue
|
|
|
|
// isn't empty, we'd better be on the main thread.
|
|
|
|
MOZ_ASSERT_IF(!mEventQueue.IsEmpty(lock), NS_IsMainThread());
|
2018-04-07 00:44:42 +03:00
|
|
|
#endif
|
2016-11-07 23:30:17 +03:00
|
|
|
}
|
|
|
|
|
2018-10-23 09:21:10 +03:00
|
|
|
// Make sure an executor has been queued on our base target. If we already
|
|
|
|
// have one, do nothing; otherwise, create and dispatch it.
|
|
|
|
nsresult EnsureExecutor(MutexAutoLock& lock) {
|
|
|
|
if (mExecutor) return NS_OK;
|
|
|
|
|
|
|
|
// Note, this creates a ref cycle keeping the inner alive
|
|
|
|
// until the queue is drained.
|
|
|
|
mExecutor = new Executor(this);
|
|
|
|
nsresult rv = mBaseTarget->Dispatch(mExecutor, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mExecutor = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-19 01:01:15 +03:00
|
|
|
nsresult CurrentName(nsACString& aName) {
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool currentThread = false;
|
|
|
|
mBaseTarget->IsOnCurrentThread(¤tThread);
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
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
|
|
|
event = mEventQueue.PeekEvent(lock);
|
2019-05-16 22:35:30 +03:00
|
|
|
// It is possible that mEventQueue wasn't empty when the executor
|
|
|
|
// was added to the queue, but someone processed events from mEventQueue
|
|
|
|
// before the executor, this is why mEventQueue is empty here
|
|
|
|
if (!event) {
|
|
|
|
aName.AssignLiteral("no runnables left in the ThrottledEventQueue");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-01-19 01:01:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(event)) {
|
|
|
|
nsresult rv = named->GetName(aName);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-05-13 21:12:24 +03:00
|
|
|
aName.AssignASCII(mName);
|
2017-01-19 01:01:15 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
void ExecuteRunnable() {
|
|
|
|
// Any thread
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool currentThread = false;
|
|
|
|
mBaseTarget->IsOnCurrentThread(¤tThread);
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-10-23 09:21:10 +03:00
|
|
|
// Normally, a paused queue doesn't dispatch any executor, but we might
|
|
|
|
// have been paused after the executor was already in flight. There's no
|
|
|
|
// way to yank the executor out of the base event target, so we just check
|
|
|
|
// for a paused queue here and return without running anything. We'll
|
|
|
|
// create a new executor when we're resumed.
|
|
|
|
if (IsPaused(lock)) {
|
|
|
|
// Note, this breaks a ref cycle.
|
|
|
|
mExecutor = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
// We only dispatch an executor runnable when we know there is something
|
|
|
|
// in the queue, so this should never fail.
|
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
|
|
|
event = mEventQueue.GetEvent(nullptr, lock);
|
|
|
|
MOZ_ASSERT(event);
|
2016-11-07 23:30:17 +03:00
|
|
|
|
|
|
|
// If there are more events in the queue, then dispatch the next
|
|
|
|
// executor. We do this now, before running the event, because
|
|
|
|
// the event might spin the event loop and we don't want to stall
|
|
|
|
// the queue.
|
2017-07-29 00:56:49 +03:00
|
|
|
if (mEventQueue.HasReadyEvent(lock)) {
|
2016-11-07 23:30:17 +03:00
|
|
|
// Dispatch the next base target runnable to attempt to execute
|
|
|
|
// the next throttled event. We must do this before executing
|
|
|
|
// the event in case the event spins the event loop.
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
mBaseTarget->Dispatch(mExecutor, NS_DISPATCH_NORMAL));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise the queue is empty and we can stop dispatching the
|
2018-04-07 00:44:42 +03:00
|
|
|
// executor.
|
2016-11-07 23:30:17 +03:00
|
|
|
else {
|
2018-04-07 00:44:42 +03:00
|
|
|
// Break the Executor::mInner / Inner::mExecutor reference loop.
|
2016-11-07 23:30:17 +03:00
|
|
|
mExecutor = nullptr;
|
|
|
|
mIdleCondVar.NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the event now that we have unlocked.
|
|
|
|
Unused << event->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-03-05 19:47:05 +03:00
|
|
|
static already_AddRefed<Inner> Create(nsISerialEventTarget* aBaseTarget,
|
2019-05-25 20:46:15 +03:00
|
|
|
const char* aName, uint32_t aPriority) {
|
2016-11-07 23:30:17 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-04-07 00:44:42 +03:00
|
|
|
MOZ_ASSERT(ClearOnShutdown_Internal::sCurrentShutdownPhase ==
|
|
|
|
ShutdownPhase::NotInShutdown);
|
2016-11-07 23:30:17 +03:00
|
|
|
|
2019-05-13 21:12:24 +03:00
|
|
|
RefPtr<Inner> ref = new Inner(aBaseTarget, aName, aPriority);
|
2016-11-07 23:30:17 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
// Any thread
|
|
|
|
return Length() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Length() const {
|
|
|
|
// Any thread
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mEventQueue.Count(lock);
|
|
|
|
}
|
|
|
|
|
2019-05-16 22:35:30 +03:00
|
|
|
already_AddRefed<nsIRunnable> GetEvent() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mEventQueue.GetEvent(nullptr, lock);
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
void AwaitIdle() const {
|
|
|
|
// Any thread, except the main thread or our base target. Blocking the
|
|
|
|
// main thread is forbidden. Blocking the base target is guaranteed to
|
|
|
|
// produce a deadlock.
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool onBaseTarget = false;
|
|
|
|
Unused << mBaseTarget->IsOnCurrentThread(&onBaseTarget);
|
|
|
|
MOZ_ASSERT(!onBaseTarget);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-10-23 09:21:10 +03:00
|
|
|
while (mExecutor || IsPaused(lock)) {
|
2016-11-07 23:30:17 +03:00
|
|
|
mIdleCondVar.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 09:21:10 +03:00
|
|
|
bool IsPaused() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return IsPaused(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPaused(const MutexAutoLock& aProofOfLock) const { return mIsPaused; }
|
|
|
|
|
|
|
|
nsresult SetIsPaused(bool aIsPaused) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
// If we will be unpaused, and we have events in our queue, make sure we
|
|
|
|
// have an executor queued on the base event target to run them. Do this
|
|
|
|
// before we actually change mIsPaused, since this is fallible.
|
|
|
|
if (!aIsPaused && !mEventQueue.IsEmpty(lock)) {
|
|
|
|
nsresult rv = EnsureExecutor(lock);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsPaused = aIsPaused;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
nsresult DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
|
|
// Any thread
|
|
|
|
nsCOMPtr<nsIRunnable> r = aEvent;
|
|
|
|
return Dispatch(r.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) {
|
|
|
|
MOZ_ASSERT(aFlags == NS_DISPATCH_NORMAL || aFlags == NS_DISPATCH_AT_END);
|
|
|
|
|
|
|
|
// Any thread
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2018-10-23 09:21:10 +03:00
|
|
|
if (!IsPaused(lock)) {
|
|
|
|
// Make sure we have an executor in flight to process events. This is
|
|
|
|
// fallible, so do it first. Our lock will prevent the executor from
|
|
|
|
// accessing the event queue before we add the event below.
|
|
|
|
nsresult rv = EnsureExecutor(lock);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2016-11-07 23:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only add the event to the underlying queue if are able to
|
|
|
|
// dispatch to our base target.
|
2019-01-26 20:18:05 +03:00
|
|
|
mEventQueue.PutEvent(std::move(aEvent), EventQueuePriority::Normal, lock);
|
2016-11-07 23:30:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aDelay) {
|
|
|
|
// The base target may implement this, but we don't. Always fail
|
|
|
|
// to provide consistent behavior.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2017-05-22 21:26:39 +03:00
|
|
|
bool IsOnCurrentThread() { return mBaseTarget->IsOnCurrentThread(); }
|
2016-11-07 23:30:17 +03:00
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
};
|
|
|
|
|
2018-04-07 00:44:42 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ThrottledEventQueue::Inner, nsISupports);
|
2016-11-07 23:30:17 +03:00
|
|
|
|
2019-03-05 19:47:05 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(ThrottledEventQueue::Inner::Executor, Runnable,
|
|
|
|
nsIRunnablePriority)
|
|
|
|
|
2017-05-23 00:25:43 +03:00
|
|
|
NS_IMPL_ISUPPORTS(ThrottledEventQueue, ThrottledEventQueue, nsIEventTarget,
|
2017-06-13 23:40:00 +03:00
|
|
|
nsISerialEventTarget);
|
2016-11-07 23:30:17 +03:00
|
|
|
|
|
|
|
ThrottledEventQueue::ThrottledEventQueue(already_AddRefed<Inner> aInner)
|
|
|
|
: mInner(aInner) {
|
|
|
|
MOZ_ASSERT(mInner);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ThrottledEventQueue> ThrottledEventQueue::Create(
|
2019-05-13 21:12:24 +03:00
|
|
|
nsISerialEventTarget* aBaseTarget, const char* aName, uint32_t aPriority) {
|
2016-11-07 23:30:17 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aBaseTarget);
|
|
|
|
|
2019-05-13 21:12:24 +03:00
|
|
|
RefPtr<Inner> inner = Inner::Create(aBaseTarget, aName, aPriority);
|
2016-11-07 23:30:17 +03:00
|
|
|
|
|
|
|
RefPtr<ThrottledEventQueue> ref = new ThrottledEventQueue(inner.forget());
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThrottledEventQueue::IsEmpty() const { return mInner->IsEmpty(); }
|
|
|
|
|
|
|
|
uint32_t ThrottledEventQueue::Length() const { return mInner->Length(); }
|
|
|
|
|
2019-05-16 22:35:30 +03:00
|
|
|
// Get the next runnable from the queue
|
|
|
|
already_AddRefed<nsIRunnable> ThrottledEventQueue::GetEvent() {
|
|
|
|
return mInner->GetEvent();
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
void ThrottledEventQueue::AwaitIdle() const { return mInner->AwaitIdle(); }
|
|
|
|
|
2018-10-23 09:21:10 +03:00
|
|
|
nsresult ThrottledEventQueue::SetIsPaused(bool aIsPaused) {
|
|
|
|
return mInner->SetIsPaused(aIsPaused);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThrottledEventQueue::IsPaused() const { return mInner->IsPaused(); }
|
|
|
|
|
2016-11-07 23:30:17 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ThrottledEventQueue::DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) {
|
|
|
|
return mInner->DispatchFromScript(aEvent, aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThrottledEventQueue::Dispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aFlags) {
|
2018-05-30 22:15:35 +03:00
|
|
|
return mInner->Dispatch(std::move(aEvent), aFlags);
|
2016-11-07 23:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThrottledEventQueue::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aFlags) {
|
2018-05-30 22:15:35 +03:00
|
|
|
return mInner->DelayedDispatch(std::move(aEvent), aFlags);
|
2016-11-07 23:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThrottledEventQueue::IsOnCurrentThread(bool* aResult) {
|
2017-05-22 21:26:39 +03:00
|
|
|
*aResult = mInner->IsOnCurrentThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
ThrottledEventQueue::IsOnCurrentThreadInfallible() {
|
|
|
|
return mInner->IsOnCurrentThread();
|
2016-11-07 23:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|