gecko-dev/dom/media/MediaTaskQueue.h
Nathan Froyd 974d8120f2 Bug 1161627 - part 2 - machine-convert TemporaryRef<T> to already_AddRefed<T>; r=ehsan
This conversion was done with the script:

  find . -name '*.cpp' -o -name '*.h' -o -name '*.mm' -o -name '*.idl' | \
    egrep -v 'cairo-win32-refptr.h|RefPtr.h|TestRefPtr.cpp' | \
    xargs sed -i -e 's/mozilla::TemporaryRef</already_AddRefed</g' \
                 -e 's/TemporaryRef</already_AddRefed</g'

Manual fixups were performed in the following instances:

- We handled mfbt/RefPtr.h manually so as to not convert TemporaryRef itself
  into already_AddRefed.

- The following files had explicit Move() calls added to make up for the lack
  of a copy constructor on already_AddRefed:

  dom/base/ImageEncoder.cpp
  dom/media/MediaTaskQueue.{h,cpp}
  dom/media/webaudio/PannerNode.cpp

- A redundant overload for MediaTaskQueue::Dispatch was deleted.

- A few manual fixups were required in mfbt/tests/TestRefPtr.cpp.

- Comments, using declarations, and forward declarations relating to
  TemporaryRef in dom/canvas/ and gfx/layers/ were changed to refer to
  already_AddRefed.
2015-06-17 10:00:52 -04:00

212 lines
6.4 KiB
C++

/* -*- 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/. */
#ifndef MediaTaskQueue_h_
#define MediaTaskQueue_h_
#include <queue>
#include "mozilla/RefPtr.h"
#include "mozilla/Monitor.h"
#include "mozilla/unused.h"
#include "SharedThreadPool.h"
#include "nsThreadUtils.h"
#include "MediaPromise.h"
#include "TaskDispatcher.h"
class nsIRunnable;
namespace mozilla {
class SharedThreadPool;
typedef MediaPromise<bool, bool, false> ShutdownPromise;
// Abstracts executing runnables in order in a thread pool. The runnables
// dispatched to the MediaTaskQueue will be executed in the order in which
// 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.
class MediaTaskQueue : public AbstractThread {
public:
explicit MediaTaskQueue(already_AddRefed<SharedThreadPool> aPool, bool aSupportsTailDispatch = false);
TaskDispatcher& TailDispatcher() override;
MediaTaskQueue* AsTaskQueue() override { return this; }
void Dispatch(already_AddRefed<nsIRunnable> aRunnable,
DispatchFailureHandling aFailureHandling = AssertDispatchSuccess,
DispatchReason aReason = NormalDispatch) override
{
MonitorAutoLock mon(mQueueMonitor);
nsresult rv = DispatchLocked(Move(aRunnable), AbortIfFlushing, aFailureHandling, aReason);
MOZ_DIAGNOSTIC_ASSERT(aFailureHandling == DontAssertDispatchSuccess || NS_SUCCEEDED(rv));
unused << rv;
}
// DEPRECATED! Do not us, if a flush happens at the same time, this function
// can hang and block forever!
void SyncDispatch(already_AddRefed<nsIRunnable> aRunnable);
// 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.
//
// The returned promise is resolved when the queue goes empty.
nsRefPtr<ShutdownPromise> BeginShutdown();
// Blocks until all task finish executing.
void AwaitIdle();
// Blocks until the queue is flagged for shutdown and all tasks have finished
// executing.
void AwaitShutdownAndIdle();
bool IsEmpty();
// Returns true if the current thread is currently running a Runnable in
// the task queue.
bool IsCurrentThreadIn() override;
protected:
virtual ~MediaTaskQueue();
// 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();
enum DispatchMode { AbortIfFlushing, IgnoreFlushing };
nsresult DispatchLocked(already_AddRefed<nsIRunnable> aRunnable, DispatchMode aMode,
DispatchFailureHandling aFailureHandling,
DispatchReason aReason = NormalDispatch);
void MaybeResolveShutdown()
{
mQueueMonitor.AssertCurrentThreadOwns();
if (mIsShutdown && !mIsRunning) {
mShutdownPromise.ResolveIfExists(true, __func__);
mPool = nullptr;
}
}
RefPtr<SharedThreadPool> mPool;
// Monitor that protects the queue and mIsRunning;
Monitor mQueueMonitor;
// Queue of tasks to run.
std::queue<nsCOMPtr<nsIRunnable>> mTasks;
// 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.
//
// 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;
// RAII class that gets instantiated for each dispatched task.
class AutoTaskGuard : public AutoTaskDispatcher
{
public:
explicit AutoTaskGuard(MediaTaskQueue* aQueue)
: AutoTaskDispatcher(/* aIsTailDispatcher = */ true), mQueue(aQueue)
{
// NB: We don't hold the lock to aQueue here. Don't do anything that
// might require it.
MOZ_ASSERT(!mQueue->mTailDispatcher);
mQueue->mTailDispatcher = this;
MOZ_ASSERT(sCurrentThreadTLS.get() == nullptr);
sCurrentThreadTLS.set(aQueue);
MOZ_ASSERT(mQueue->mRunningThread == nullptr);
mQueue->mRunningThread = NS_GetCurrentThread();
}
~AutoTaskGuard()
{
DrainDirectTasks();
MOZ_ASSERT(mQueue->mRunningThread == NS_GetCurrentThread());
mQueue->mRunningThread = nullptr;
sCurrentThreadTLS.set(nullptr);
mQueue->mTailDispatcher = nullptr;
}
private:
MediaTaskQueue* mQueue;
};
TaskDispatcher* mTailDispatcher;
// 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;
MediaPromiseHolder<ShutdownPromise> mShutdownPromise;
// True if we're flushing; we reject new tasks if we're flushing.
bool mIsFlushing;
class Runner : public nsRunnable {
public:
explicit Runner(MediaTaskQueue* aQueue)
: mQueue(aQueue)
{
}
NS_METHOD Run() override;
private:
RefPtr<MediaTaskQueue> mQueue;
};
};
class FlushableMediaTaskQueue : public MediaTaskQueue
{
public:
explicit FlushableMediaTaskQueue(already_AddRefed<SharedThreadPool> aPool) : MediaTaskQueue(Move(aPool)) {}
nsresult FlushAndDispatch(already_AddRefed<nsIRunnable> aRunnable);
void Flush();
bool IsDispatchReliable() override { return false; }
private:
class MOZ_STACK_CLASS AutoSetFlushing
{
public:
explicit AutoSetFlushing(FlushableMediaTaskQueue* aTaskQueue) : mTaskQueue(aTaskQueue)
{
mTaskQueue->mQueueMonitor.AssertCurrentThreadOwns();
mTaskQueue->mIsFlushing = true;
}
~AutoSetFlushing()
{
mTaskQueue->mQueueMonitor.AssertCurrentThreadOwns();
mTaskQueue->mIsFlushing = false;
}
private:
FlushableMediaTaskQueue* mTaskQueue;
};
void FlushLocked();
};
} // namespace mozilla
#endif // MediaTaskQueue_h_