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/. */
|
|
|
|
|
|
|
|
#include "MediaTaskQueue.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "SharedThreadPool.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
MediaTaskQueue::MediaTaskQueue(TemporaryRef<SharedThreadPool> aPool)
|
|
|
|
: mPool(aPool)
|
|
|
|
, mQueueMonitor("MediaTaskQueue::Queue")
|
|
|
|
, mIsRunning(false)
|
|
|
|
, mIsShutdown(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MediaTaskQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaTaskQueue::~MediaTaskQueue()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mIsShutdown);
|
|
|
|
MOZ_COUNT_DTOR(MediaTaskQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaTaskQueue::Dispatch(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
if (mIsShutdown) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mTasks.push(aRunnable);
|
|
|
|
if (mIsRunning) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
RefPtr<nsIRunnable> runner(new Runner(this));
|
|
|
|
nsresult rv = mPool->Dispatch(runner, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to dispatch runnable to run MediaTaskQueue");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mIsRunning = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTaskQueue::AwaitIdle()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTaskQueue::AwaitIdleLocked()
|
|
|
|
{
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mIsRunning || mTasks.empty());
|
|
|
|
while (mIsRunning) {
|
|
|
|
mQueueMonitor.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTaskQueue::Shutdown()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
mIsShutdown = true;
|
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTaskQueue::Flush()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
while (!mTasks.empty()) {
|
|
|
|
mTasks.pop();
|
|
|
|
}
|
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaTaskQueue::IsEmpty()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return mTasks.empty();
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:53:52 -08:00
|
|
|
bool
|
|
|
|
MediaTaskQueue::IsCurrentThreadIn()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return NS_GetCurrentThread() == mRunningThread;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-04 17:29:26 -08:00
|
|
|
nsresult
|
|
|
|
MediaTaskQueue::Runner::Run()
|
|
|
|
{
|
|
|
|
RefPtr<nsIRunnable> event;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mQueue->mIsRunning);
|
2014-02-17 14:53:52 -08:00
|
|
|
mQueue->mRunningThread = NS_GetCurrentThread();
|
2014-02-04 17:29:26 -08:00
|
|
|
if (mQueue->mTasks.size() == 0) {
|
|
|
|
mQueue->mIsRunning = false;
|
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
event = mQueue->mTasks.front();
|
|
|
|
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.
|
|
|
|
event->Run();
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
if (mQueue->mTasks.size() == 0) {
|
|
|
|
// No more events to run. Exit the task runner.
|
|
|
|
mQueue->mIsRunning = false;
|
|
|
|
mon.NotifyAll();
|
2014-02-17 14:53:52 -08:00
|
|
|
mQueue->mRunningThread = nullptr;
|
2014-02-04 17:29:26 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's at least one more event that we can run. Dispatch this Runner
|
|
|
|
// to the thread pool again to ensure it runs again. Note that we don't just
|
|
|
|
// run in a loop here so that we don't hog the thread pool. This means we may
|
|
|
|
// run on another thread next time, but we rely on the memory fences from
|
|
|
|
// mQueueMonitor for thread safety of non-threadsafe tasks.
|
2014-02-17 14:53:52 -08:00
|
|
|
{
|
2014-02-04 17:29:26 -08:00
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
2014-02-17 14:53:52 -08:00
|
|
|
// Note: Hold the monitor *before* we dispatch, in case we context switch
|
|
|
|
// to another thread pool in the queue immediately and take the lock in the
|
|
|
|
// other thread; mRunningThread could be set to the new thread's value and
|
|
|
|
// then incorrectly anulled below in that case.
|
|
|
|
nsresult rv = mQueue->mPool->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Failed to dispatch, shutdown!
|
|
|
|
mQueue->mIsRunning = false;
|
|
|
|
mQueue->mIsShutdown = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
mQueue->mRunningThread = nullptr;
|
2014-02-04 17:29:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|