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:52:43 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
#include "nsIEventTarget.h"
|
2014-02-05 05:29:26 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
class TaskQueue::EventTargetWrapper final : public nsIEventTarget
|
|
|
|
{
|
|
|
|
RefPtr<TaskQueue> mTaskQueue;
|
|
|
|
|
|
|
|
~EventTargetWrapper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit EventTargetWrapper(TaskQueue* aTaskQueue)
|
|
|
|
: mTaskQueue(aTaskQueue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTaskQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
DispatchFromScript(nsIRunnable* aEvent, uint32_t aFlags) override
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> ref = aEvent;
|
|
|
|
return Dispatch(ref.forget(), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) override
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = aEvent;
|
|
|
|
MonitorAutoLock mon(mTaskQueue->mQueueMonitor);
|
|
|
|
return mTaskQueue->DispatchLocked(/* passed by ref */runnable,
|
|
|
|
DontAssertDispatchSuccess,
|
|
|
|
NormalDispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t aFlags) override
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
IsOnCurrentThread(bool* aResult) override
|
|
|
|
{
|
|
|
|
*aResult = mTaskQueue->IsCurrentThreadIn();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(TaskQueue::EventTargetWrapper, nsIEventTarget)
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
|
|
|
|
bool aRequireTailDispatch)
|
2015-04-14 09:53:07 +03:00
|
|
|
: AbstractThread(aRequireTailDispatch)
|
2016-09-14 06:12:15 +03:00
|
|
|
, mTarget(aTarget)
|
2015-07-16 21:13:49 +03:00
|
|
|
, mQueueMonitor("TaskQueue::Queue")
|
2015-04-06 21:24:16 +03:00
|
|
|
, mTailDispatcher(nullptr)
|
2014-02-05 05:29:26 +04:00
|
|
|
, mIsRunning(false)
|
|
|
|
, mIsShutdown(false)
|
|
|
|
{
|
2015-07-16 21:13:49 +03:00
|
|
|
MOZ_COUNT_CTOR(TaskQueue);
|
2014-02-05 05:29:26 +04:00
|
|
|
}
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::~TaskQueue()
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mIsShutdown);
|
2015-07-16 21:13:49 +03:00
|
|
|
MOZ_COUNT_DTOR(TaskQueue);
|
2014-02-05 05:29:26 +04:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:24:16 +03:00
|
|
|
TaskDispatcher&
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::TailDispatcher()
|
2015-04-06 21:24:16 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(mTailDispatcher);
|
|
|
|
return *mTailDispatcher;
|
|
|
|
}
|
|
|
|
|
2016-03-18 06:27:15 +03:00
|
|
|
// Note aRunnable is passed by ref to support conditional ownership transfer.
|
|
|
|
// See Dispatch() in TaskQueue.h for more details.
|
2014-08-15 04:05:00 +04:00
|
|
|
nsresult
|
2016-03-18 06:27:15 +03:00
|
|
|
TaskQueue::DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
|
2016-11-10 10:34:35 +03:00
|
|
|
DispatchFailureHandling aFailureHandling,
|
2016-03-18 06:27:15 +03:00
|
|
|
DispatchReason aReason)
|
2014-08-15 04:05:00 +04:00
|
|
|
{
|
2016-11-10 10:45:45 +03:00
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mIsShutdown) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-04-15 20:54:25 +03:00
|
|
|
AbstractThread* currentThread;
|
2015-05-14 04:47:42 +03:00
|
|
|
if (aReason != TailDispatch && (currentThread = GetCurrent()) && RequiresTailDispatch(currentThread)) {
|
2016-03-18 06:27:15 +03:00
|
|
|
currentThread->TailDispatcher().AddTask(this, aRunnable.forget(), aFailureHandling);
|
2015-04-15 20:54:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-18 06:27:15 +03:00
|
|
|
mTasks.push(aRunnable.forget());
|
2014-02-05 05:29:26 +04:00
|
|
|
if (mIsRunning) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> runner(new Runner(this));
|
2016-09-14 06:12:15 +03:00
|
|
|
nsresult rv = mTarget->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
|
2014-02-05 05:29:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-07-16 21:13:49 +03:00
|
|
|
NS_WARNING("Failed to dispatch runnable to run TaskQueue");
|
2014-02-05 05:29:26 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mIsRunning = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::AwaitIdle()
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::AwaitIdleLocked()
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
2015-04-29 01:15:49 +03:00
|
|
|
// Make sure there are no tasks for this queue waiting in the caller's tail
|
2015-04-16 19:20:22 +03:00
|
|
|
// dispatcher.
|
|
|
|
MOZ_ASSERT_IF(AbstractThread::GetCurrent(),
|
2016-09-21 12:24:43 +03:00
|
|
|
!AbstractThread::GetCurrent()->HasTailTasksFor(this));
|
2015-04-16 19:20:22 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mIsRunning || mTasks.empty());
|
|
|
|
while (mIsRunning) {
|
|
|
|
mQueueMonitor.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::AwaitShutdownAndIdle()
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
2016-05-12 07:53:19 +03:00
|
|
|
MOZ_ASSERT(!IsCurrentThreadIn());
|
2015-04-29 01:15:49 +03:00
|
|
|
// Make sure there are no tasks for this queue waiting in the caller's tail
|
2015-04-16 19:20:22 +03:00
|
|
|
// dispatcher.
|
|
|
|
MOZ_ASSERT_IF(AbstractThread::GetCurrent(),
|
2016-09-21 12:24:43 +03:00
|
|
|
!AbstractThread::GetCurrent()->HasTailTasksFor(this));
|
2015-04-16 19:20:22 +03:00
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2014-12-02 08:51:02 +03:00
|
|
|
while (!mIsShutdown) {
|
|
|
|
mQueueMonitor.Wait();
|
|
|
|
}
|
2014-02-05 05:29:26 +04:00
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ShutdownPromise>
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::BeginShutdown()
|
2014-12-02 08:51:02 +03:00
|
|
|
{
|
2015-06-16 02:19:32 +03:00
|
|
|
// Dispatch any tasks for this queue waiting in the caller's tail dispatcher,
|
|
|
|
// since this is the last opportunity to do so.
|
|
|
|
if (AbstractThread* currentThread = AbstractThread::GetCurrent()) {
|
2016-09-21 12:24:43 +03:00
|
|
|
currentThread->TailDispatchTasksFor(this);
|
2015-06-16 02:19:32 +03:00
|
|
|
}
|
2015-04-29 01:15:49 +03:00
|
|
|
|
2014-12-02 08:51:02 +03:00
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
mIsShutdown = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
|
2015-06-11 06:06:09 +03:00
|
|
|
MaybeResolveShutdown();
|
2014-12-02 08:51:02 +03:00
|
|
|
mon.NotifyAll();
|
2014-12-09 22:43:21 +03:00
|
|
|
return p;
|
2014-12-02 08:51:02 +03:00
|
|
|
}
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
bool
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::IsEmpty()
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return mTasks.empty();
|
|
|
|
}
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
uint32_t
|
|
|
|
TaskQueue::ImpreciseLengthForHeuristics()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return mTasks.size();
|
|
|
|
}
|
|
|
|
|
2014-02-18 02:53:52 +04:00
|
|
|
bool
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::IsCurrentThreadIn()
|
2014-02-18 02:53:52 +04:00
|
|
|
{
|
2015-04-07 21:26:35 +03:00
|
|
|
bool in = NS_GetCurrentThread() == mRunningThread;
|
|
|
|
return in;
|
2014-02-18 02:53:52 +04:00
|
|
|
}
|
|
|
|
|
2016-09-14 06:12:15 +03:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
TaskQueue::WrapAsEventTarget()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEventTarget> ref = new EventTargetWrapper(this);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
nsresult
|
2015-07-16 21:13:49 +03:00
|
|
|
TaskQueue::Runner::Run()
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> event;
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mQueue->mIsRunning);
|
|
|
|
if (mQueue->mTasks.size() == 0) {
|
|
|
|
mQueue->mIsRunning = false;
|
2015-06-11 06:06:09 +03:00
|
|
|
mQueue->MaybeResolveShutdown();
|
2014-02-05 05:29:26 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-09-08 03:02:16 +03:00
|
|
|
event = mQueue->mTasks.front().forget();
|
2014-02-05 05:29:26 +04:00
|
|
|
mQueue->mTasks.pop();
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(event);
|
|
|
|
|
|
|
|
// Note that dropping the queue monitor before running the task, and
|
|
|
|
// taking the monitor again after the task has run ensures we have memory
|
|
|
|
// fences enforced. This means that if the object we're calling wasn't
|
|
|
|
// designed to be threadsafe, it will be, provided we're only calling it
|
|
|
|
// in this task queue.
|
2015-04-07 21:26:35 +03:00
|
|
|
{
|
|
|
|
AutoTaskGuard g(mQueue);
|
|
|
|
event->Run();
|
|
|
|
}
|
2014-02-05 05:29:26 +04:00
|
|
|
|
2014-02-18 02:53:52 +04:00
|
|
|
// Drop the reference to event. The event will hold a reference to the
|
|
|
|
// object it's calling, and we don't want to keep it alive, it may be
|
|
|
|
// making assumptions what holds references to it. This is especially
|
|
|
|
// the case if the object is waiting for us to shutdown, so that it
|
|
|
|
// can shutdown (like in the MediaDecoderStateMachine's SHUTDOWN case).
|
|
|
|
event = nullptr;
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
if (mQueue->mTasks.size() == 0) {
|
|
|
|
// No more events to run. Exit the task runner.
|
|
|
|
mQueue->mIsRunning = false;
|
2015-06-11 06:06:09 +03:00
|
|
|
mQueue->MaybeResolveShutdown();
|
2014-02-05 05:29:26 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's at least one more event that we can run. Dispatch this Runner
|
2016-09-14 06:12:15 +03:00
|
|
|
// to the target again to ensure it runs again. Note that we don't just
|
|
|
|
// run in a loop here so that we don't hog the target. This means we may
|
2014-02-05 05:29:26 +04:00
|
|
|
// run on another thread next time, but we rely on the memory fences from
|
|
|
|
// mQueueMonitor for thread safety of non-threadsafe tasks.
|
2016-09-14 06:12:15 +03:00
|
|
|
nsresult rv = mQueue->mTarget->Dispatch(this, NS_DISPATCH_AT_END);
|
2015-04-10 21:52:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Failed to dispatch, shutdown!
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
2015-04-10 21:52:08 +03:00
|
|
|
mQueue->mIsRunning = false;
|
|
|
|
mQueue->mIsShutdown = true;
|
2015-06-11 06:06:09 +03:00
|
|
|
mQueue->MaybeResolveShutdown();
|
2015-04-10 21:52:08 +03:00
|
|
|
mon.NotifyAll();
|
2014-02-05 05:29:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|