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/. */
|
|
|
|
|
|
|
|
#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)
|
2014-08-15 04:05:00 +04:00
|
|
|
, mIsFlushing(false)
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MediaTaskQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaTaskQueue::~MediaTaskQueue()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mIsShutdown);
|
|
|
|
MOZ_COUNT_DTOR(MediaTaskQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-11 22:12:00 +04:00
|
|
|
MediaTaskQueue::Dispatch(TemporaryRef<nsIRunnable> aRunnable)
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2014-08-15 04:05:00 +04:00
|
|
|
return DispatchLocked(aRunnable, AbortIfFlushing);
|
|
|
|
}
|
|
|
|
|
2014-12-09 01:45:37 +03:00
|
|
|
nsresult
|
|
|
|
MediaTaskQueue::ForceDispatch(TemporaryRef<nsIRunnable> aRunnable)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return DispatchLocked(aRunnable, Forced);
|
|
|
|
}
|
|
|
|
|
2014-08-15 04:05:00 +04:00
|
|
|
nsresult
|
|
|
|
MediaTaskQueue::DispatchLocked(TemporaryRef<nsIRunnable> aRunnable,
|
|
|
|
DispatchMode aMode)
|
|
|
|
{
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
if (mIsFlushing && aMode == AbortIfFlushing) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2014-02-05 05:29:26 +04:00
|
|
|
if (mIsShutdown) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-12-09 01:45:37 +03:00
|
|
|
mTasks.push(TaskQueueEntry(aRunnable, aMode == Forced));
|
2014-02-05 05:29:26 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-30 10:53:34 +04:00
|
|
|
class MediaTaskQueueSyncRunnable : public nsRunnable {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit MediaTaskQueueSyncRunnable(TemporaryRef<nsIRunnable> aRunnable)
|
2014-07-30 10:53:34 +04:00
|
|
|
: mRunnable(aRunnable)
|
|
|
|
, mMonitor("MediaTaskQueueSyncRunnable")
|
|
|
|
, mDone(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsresult rv = mRunnable->Run();
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mDone = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WaitUntilDone() {
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
while (!mDone) {
|
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
RefPtr<nsIRunnable> mRunnable;
|
|
|
|
Monitor mMonitor;
|
|
|
|
bool mDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MediaTaskQueue::SyncDispatch(TemporaryRef<nsIRunnable> aRunnable) {
|
|
|
|
RefPtr<MediaTaskQueueSyncRunnable> task(new MediaTaskQueueSyncRunnable(aRunnable));
|
|
|
|
nsresult rv = Dispatch(task);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return task->WaitUntilDone();
|
|
|
|
}
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
void
|
|
|
|
MediaTaskQueue::AwaitIdle()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaTaskQueue::AwaitIdleLocked()
|
|
|
|
{
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mIsRunning || mTasks.empty());
|
|
|
|
while (mIsRunning) {
|
|
|
|
mQueueMonitor.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-02 08:51:02 +03:00
|
|
|
MediaTaskQueue::AwaitShutdownAndIdle()
|
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();
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:43:21 +03:00
|
|
|
nsRefPtr<ShutdownPromise>
|
2014-12-02 08:51:02 +03:00
|
|
|
MediaTaskQueue::BeginShutdown()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
mIsShutdown = true;
|
2014-12-09 22:43:21 +03:00
|
|
|
nsRefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
|
|
|
|
if (!mIsRunning) {
|
|
|
|
mShutdownPromise.Resolve(true, __func__);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-02-15 06:08:15 +03:00
|
|
|
void
|
|
|
|
FlushableMediaTaskQueue::Flush()
|
2014-08-15 04:05:00 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
AutoSetFlushing autoFlush(this);
|
2014-12-09 01:45:37 +03:00
|
|
|
FlushLocked();
|
2014-08-15 04:05:00 +04:00
|
|
|
AwaitIdleLocked();
|
|
|
|
}
|
|
|
|
|
2015-02-15 06:08:15 +03:00
|
|
|
nsresult
|
|
|
|
FlushableMediaTaskQueue::FlushAndDispatch(TemporaryRef<nsIRunnable> aRunnable)
|
2014-02-05 05:29:26 +04:00
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
2014-08-15 04:05:00 +04:00
|
|
|
AutoSetFlushing autoFlush(this);
|
2014-12-09 01:45:37 +03:00
|
|
|
FlushLocked();
|
2015-02-15 06:08:15 +03:00
|
|
|
nsresult rv = DispatchLocked(aRunnable, IgnoreFlushing);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-12-09 01:45:37 +03:00
|
|
|
AwaitIdleLocked();
|
2015-02-15 06:08:15 +03:00
|
|
|
return NS_OK;
|
2014-12-09 01:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-15 06:08:15 +03:00
|
|
|
FlushableMediaTaskQueue::FlushLocked()
|
2014-12-09 01:45:37 +03:00
|
|
|
{
|
|
|
|
mQueueMonitor.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mIsFlushing);
|
|
|
|
|
|
|
|
// Clear the tasks, but preserve those with mForceDispatch by re-appending
|
|
|
|
// them to the queue.
|
|
|
|
size_t numTasks = mTasks.size();
|
|
|
|
for (size_t i = 0; i < numTasks; ++i) {
|
|
|
|
if (mTasks.front().mForceDispatch) {
|
|
|
|
mTasks.push(mTasks.front());
|
|
|
|
}
|
2014-02-05 05:29:26 +04:00
|
|
|
mTasks.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaTaskQueue::IsEmpty()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return mTasks.empty();
|
|
|
|
}
|
|
|
|
|
2014-02-18 02:53:52 +04:00
|
|
|
bool
|
|
|
|
MediaTaskQueue::IsCurrentThreadIn()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
MonitorAutoLock mon(mQueueMonitor);
|
|
|
|
return NS_GetCurrentThread() == mRunningThread;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-05 05:29:26 +04:00
|
|
|
nsresult
|
|
|
|
MediaTaskQueue::Runner::Run()
|
|
|
|
{
|
|
|
|
RefPtr<nsIRunnable> event;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
|
|
|
MOZ_ASSERT(mQueue->mIsRunning);
|
2014-02-18 02:53:52 +04:00
|
|
|
mQueue->mRunningThread = NS_GetCurrentThread();
|
2014-02-05 05:29:26 +04:00
|
|
|
if (mQueue->mTasks.size() == 0) {
|
|
|
|
mQueue->mIsRunning = false;
|
2014-12-09 22:43:21 +03:00
|
|
|
mQueue->mShutdownPromise.ResolveIfExists(true, __func__);
|
2014-02-05 05:29:26 +04:00
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-12-09 01:45:37 +03:00
|
|
|
event = mQueue->mTasks.front().mRunnable;
|
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.
|
|
|
|
event->Run();
|
|
|
|
|
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;
|
2014-12-09 22:43:21 +03:00
|
|
|
mQueue->mShutdownPromise.ResolveIfExists(true, __func__);
|
2014-02-05 05:29:26 +04:00
|
|
|
mon.NotifyAll();
|
2014-02-18 02:53:52 +04:00
|
|
|
mQueue->mRunningThread = nullptr;
|
2014-02-05 05:29:26 +04: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-18 02:53:52 +04:00
|
|
|
{
|
2014-02-05 05:29:26 +04:00
|
|
|
MonitorAutoLock mon(mQueue->mQueueMonitor);
|
2014-02-18 02:53:52 +04: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-05 05:29:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|