2015-03-14 05:41:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#if !defined(AbstractThread_h_)
|
|
|
|
#define AbstractThread_h_
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsRefPtr.h"
|
|
|
|
|
2015-04-14 09:53:07 +03:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-04-20 15:29:35 +03:00
|
|
|
class MediaTaskQueue;
|
2015-04-14 09:53:07 +03:00
|
|
|
class TaskDispatcher;
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
/*
|
|
|
|
* We often want to run tasks on a target that guarantees that events will never
|
|
|
|
* run in parallel. There are various target types that achieve this - namely
|
|
|
|
* nsIThread and MediaTaskQueue. Note that nsIThreadPool (which implements
|
|
|
|
* nsIEventTarget) does not have this property, so we do not want to use
|
|
|
|
* nsIEventTarget for this purpose. This class encapsulates the specifics of
|
|
|
|
* the structures we might use here and provides a consistent interface.
|
|
|
|
*
|
2015-04-10 21:21:47 +03:00
|
|
|
* At present, the supported AbstractThread implementations are MediaTaskQueue
|
|
|
|
* and AbstractThread::MainThread. If you add support for another thread that is
|
|
|
|
* not the MainThread, you'll need to figure out how to make it unique such that
|
|
|
|
* comparing AbstractThread pointers is equivalent to comparing nsIThread pointers.
|
2015-03-14 05:41:12 +03:00
|
|
|
*/
|
|
|
|
class AbstractThread
|
|
|
|
{
|
|
|
|
public:
|
2015-04-14 09:53:07 +03:00
|
|
|
// Returns the AbstractThread that the caller is currently running in, or null
|
|
|
|
// if the caller is not running in an AbstractThread.
|
|
|
|
static AbstractThread* GetCurrent() { return sCurrentThreadTLS.get(); }
|
|
|
|
|
|
|
|
AbstractThread(bool aRequireTailDispatch) : mRequireTailDispatch(aRequireTailDispatch) {}
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AbstractThread);
|
2015-04-15 03:25:14 +03:00
|
|
|
|
|
|
|
enum DispatchFailureHandling { AssertDispatchSuccess, DontAssertDispatchSuccess };
|
2015-04-14 20:58:49 +03:00
|
|
|
enum DispatchReason { NormalDispatch, TailDispatch };
|
2015-04-15 03:25:14 +03:00
|
|
|
virtual void Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
2015-04-14 20:58:49 +03:00
|
|
|
DispatchFailureHandling aHandling = AssertDispatchSuccess,
|
|
|
|
DispatchReason aReason = NormalDispatch) = 0;
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
virtual bool IsCurrentThreadIn() = 0;
|
|
|
|
|
2015-04-15 03:47:22 +03:00
|
|
|
// Returns true if dispatch is generally reliable. This is used to guard
|
|
|
|
// against FlushableMediaTaskQueues, which should go away.
|
|
|
|
virtual bool IsDispatchReliable() { return true; }
|
|
|
|
|
2015-04-14 09:53:07 +03:00
|
|
|
// Returns a TaskDispatcher that will dispatch its tasks when the currently-
|
|
|
|
// running tasks pops off the stack.
|
|
|
|
//
|
|
|
|
// May only be called when running within the it is invoked up, and only on
|
|
|
|
// threads which support it.
|
|
|
|
virtual TaskDispatcher& TailDispatcher() = 0;
|
|
|
|
|
|
|
|
// Returns true if this task queue requires all dispatches performed by its
|
|
|
|
// tasks to go through the tail dispatcher.
|
|
|
|
bool RequiresTailDispatch() const { return mRequireTailDispatch; }
|
|
|
|
|
2015-04-20 15:29:35 +03:00
|
|
|
virtual MediaTaskQueue* AsTaskQueue() { MOZ_CRASH("Not a task queue!"); }
|
|
|
|
virtual nsIThread* AsXPCOMThread() { MOZ_CRASH("Not an XPCOM thread!"); }
|
|
|
|
|
2015-04-02 23:21:53 +03:00
|
|
|
// Convenience method for getting an AbstractThread for the main thread.
|
|
|
|
static AbstractThread* MainThread();
|
2015-04-07 20:59:44 +03:00
|
|
|
|
|
|
|
// Must be called exactly once during startup.
|
|
|
|
static void InitStatics();
|
2015-04-02 23:21:53 +03:00
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
protected:
|
|
|
|
virtual ~AbstractThread() {}
|
2015-04-14 09:53:07 +03:00
|
|
|
static ThreadLocal<AbstractThread*> sCurrentThreadTLS;
|
|
|
|
|
|
|
|
// True if we want to require that every task dispatched from tasks running in
|
|
|
|
// this queue go through our queue's tail dispatcher.
|
|
|
|
const bool mRequireTailDispatch;
|
2015-03-14 05:41:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|