2014-02-04 17:29:26 -08: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 11:13:49 -07:00
|
|
|
#ifndef TaskQueue_h_
|
|
|
|
#define TaskQueue_h_
|
2014-02-04 17:29:26 -08:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
2015-04-14 17:25:14 -07:00
|
|
|
#include "mozilla/unused.h"
|
2014-12-01 21:51:02 -08:00
|
|
|
#include "SharedThreadPool.h"
|
2014-02-04 17:29:26 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2015-07-16 11:06:49 -07:00
|
|
|
#include "MozPromise.h"
|
2015-04-06 11:24:16 -07:00
|
|
|
#include "TaskDispatcher.h"
|
2014-02-04 17:29:26 -08:00
|
|
|
|
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class SharedThreadPool;
|
|
|
|
|
2015-07-16 11:06:49 -07:00
|
|
|
typedef MozPromise<bool, bool, false> ShutdownPromise;
|
2014-12-09 11:43:21 -08:00
|
|
|
|
2014-02-04 17:29:26 -08:00
|
|
|
// Abstracts executing runnables in order in a thread pool. The runnables
|
2015-07-16 11:13:49 -07:00
|
|
|
// dispatched to the TaskQueue will be executed in the order in which
|
2014-02-04 17:29:26 -08: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.
|
2015-07-16 11:13:49 -07:00
|
|
|
class TaskQueue : public AbstractThread {
|
2014-02-04 17:29:26 -08:00
|
|
|
public:
|
2015-07-16 11:13:49 -07:00
|
|
|
explicit TaskQueue(already_AddRefed<SharedThreadPool> aPool, bool aSupportsTailDispatch = false);
|
2014-02-04 17:29:26 -08:00
|
|
|
|
2015-04-13 23:53:07 -07:00
|
|
|
TaskDispatcher& TailDispatcher() override;
|
2015-04-07 11:44:10 -07:00
|
|
|
|
2015-07-16 11:13:49 -07:00
|
|
|
TaskQueue* AsTaskQueue() override { return this; }
|
2015-04-20 05:29:35 -07:00
|
|
|
|
2015-04-14 17:25:14 -07:00
|
|
|
void Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
2015-04-14 10:58:49 -07:00
|
|
|
DispatchFailureHandling aFailureHandling = AssertDispatchSuccess,
|
|
|
|
DispatchReason aReason = NormalDispatch) override
|
2015-04-02 13:39:34 -07:00
|
|
|
{
|
2015-04-14 17:25:14 -07:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2015-04-15 10:54:25 -07:00
|
|
|
nsresult rv = DispatchLocked(Move(aRunnable), AbortIfFlushing, aFailureHandling, aReason);
|
2015-04-14 17:25:14 -07:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFailureHandling == DontAssertDispatchSuccess || NS_SUCCEEDED(rv));
|
|
|
|
unused << rv;
|
2015-04-02 13:39:34 -07:00
|
|
|
}
|
|
|
|
|
2015-04-14 17:14:49 -07:00
|
|
|
// DEPRECATED! Do not us, if a flush happens at the same time, this function
|
|
|
|
// can hang and block forever!
|
2015-06-17 07:00:52 -07:00
|
|
|
void SyncDispatch(already_AddRefed<nsIRunnable> aRunnable);
|
2014-07-29 23:53:34 -07:00
|
|
|
|
2014-12-01 21:51:02 -08: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
|
|
|
|
// by the threadpool event queue when the task queue is non-empty.
|
2014-12-09 11:43:21 -08:00
|
|
|
//
|
|
|
|
// The returned promise is resolved when the queue goes empty.
|
|
|
|
nsRefPtr<ShutdownPromise> BeginShutdown();
|
2014-02-04 17:29:26 -08:00
|
|
|
|
|
|
|
// Blocks until all task finish executing.
|
|
|
|
void AwaitIdle();
|
|
|
|
|
2014-12-01 21:51:02 -08:00
|
|
|
// Blocks until the queue is flagged for shutdown and all tasks have finished
|
|
|
|
// executing.
|
|
|
|
void AwaitShutdownAndIdle();
|
|
|
|
|
2014-02-04 17:29:26 -08:00
|
|
|
bool IsEmpty();
|
|
|
|
|
2014-02-17 14:53:52 -08:00
|
|
|
// Returns true if the current thread is currently running a Runnable in
|
2015-04-10 11:50:33 -07:00
|
|
|
// the task queue.
|
2015-04-02 13:39:34 -07:00
|
|
|
bool IsCurrentThreadIn() override;
|
2014-02-17 14:53:52 -08:00
|
|
|
|
2015-02-14 19:08:15 -08:00
|
|
|
protected:
|
2015-07-16 11:13:49 -07:00
|
|
|
virtual ~TaskQueue();
|
2015-02-14 19:08:15 -08:00
|
|
|
|
2014-02-04 17:29:26 -08: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();
|
|
|
|
|
2015-04-14 17:47:22 -07:00
|
|
|
enum DispatchMode { AbortIfFlushing, IgnoreFlushing };
|
2014-08-14 17:05:00 -07:00
|
|
|
|
2015-04-14 10:58:49 -07:00
|
|
|
nsresult DispatchLocked(already_AddRefed<nsIRunnable> aRunnable, DispatchMode aMode,
|
2015-04-15 10:54:25 -07:00
|
|
|
DispatchFailureHandling aFailureHandling,
|
2015-04-14 10:58:49 -07:00
|
|
|
DispatchReason aReason = NormalDispatch);
|
2014-08-14 17:05:00 -07:00
|
|
|
|
2015-06-10 20:06:09 -07:00
|
|
|
void MaybeResolveShutdown()
|
|
|
|
{
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mIsShutdown && !mIsRunning) {
|
|
|
|
mShutdownPromise.ResolveIfExists(true, __func__);
|
2015-06-10 20:06:54 -07:00
|
|
|
mPool = nullptr;
|
2015-06-10 20:06:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 17:29:26 -08:00
|
|
|
RefPtr<SharedThreadPool> mPool;
|
|
|
|
|
|
|
|
// Monitor that protects the queue and mIsRunning;
|
|
|
|
Monitor mQueueMonitor;
|
|
|
|
|
|
|
|
// Queue of tasks to run.
|
2015-04-14 18:01:44 -07:00
|
|
|
std::queue<nsCOMPtr<nsIRunnable>> mTasks;
|
2014-02-04 17:29:26 -08:00
|
|
|
|
2014-02-17 14:53:52 -08: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 11:50:33 -07: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.
|
|
|
|
Atomic<nsIThread*> mRunningThread;
|
2014-02-17 14:53:52 -08:00
|
|
|
|
2015-04-07 11:26:35 -07:00
|
|
|
// RAII class that gets instantiated for each dispatched task.
|
2015-04-06 11:24:16 -07:00
|
|
|
class AutoTaskGuard : public AutoTaskDispatcher
|
2015-04-07 11:26:35 -07:00
|
|
|
{
|
|
|
|
public:
|
2015-07-16 11:13:49 -07:00
|
|
|
explicit AutoTaskGuard(TaskQueue* aQueue)
|
2015-04-14 10:58:49 -07:00
|
|
|
: AutoTaskDispatcher(/* aIsTailDispatcher = */ true), mQueue(aQueue)
|
2015-04-07 11:26:35 -07:00
|
|
|
{
|
|
|
|
// NB: We don't hold the lock to aQueue here. Don't do anything that
|
|
|
|
// might require it.
|
2015-04-06 11:24:16 -07:00
|
|
|
MOZ_ASSERT(!mQueue->mTailDispatcher);
|
|
|
|
mQueue->mTailDispatcher = this;
|
|
|
|
|
2015-04-13 23:53:07 -07:00
|
|
|
MOZ_ASSERT(sCurrentThreadTLS.get() == nullptr);
|
|
|
|
sCurrentThreadTLS.set(aQueue);
|
2015-04-06 11:24:16 -07:00
|
|
|
|
2015-04-10 11:50:33 -07:00
|
|
|
MOZ_ASSERT(mQueue->mRunningThread == nullptr);
|
|
|
|
mQueue->mRunningThread = NS_GetCurrentThread();
|
2015-04-07 11:26:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoTaskGuard()
|
|
|
|
{
|
2015-04-16 12:24:54 -07:00
|
|
|
DrainDirectTasks();
|
|
|
|
|
2015-04-10 11:50:33 -07:00
|
|
|
MOZ_ASSERT(mQueue->mRunningThread == NS_GetCurrentThread());
|
|
|
|
mQueue->mRunningThread = nullptr;
|
|
|
|
|
2015-04-13 23:53:07 -07:00
|
|
|
sCurrentThreadTLS.set(nullptr);
|
2015-04-06 11:24:16 -07:00
|
|
|
mQueue->mTailDispatcher = nullptr;
|
2015-04-07 11:26:35 -07:00
|
|
|
}
|
2015-04-06 11:24:16 -07:00
|
|
|
|
|
|
|
private:
|
2015-07-16 11:13:49 -07:00
|
|
|
TaskQueue* mQueue;
|
2015-04-07 11:26:35 -07:00
|
|
|
};
|
|
|
|
|
2015-04-06 11:24:16 -07:00
|
|
|
TaskDispatcher* mTailDispatcher;
|
|
|
|
|
2014-02-04 17:29:26 -08:00
|
|
|
// True if we've dispatched an event to the pool to execute events from
|
|
|
|
// the queue.
|
|
|
|
bool mIsRunning;
|
|
|
|
|
|
|
|
// True if we've started our shutdown process.
|
|
|
|
bool mIsShutdown;
|
2015-07-16 11:06:49 -07:00
|
|
|
MozPromiseHolder<ShutdownPromise> mShutdownPromise;
|
2014-02-04 17:29:26 -08:00
|
|
|
|
2015-02-14 19:08:15 -08:00
|
|
|
// True if we're flushing; we reject new tasks if we're flushing.
|
|
|
|
bool mIsFlushing;
|
|
|
|
|
|
|
|
class Runner : public nsRunnable {
|
|
|
|
public:
|
2015-07-16 11:13:49 -07:00
|
|
|
explicit Runner(TaskQueue* aQueue)
|
2015-02-14 19:08:15 -08:00
|
|
|
: mQueue(aQueue)
|
|
|
|
{
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
NS_METHOD Run() override;
|
2015-02-14 19:08:15 -08:00
|
|
|
private:
|
2015-07-16 11:13:49 -07:00
|
|
|
RefPtr<TaskQueue> mQueue;
|
2015-02-14 19:08:15 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-07-16 11:13:49 -07:00
|
|
|
class FlushableTaskQueue : public TaskQueue
|
2015-02-14 19:08:15 -08:00
|
|
|
{
|
|
|
|
public:
|
2015-07-16 11:13:49 -07:00
|
|
|
explicit FlushableTaskQueue(already_AddRefed<SharedThreadPool> aPool) : TaskQueue(Move(aPool)) {}
|
2015-06-17 07:00:52 -07:00
|
|
|
nsresult FlushAndDispatch(already_AddRefed<nsIRunnable> aRunnable);
|
2015-02-14 19:08:15 -08:00
|
|
|
void Flush();
|
|
|
|
|
2015-04-14 17:47:22 -07:00
|
|
|
bool IsDispatchReliable() override { return false; }
|
|
|
|
|
2015-02-14 19:08:15 -08:00
|
|
|
private:
|
|
|
|
|
2014-08-14 17:05:00 -07:00
|
|
|
class MOZ_STACK_CLASS AutoSetFlushing
|
|
|
|
{
|
|
|
|
public:
|
2015-07-16 11:13:49 -07:00
|
|
|
explicit AutoSetFlushing(FlushableTaskQueue* aTaskQueue) : mTaskQueue(aTaskQueue)
|
2014-08-14 17:05:00 -07:00
|
|
|
{
|
|
|
|
mTaskQueue->mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
mTaskQueue->mIsFlushing = true;
|
|
|
|
}
|
|
|
|
~AutoSetFlushing()
|
|
|
|
{
|
|
|
|
mTaskQueue->mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
mTaskQueue->mIsFlushing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-07-16 11:13:49 -07:00
|
|
|
FlushableTaskQueue* mTaskQueue;
|
2014-08-14 17:05:00 -07:00
|
|
|
};
|
|
|
|
|
2015-02-14 19:08:15 -08:00
|
|
|
void FlushLocked();
|
2014-08-14 17:05:00 -07:00
|
|
|
|
2014-02-04 17:29:26 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-07-16 11:13:49 -07:00
|
|
|
#endif // TaskQueue_h_
|