2016-05-25 00:45:44 +03: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: */
|
2015-03-14 05:41:12 +03:00
|
|
|
/* 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_
|
|
|
|
|
2017-06-09 23:19:13 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/ThreadLocal.h"
|
2015-03-14 05:41:12 +03:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsIRunnable.h"
|
2017-06-09 23:19:13 +03:00
|
|
|
#include "nsISerialEventTarget.h"
|
2015-03-14 05:41:12 +03:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsIThread.h"
|
2015-04-14 09:53:07 +03:00
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
class TaskQueue;
|
2015-04-14 09:53:07 +03:00
|
|
|
class TaskDispatcher;
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
/*
|
2017-06-10 07:24:46 +03:00
|
|
|
* NOTE: PLEASE AVOID USE OF AbstractThread OUTSIDE MEDIA CODE WHEN POSSIBLE.
|
|
|
|
* The nsISerialEventTarget interface should be preferred. AbstractThread
|
|
|
|
* has unusual "tail dispatch" semantics that usually are not needed outside
|
|
|
|
* of media code.
|
|
|
|
*
|
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
|
2015-07-16 21:13:49 +03:00
|
|
|
* nsIThread and TaskQueue. Note that nsIThreadPool (which implements
|
2015-03-14 05:41:12 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2016-11-29 08:01:18 +03:00
|
|
|
* At present, the supported AbstractThread implementations are TaskQueue,
|
|
|
|
* AbstractThread::MainThread() and DocGroup::AbstractThreadFor().
|
|
|
|
* 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
|
|
|
*/
|
2017-06-09 23:19:13 +03:00
|
|
|
class AbstractThread : public nsISerialEventTarget
|
2015-03-14 05:41:12 +03:00
|
|
|
{
|
|
|
|
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(); }
|
|
|
|
|
2015-05-14 04:47:42 +03:00
|
|
|
AbstractThread(bool aSupportsTailDispatch) : mSupportsTailDispatch(aSupportsTailDispatch) {}
|
2015-04-14 09:53:07 +03:00
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
// Returns an AbstractThread wrapper of a nsIThread.
|
2016-04-12 07:12:22 +03:00
|
|
|
static already_AddRefed<AbstractThread>
|
|
|
|
CreateXPCOMThreadWrapper(nsIThread* aThread, bool aRequireTailDispatch);
|
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
// Returns an AbstractThread wrapper of a non-nsIThread EventTarget on the main thread.
|
|
|
|
static already_AddRefed<AbstractThread>
|
|
|
|
CreateEventTargetWrapper(nsIEventTarget* aEventTarget, bool aRequireTailDispatch);
|
|
|
|
|
2017-06-09 23:19:13 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
// We don't use NS_DECL_NSIEVENTTARGET so that we can remove the default
|
|
|
|
// |flags| parameter from Dispatch. Otherwise, a single-argument Dispatch call
|
|
|
|
// would be ambiguous.
|
|
|
|
NS_IMETHOD_(bool) IsOnCurrentThreadInfallible(void) override;
|
|
|
|
NS_IMETHOD IsOnCurrentThread(bool *_retval) override;
|
|
|
|
NS_IMETHOD Dispatch(already_AddRefed<nsIRunnable> event, uint32_t flags) override;
|
|
|
|
NS_IMETHOD DispatchFromScript(nsIRunnable *event, uint32_t flags) override;
|
|
|
|
NS_IMETHOD DelayedDispatch(already_AddRefed<nsIRunnable> event, uint32_t delay) override;
|
2015-04-15 03:25:14 +03:00
|
|
|
|
2017-11-15 10:49:04 +03:00
|
|
|
enum DispatchFailureHandling { AssertDispatchSuccess, DontAssertDispatchSuccess };
|
2015-04-14 20:58:49 +03:00
|
|
|
enum DispatchReason { NormalDispatch, TailDispatch };
|
2017-11-09 03:43:40 +03:00
|
|
|
virtual nsresult Dispatch(already_AddRefed<nsIRunnable> aRunnable,
|
2017-11-15 10:49:04 +03:00
|
|
|
DispatchFailureHandling aHandling = AssertDispatchSuccess,
|
2017-11-09 03:43:40 +03:00
|
|
|
DispatchReason aReason = NormalDispatch) = 0;
|
2015-04-14 20:58:49 +03:00
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
virtual bool IsCurrentThreadIn() = 0;
|
|
|
|
|
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;
|
|
|
|
|
2016-09-21 12:24:43 +03:00
|
|
|
// Returns true if we have tail tasks scheduled, or if this isn't known.
|
|
|
|
// Returns false if we definitely don't have any tail tasks.
|
|
|
|
virtual bool MightHaveTailTasks() { return true; }
|
|
|
|
|
|
|
|
// Helper functions for methods on the tail TasklDispatcher. These check
|
|
|
|
// HasTailTasks to avoid allocating a TailDispatcher if it isn't
|
|
|
|
// needed.
|
2017-11-15 10:49:04 +03:00
|
|
|
void TailDispatchTasksFor(AbstractThread* aThread);
|
2016-09-21 12:24:43 +03:00
|
|
|
bool HasTailTasksFor(AbstractThread* aThread);
|
|
|
|
|
2015-05-14 04:47:42 +03:00
|
|
|
// Returns true if this supports the tail dispatcher.
|
|
|
|
bool SupportsTailDispatch() const { return mSupportsTailDispatch; }
|
|
|
|
|
|
|
|
// Returns true if this thread requires all dispatches originating from
|
|
|
|
// aThread go through the tail dispatcher.
|
|
|
|
bool RequiresTailDispatch(AbstractThread* aThread) const;
|
2016-05-04 11:24:25 +03:00
|
|
|
bool RequiresTailDispatchFromCurrentThread() const;
|
2015-04-14 09:53:07 +03:00
|
|
|
|
2015-07-16 21:13:49 +03:00
|
|
|
virtual TaskQueue* AsTaskQueue() { MOZ_CRASH("Not a task queue!"); }
|
2016-11-29 08:01:18 +03:00
|
|
|
virtual nsIEventTarget* AsEventTarget() { MOZ_CRASH("Not an event target!"); }
|
2015-04-20 15:29:35 +03:00
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
// Returns the non-DocGroup version of AbstractThread on the main thread.
|
|
|
|
// A DocGroup-versioned one is available in DispatcherTrait::AbstractThreadFor().
|
|
|
|
// Note: DispatcherTrait::AbstractThreadFor() SHALL be used when possible.
|
2015-04-02 23:21:53 +03:00
|
|
|
static AbstractThread* MainThread();
|
2015-04-07 20:59:44 +03:00
|
|
|
|
|
|
|
// Must be called exactly once during startup.
|
2017-04-19 08:24:09 +03:00
|
|
|
static void InitTLS();
|
|
|
|
static void InitMainThread();
|
2015-04-02 23:21:53 +03:00
|
|
|
|
2015-06-10 01:14:09 +03:00
|
|
|
void DispatchStateChange(already_AddRefed<nsIRunnable> aRunnable);
|
|
|
|
|
|
|
|
static void DispatchDirectTask(already_AddRefed<nsIRunnable> aRunnable);
|
|
|
|
|
2016-11-29 08:01:18 +03:00
|
|
|
// Create a runnable that will run |aRunnable| and drain the direct tasks
|
|
|
|
// generated by it.
|
|
|
|
virtual already_AddRefed<nsIRunnable>
|
|
|
|
CreateDirectTaskDrainer(already_AddRefed<nsIRunnable> aRunnable)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Not support!");
|
|
|
|
}
|
|
|
|
|
2017-08-01 00:29:07 +03:00
|
|
|
struct AutoEnter
|
|
|
|
{
|
2017-08-04 01:17:11 +03:00
|
|
|
explicit AutoEnter(AbstractThread* aThread)
|
2017-08-01 00:29:07 +03:00
|
|
|
{
|
|
|
|
mLastCurrentThread = sCurrentThreadTLS.get();
|
|
|
|
sCurrentThreadTLS.set(aThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoEnter()
|
|
|
|
{
|
|
|
|
sCurrentThreadTLS.set(mLastCurrentThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AbstractThread* mLastCurrentThread = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-03-14 05:41:12 +03:00
|
|
|
protected:
|
|
|
|
virtual ~AbstractThread() {}
|
2015-11-23 22:11:22 +03:00
|
|
|
static MOZ_THREAD_LOCAL(AbstractThread*) sCurrentThreadTLS;
|
2015-04-14 09:53:07 +03:00
|
|
|
|
|
|
|
// True if we want to require that every task dispatched from tasks running in
|
|
|
|
// this queue go through our queue's tail dispatcher.
|
2015-05-14 04:47:42 +03:00
|
|
|
const bool mSupportsTailDispatch;
|
2015-03-14 05:41:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|