2014-02-05 05:29:26 +04: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/. */
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
#ifndef TaskQueue_h_
|
|
|
|
#define TaskQueue_h_
|
2014-02-05 05:29:26 +04:00
|
|
|
|
|
|
|
#include "mozilla/Monitor.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
#include "mozilla/TaskDispatcher.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
class nsIEventTarget;
|
2014-02-05 05:29:26 +04:00
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-07-16 21:06:49 +03:00
|
|
|
typedef MozPromise<bool, bool, false> ShutdownPromise;
|
2014-12-09 22:43:21 +03:00
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
// Abstracts executing runnables in order on an arbitrary event target. The
|
|
|
|
// runnables dispatched to the TaskQueue will be executed in the order in which
|
2014-02-05 05:29:26 +04:00
|
|
|
// they're received, and are guaranteed to not be executed concurrently.
|
|
|
|
// They may be executed on different threads, and a memory barrier is used
|
|
|
|
// to make this threadsafe for objects that aren't already threadsafe.
|
2016-09-14 06:12:15 +03:00
|
|
|
//
|
|
|
|
// Note, since a TaskQueue can also be converted to an nsIEventTarget using
|
|
|
|
// WrapAsEventTarget() its possible to construct a hierarchy of TaskQueues.
|
|
|
|
// Consider these three TaskQueues:
|
|
|
|
//
|
|
|
|
// TQ1 dispatches to the main thread
|
|
|
|
// TQ2 dispatches to TQ1
|
|
|
|
// TQ3 dispatches to TQ1
|
|
|
|
//
|
|
|
|
// This ensures there is only ever a single runnable from the entire chain on
|
|
|
|
// the main thread. It also ensures that TQ2 and TQ3 only have a single runnable
|
|
|
|
// in TQ1 at any time.
|
|
|
|
//
|
|
|
|
// This arrangement lets you prioritize work by dispatching runnables directly
|
|
|
|
// to TQ1. You can issue many runnables for important work. Meanwhile the TQ2
|
|
|
|
// and TQ3 work will always execute at most one runnable and then yield.
|
|
|
|
class TaskQueue : public AbstractThread
|
|
|
|
{
|
|
|
|
class EventTargetWrapper;
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
public:
|
2016-09-14 06:12:15 +03:00
|
|
|
explicit TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
|
|
|
|
bool aSupportsTailDispatch = false);
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2017-04-25 09:57:55 +03:00
|
|
|
TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
|
|
|
|
const char* aName,
|
|
|
|
bool aSupportsTailDispatch = false);
|
|
|
|
|
2015-04-14 09:53:07 +03:00
|
|
|
TaskDispatcher& TailDispatcher() override;
|
2015-04-07 21:44:10 +03:00
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue* AsTaskQueue() override { return this; }
|
2015-04-20 15:29:35 +03:00
|
|
|
|
2017-11-09 03:43:40 +03:00
|
|
|
nsresult Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
|
|
|
DispatchFailureHandling aFailureHandling = AssertDispatchSuccess,
|
|
|
|
DispatchReason aReason = NormalDispatch) override
|
2015-04-02 23:39:34 +03:00
|
|
|
{
|
2016-03-18 06:27:15 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = aRunnable;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2016-11-10 10:34:35 +03:00
|
|
|
nsresult rv = DispatchLocked(/* passed by ref */r, aFailureHandling, aReason);
|
2017-04-25 10:08:23 +03:00
|
|
|
#if defined(DEBUG) || !defined(RELEASE_OR_BETA) || defined(EARLY_BETA_OR_EARLIER)
|
|
|
|
if (NS_FAILED(rv) && aFailureHandling == AssertDispatchSuccess) {
|
|
|
|
MOZ_CRASH_UNSAFE_PRINTF("%s: Dispatch failed. rv=%x", mName, uint32_t(rv));
|
|
|
|
}
|
|
|
|
#endif
|
2017-11-09 03:43:40 +03:00
|
|
|
return rv;
|
2016-03-18 06:27:15 +03:00
|
|
|
}
|
|
|
|
// If the ownership of |r| is not transferred in DispatchLocked() due to
|
|
|
|
// dispatch failure, it will be deleted here outside the lock. We do so
|
|
|
|
// since the destructor of the runnable might access TaskQueue and result
|
|
|
|
// in deadlocks.
|
2015-04-02 23:39:34 +03:00
|
|
|
}
|
|
|
|
|
2017-06-09 23:19:13 +03:00
|
|
|
// Prevent a GCC warning about the other overload of Dispatch being hidden.
|
|
|
|
using AbstractThread::Dispatch;
|
|
|
|
|
2014-12-02 08:51:02 +03:00
|
|
|
// Puts the queue in a shutdown state and returns immediately. The queue will
|
|
|
|
// remain alive at least until all the events are drained, because the Runners
|
|
|
|
// hold a strong reference to the task queue, and one of them is always held
|
2016-09-14 06:12:15 +03:00
|
|
|
// by the target event queue when the task queue is non-empty.
|
2014-12-09 22:43:21 +03:00
|
|
|
//
|
|
|
|
// The returned promise is resolved when the queue goes empty.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ShutdownPromise> BeginShutdown();
|
2014-02-05 05:29:26 +04:00
|
|
|
|
|
|
|
// Blocks until all task finish executing.
|
|
|
|
void AwaitIdle();
|
|
|
|
|
2014-12-02 08:51:02 +03:00
|
|
|
// Blocks until the queue is flagged for shutdown and all tasks have finished
|
|
|
|
// executing.
|
|
|
|
void AwaitShutdownAndIdle();
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
bool IsEmpty();
|
2016-09-14 06:12:15 +03:00
|
|
|
uint32_t ImpreciseLengthForHeuristics();
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2014-02-18 02:53:52 +04:00
|
|
|
// Returns true if the current thread is currently running a Runnable in
|
2015-04-10 21:50:33 +03:00
|
|
|
// the task queue.
|
2015-04-02 23:39:34 +03:00
|
|
|
bool IsCurrentThreadIn() override;
|
2014-02-18 02:53:52 +04:00
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
// Create a new nsIEventTarget wrapper object that dispatches to this
|
|
|
|
// TaskQueue.
|
2017-05-23 00:25:43 +03:00
|
|
|
already_AddRefed<nsISerialEventTarget> WrapAsEventTarget();
|
2016-09-14 06:12:15 +03:00
|
|
|
|
2015-02-15 06:08:15 +03:00
|
|
|
protected:
|
2015-07-16 21:13:49 +03:00
|
|
|
virtual ~TaskQueue();
|
2015-02-15 06:08:15 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
|
|
|
|
// Blocks until all task finish executing. Called internally by methods
|
|
|
|
// that need to wait until the task queue is idle.
|
|
|
|
// mQueueMonitor must be held.
|
|
|
|
void AwaitIdleLocked();
|
|
|
|
|
2016-03-18 06:27:15 +03:00
|
|
|
nsresult DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
|
2015-04-15 20:54:25 +03:00
|
|
|
DispatchFailureHandling aFailureHandling,
|
2015-04-14 20:58:49 +03:00
|
|
|
DispatchReason aReason = NormalDispatch);
|
2014-08-15 04:05:00 +04:00
|
|
|
|
2015-06-11 06:06:09 +03:00
|
|
|
void MaybeResolveShutdown()
|
|
|
|
{
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mIsShutdown && !mIsRunning) {
|
|
|
|
mShutdownPromise.ResolveIfExists(true, __func__);
|
2016-09-14 06:12:15 +03:00
|
|
|
mTarget = nullptr;
|
2015-06-11 06:06:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
2014-02-05 05:29:26 +04:00
|
|
|
|
|
|
|
// Monitor that protects the queue and mIsRunning;
|
|
|
|
Monitor mQueueMonitor;
|
|
|
|
|
|
|
|
// Queue of tasks to run.
|
2015-04-15 04:01:44 +03:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>> mTasks;
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2014-02-18 02:53:52 +04:00
|
|
|
// The thread currently running the task queue. We store a reference
|
|
|
|
// to this so that IsCurrentThreadIn() can tell if the current thread
|
|
|
|
// is the thread currently running in the task queue.
|
2015-04-10 21:50:33 +03:00
|
|
|
//
|
|
|
|
// This may be read on any thread, but may only be written on mRunningThread.
|
|
|
|
// The thread can't die while we're running in it, and we only use it for
|
|
|
|
// pointer-comparison with the current thread anyway - so we make it atomic
|
|
|
|
// and don't refcount it.
|
2017-06-01 23:44:20 +03:00
|
|
|
Atomic<PRThread*> mRunningThread;
|
2014-02-18 02:53:52 +04:00
|
|
|
|
2015-04-07 21:26:35 +03:00
|
|
|
// RAII class that gets instantiated for each dispatched task.
|
2015-04-06 21:24:16 +03:00
|
|
|
class AutoTaskGuard : public AutoTaskDispatcher
|
2015-04-07 21:26:35 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-07-16 21:13:49 +03:00
|
|
|
explicit AutoTaskGuard(TaskQueue* aQueue)
|
2015-04-14 20:58:49 +03:00
|
|
|
: AutoTaskDispatcher(/* aIsTailDispatcher = */ true), mQueue(aQueue)
|
2016-09-14 06:12:15 +03:00
|
|
|
, mLastCurrentThread(nullptr)
|
2015-04-07 21:26:35 +03:00
|
|
|
{
|
|
|
|
// NB: We don't hold the lock to aQueue here. Don't do anything that
|
|
|
|
// might require it.
|
2015-04-06 21:24:16 +03:00
|
|
|
MOZ_ASSERT(!mQueue->mTailDispatcher);
|
|
|
|
mQueue->mTailDispatcher = this;
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
mLastCurrentThread = sCurrentThreadTLS.get();
|
2015-04-14 09:53:07 +03:00
|
|
|
sCurrentThreadTLS.set(aQueue);
|
2015-04-06 21:24:16 +03:00
|
|
|
|
2015-04-10 21:50:33 +03:00
|
|
|
MOZ_ASSERT(mQueue->mRunningThread == nullptr);
|
2017-06-01 23:44:20 +03:00
|
|
|
mQueue->mRunningThread = GetCurrentPhysicalThread();
|
2015-04-07 21:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoTaskGuard()
|
|
|
|
{
|
2015-04-16 22:24:54 +03:00
|
|
|
DrainDirectTasks();
|
|
|
|
|
2017-06-01 23:44:20 +03:00
|
|
|
MOZ_ASSERT(mQueue->mRunningThread == GetCurrentPhysicalThread());
|
2015-04-10 21:50:33 +03:00
|
|
|
mQueue->mRunningThread = nullptr;
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
sCurrentThreadTLS.set(mLastCurrentThread);
|
2015-04-06 21:24:16 +03:00
|
|
|
mQueue->mTailDispatcher = nullptr;
|
2015-04-07 21:26:35 +03:00
|
|
|
}
|
2015-04-06 21:24:16 +03:00
|
|
|
|
|
|
|
private:
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue* mQueue;
|
2016-09-14 06:12:15 +03:00
|
|
|
AbstractThread* mLastCurrentThread;
|
2015-04-07 21:26:35 +03:00
|
|
|
};
|
|
|
|
|
2015-04-06 21:24:16 +03:00
|
|
|
TaskDispatcher* mTailDispatcher;
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
// True if we've dispatched an event to the target to execute events from
|
2014-02-05 05:29:26 +04:00
|
|
|
// the queue.
|
|
|
|
bool mIsRunning;
|
|
|
|
|
|
|
|
// True if we've started our shutdown process.
|
|
|
|
bool mIsShutdown;
|
2015-07-16 21:06:49 +03:00
|
|
|
MozPromiseHolder<ShutdownPromise> mShutdownPromise;
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2017-04-25 09:57:55 +03:00
|
|
|
// The name of this TaskQueue. Useful when debugging dispatch failures.
|
|
|
|
const char* const mName;
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class Runner : public Runnable {
|
2015-02-15 06:08:15 +03:00
|
|
|
public:
|
2015-07-16 21:13:49 +03:00
|
|
|
explicit Runner(TaskQueue* aQueue)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("TaskQueue::Runner")
|
|
|
|
, mQueue(aQueue)
|
2015-02-15 06:08:15 +03:00
|
|
|
{
|
|
|
|
}
|
2016-08-08 03:54:50 +03:00
|
|
|
NS_IMETHOD Run() override;
|
2015-02-15 06:08:15 +03:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<TaskQueue> mQueue;
|
2015-02-15 06:08:15 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
#endif // TaskQueue_h_
|