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"
|
2015-04-15 03:25:14 +03:00
|
|
|
#include "mozilla/unused.h"
|
2015-07-16 21:52:43 +03:00
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
2015-08-03 21:44:10 +03:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
2014-02-05 05:29:26 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
class nsIRunnable;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class SharedThreadPool;
|
|
|
|
|
2015-07-16 21:06:49 +03:00
|
|
|
typedef MozPromise<bool, bool, false> ShutdownPromise;
|
2014-12-09 22:43:21 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
// Abstracts executing runnables in order in a thread pool. The runnables
|
2015-07-16 21:13:49 +03:00
|
|
|
// 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.
|
2015-07-16 21:13:49 +03:00
|
|
|
class TaskQueue : public AbstractThread {
|
2014-02-05 05:29:26 +04:00
|
|
|
public:
|
2015-07-16 21:13:49 +03:00
|
|
|
explicit TaskQueue(already_AddRefed<SharedThreadPool> aPool, bool aSupportsTailDispatch = false);
|
2014-02-05 05:29:26 +04:00
|
|
|
|
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
|
|
|
|
2015-04-15 03:25:14 +03:00
|
|
|
void Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
2015-04-14 20:58:49 +03:00
|
|
|
DispatchFailureHandling aFailureHandling = AssertDispatchSuccess,
|
|
|
|
DispatchReason aReason = NormalDispatch) override
|
2015-04-02 23:39:34 +03:00
|
|
|
{
|
2015-04-15 03:25:14 +03:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2015-04-15 20:54:25 +03:00
|
|
|
nsresult rv = DispatchLocked(Move(aRunnable), AbortIfFlushing, aFailureHandling, aReason);
|
2015-04-15 03:25:14 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aFailureHandling == DontAssertDispatchSuccess || NS_SUCCEEDED(rv));
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << rv;
|
2015-04-02 23:39:34 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// by the threadpool 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();
|
|
|
|
|
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
|
|
|
|
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();
|
|
|
|
|
2015-04-15 03:47:22 +03:00
|
|
|
enum DispatchMode { AbortIfFlushing, IgnoreFlushing };
|
2014-08-15 04:05:00 +04:00
|
|
|
|
2015-04-14 20:58:49 +03:00
|
|
|
nsresult DispatchLocked(already_AddRefed<nsIRunnable> aRunnable, DispatchMode aMode,
|
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__);
|
2015-06-11 06:06:54 +03:00
|
|
|
mPool = nullptr;
|
2015-06-11 06:06:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedThreadPool> mPool;
|
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.
|
|
|
|
Atomic<nsIThread*> 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)
|
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;
|
|
|
|
|
2015-04-14 09:53:07 +03:00
|
|
|
MOZ_ASSERT(sCurrentThreadTLS.get() == nullptr);
|
|
|
|
sCurrentThreadTLS.set(aQueue);
|
2015-04-06 21:24:16 +03:00
|
|
|
|
2015-04-10 21:50:33 +03:00
|
|
|
MOZ_ASSERT(mQueue->mRunningThread == nullptr);
|
|
|
|
mQueue->mRunningThread = NS_GetCurrentThread();
|
2015-04-07 21:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoTaskGuard()
|
|
|
|
{
|
2015-04-16 22:24:54 +03:00
|
|
|
DrainDirectTasks();
|
|
|
|
|
2015-04-10 21:50:33 +03:00
|
|
|
MOZ_ASSERT(mQueue->mRunningThread == NS_GetCurrentThread());
|
|
|
|
mQueue->mRunningThread = nullptr;
|
|
|
|
|
2015-04-14 09:53:07 +03:00
|
|
|
sCurrentThreadTLS.set(nullptr);
|
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;
|
2015-04-07 21:26:35 +03:00
|
|
|
};
|
|
|
|
|
2015-04-06 21:24:16 +03:00
|
|
|
TaskDispatcher* mTailDispatcher;
|
|
|
|
|
2014-02-05 05:29:26 +04: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 21:06:49 +03:00
|
|
|
MozPromiseHolder<ShutdownPromise> mShutdownPromise;
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2015-02-15 06:08:15 +03:00
|
|
|
// True if we're flushing; we reject new tasks if we're flushing.
|
|
|
|
bool mIsFlushing;
|
|
|
|
|
|
|
|
class Runner : public nsRunnable {
|
|
|
|
public:
|
2015-07-16 21:13:49 +03:00
|
|
|
explicit Runner(TaskQueue* aQueue)
|
2015-02-15 06:08:15 +03:00
|
|
|
: mQueue(aQueue)
|
|
|
|
{
|
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_METHOD 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_
|