Source code

Revision control

Copy as Markdown

Other Tools

/* -*- 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 nsThreadUtils_h__
#define nsThreadUtils_h__
#include <type_traits>
#include <tuple>
#include <utility>
#include "MainThreadUtils.h"
#include "mozilla/EventQueue.h"
#include "mozilla/AbstractThread.h"
#include "mozilla/Atomics.h"
#include "mozilla/Likely.h"
#include "mozilla/Maybe.h"
#include "mozilla/ThreadLocal.h"
#include "mozilla/TimeStamp.h"
#include "nsCOMPtr.h"
#include "nsICancelableRunnable.h"
#include "nsIDiscardableRunnable.h"
#include "nsIIdlePeriod.h"
#include "nsIIdleRunnable.h"
#include "nsINamed.h"
#include "nsIRunnable.h"
#include "nsIThreadManager.h"
#include "nsITimer.h"
#include "nsString.h"
#include "prinrval.h"
#include "prthread.h"
class MessageLoop;
class nsIThread;
//-----------------------------------------------------------------------------
// These methods are alternatives to the methods on nsIThreadManager, provided
// for convenience.
/**
* Create a new thread, and optionally provide an initial event for the thread.
*
* @param aName
* The name of the thread.
* @param aResult
* The resulting nsIThread object.
* @param aInitialEvent
* The initial event to run on this thread. This parameter may be null.
* @param aOptions
* Options used to configure thread creation.
* Options are documented in nsIThreadManager.idl.
*
* @returns NS_ERROR_INVALID_ARG
* Indicates that the given name is not unique.
*/
extern nsresult NS_NewNamedThread(
const nsACString& aName, nsIThread** aResult,
nsIRunnable* aInitialEvent = nullptr,
nsIThreadManager::ThreadCreationOptions aOptions = {});
extern nsresult NS_NewNamedThread(
const nsACString& aName, nsIThread** aResult,
already_AddRefed<nsIRunnable> aInitialEvent,
nsIThreadManager::ThreadCreationOptions aOptions = {});
template <size_t LEN>
inline nsresult NS_NewNamedThread(
const char (&aName)[LEN], nsIThread** aResult,
already_AddRefed<nsIRunnable> aInitialEvent,
nsIThreadManager::ThreadCreationOptions aOptions = {}) {
static_assert(LEN <= 16, "Thread name must be no more than 16 characters");
return NS_NewNamedThread(nsDependentCString(aName, LEN - 1), aResult,
std::move(aInitialEvent), aOptions);
}
template <size_t LEN>
inline nsresult NS_NewNamedThread(
const char (&aName)[LEN], nsIThread** aResult,
nsIRunnable* aInitialEvent = nullptr,
nsIThreadManager::ThreadCreationOptions aOptions = {}) {
nsCOMPtr<nsIRunnable> event = aInitialEvent;
static_assert(LEN <= 16, "Thread name must be no more than 16 characters");
return NS_NewNamedThread(nsDependentCString(aName, LEN - 1), aResult,
event.forget(), aOptions);
}
/**
* Get a reference to the current thread, creating it if it does not exist yet.
*
* @param aResult
* The resulting nsIThread object.
*/
extern nsresult NS_GetCurrentThread(nsIThread** aResult);
/**
* Dispatch the given event to the current thread.
*
* @param aEvent
* The event to dispatch.
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
*/
extern nsresult NS_DispatchToCurrentThread(nsIRunnable* aEvent);
extern nsresult NS_DispatchToCurrentThread(
already_AddRefed<nsIRunnable>&& aEvent);
/**
* Dispatch the given event to the main thread.
*
* @param aEvent
* The event to dispatch.
* @param aDispatchFlags
* The flags to pass to the main thread's dispatch method.
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
*/
extern nsresult NS_DispatchToMainThread(
nsIRunnable* aEvent, uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
extern nsresult NS_DispatchToMainThread(
already_AddRefed<nsIRunnable>&& aEvent,
uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
extern nsresult NS_DelayedDispatchToCurrentThread(
already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDelayMs);
/**
* Dispatch the given event to the specified queue of the current thread.
*
* @param aEvent The event to dispatch.
* @param aQueue The event queue for the thread to use
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
* @returns NS_ERROR_UNEXPECTED
* If the thread is shutting down.
*/
extern nsresult NS_DispatchToCurrentThreadQueue(
already_AddRefed<nsIRunnable>&& aEvent, mozilla::EventQueuePriority aQueue);
/**
* Dispatch the given event to the specified queue of the main thread.
*
* @param aEvent The event to dispatch.
* @param aQueue The event queue for the thread to use
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
* @returns NS_ERROR_UNEXPECTED
* If the thread is shutting down.
*/
extern nsresult NS_DispatchToMainThreadQueue(
already_AddRefed<nsIRunnable>&& aEvent, mozilla::EventQueuePriority aQueue);
/**
* Dispatch the given event to an idle queue of the current thread.
*
* @param aEvent The event to dispatch. If the event implements
* nsIIdleRunnable, it will receive a call on
* nsIIdleRunnable::SetTimer when dispatched, with the value of
* aTimeout.
*
* @param aTimeout The time in milliseconds until the event should be
* moved from an idle queue to the regular queue, if it hasn't been
* executed. If aEvent is also an nsIIdleRunnable, it is expected
* that it should handle the timeout itself, after a call to
* nsIIdleRunnable::SetTimer.
*
* @param aQueue
* The event queue for the thread to use. Must be an idle queue
* (Idle or DeferredTimers)
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
* @returns NS_ERROR_UNEXPECTED
* If the thread is shutting down.
*/
extern nsresult NS_DispatchToCurrentThreadQueue(
already_AddRefed<nsIRunnable>&& aEvent, uint32_t aTimeout,
mozilla::EventQueuePriority aQueue);
/**
* Dispatch the given event to a queue of a thread.
*
* @param aEvent The event to dispatch.
* @param aThread The target thread for the dispatch.
* @param aQueue The event queue for the thread to use.
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
* @returns NS_ERROR_UNEXPECTED
* If the thread is shutting down.
*/
extern nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
nsIThread* aThread,
mozilla::EventQueuePriority aQueue);
/**
* Dispatch the given event to an idle queue of a thread.
*
* @param aEvent The event to dispatch. If the event implements
* nsIIdleRunnable, it will receive a call on
* nsIIdleRunnable::SetTimer when dispatched, with the value of
* aTimeout.
*
* @param aTimeout The time in milliseconds until the event should be
* moved from an idle queue to the regular queue, if it hasn't been
* executed. If aEvent is also an nsIIdleRunnable, it is expected
* that it should handle the timeout itself, after a call to
* nsIIdleRunnable::SetTimer.
*
* @param aThread The target thread for the dispatch.
*
* @param aQueue
* The event queue for the thread to use. Must be an idle queue
* (Idle or DeferredTimers)
*
* @returns NS_ERROR_INVALID_ARG
* If event is null.
* @returns NS_ERROR_UNEXPECTED
* If the thread is shutting down.
*/
extern nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
uint32_t aTimeout, nsIThread* aThread,
mozilla::EventQueuePriority aQueue);
#ifndef XPCOM_GLUE_AVOID_NSPR
/**
* Process all pending events for the given thread before returning. This
* method simply calls ProcessNextEvent on the thread while HasPendingEvents
* continues to return true and the time spent in NS_ProcessPendingEvents
* does not exceed the given timeout value.
*
* @param aThread
* The thread object for which to process pending events. If null, then
* events will be processed for the current thread.
* @param aTimeout
* The maximum number of milliseconds to spend processing pending events.
* Events are not pre-empted to honor this timeout. Rather, the timeout
* value is simply used to determine whether or not to process another event.
* Pass PR_INTERVAL_NO_TIMEOUT to specify no timeout.
*/
extern nsresult NS_ProcessPendingEvents(
nsIThread* aThread, PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT);
#endif
/**
* Shortcut for nsIThread::HasPendingEvents.
*
* It is an error to call this function when the given thread is not the
* current thread. This function will return false if called from some
* other thread.
*
* @param aThread
* The current thread or null.
*
* @returns
* A boolean value that if "true" indicates that there are pending events
* in the current thread's event queue.
*/
extern bool NS_HasPendingEvents(nsIThread* aThread = nullptr);
/**
* Shortcut for nsIThread::ProcessNextEvent.
*
* It is an error to call this function when the given thread is not the
* current thread. This function will simply return false if called
* from some other thread.
*
* @param aThread
* The current thread or null.
* @param aMayWait
* A boolean parameter that if "true" indicates that the method may block
* the calling thread to wait for a pending event.
*
* @returns
* A boolean value that if "true" indicates that an event from the current
* thread's event queue was processed.
*/
extern bool NS_ProcessNextEvent(nsIThread* aThread = nullptr,
bool aMayWait = true);
/**
* Returns true if we're in the compositor thread.
*
* We declare this here because the headers required to invoke
* CompositorThreadHolder::IsInCompositorThread() also pull in a bunch of system
* headers that #define various tokens in a way that can break the build.
*/
extern bool NS_IsInCompositorThread();
extern bool NS_IsInCanvasThreadOrWorker();
extern bool NS_IsInVRThread();
//-----------------------------------------------------------------------------
// Helpers that work with nsCOMPtr:
inline already_AddRefed<nsIThread> do_GetCurrentThread() {
nsIThread* thread = nullptr;
NS_GetCurrentThread(&thread);
return already_AddRefed<nsIThread>(thread);
}
inline already_AddRefed<nsIThread> do_GetMainThread() {
nsIThread* thread = nullptr;
NS_GetMainThread(&thread);
return already_AddRefed<nsIThread>(thread);
}
//-----------------------------------------------------------------------------
// Fast access to the current thread. Will create an nsIThread if one does not
// exist already! Do not release the returned pointer! If you want to use this
// pointer from some other thread, then you will need to AddRef it. Otherwise,
// you should only consider this pointer valid from code running on the current
// thread.
extern nsIThread* NS_GetCurrentThread();
// Exactly the same as NS_GetCurrentThread, except it will not create an
// nsThread if one does not exist yet. This is useful in cases where you have
// code that runs on threads that may or may not not be driven by an nsThread
// event loop, and wish to avoid inadvertently creating a superfluous nsThread.
extern nsIThread* NS_GetCurrentThreadNoCreate();
/**
* Set the name of the current thread. Prefer this function over
* PR_SetCurrentThreadName() if possible. The name will also be included in the
* crash report.
*
* @param aName
* Name of the thread. A C language null-terminated string.
*/
extern void NS_SetCurrentThreadName(const char* aName);
//-----------------------------------------------------------------------------
#ifndef XPCOM_GLUE_AVOID_NSPR
namespace mozilla {
// This class is designed to be subclassed.
class IdlePeriod : public nsIIdlePeriod {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIIDLEPERIOD
IdlePeriod() = default;
protected:
virtual ~IdlePeriod() = default;
private:
IdlePeriod(const IdlePeriod&) = delete;
IdlePeriod& operator=(const IdlePeriod&) = delete;
IdlePeriod& operator=(const IdlePeriod&&) = delete;
};
// Cancelable runnable methods implement nsICancelableRunnable, and
// Idle and IdleWithTimer also nsIIdleRunnable.
enum class RunnableKind { Standard, Cancelable, Idle, IdleWithTimer };
// Implementing nsINamed on Runnable bloats vtables for the hundreds of
// Runnable subclasses that we have, so we want to avoid that overhead
// when we're not using nsINamed for anything.
# ifndef RELEASE_OR_BETA
# define MOZ_COLLECTING_RUNNABLE_TELEMETRY
# endif
// This class is designed to be subclassed.
class Runnable : public nsIRunnable
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
,
public nsINamed
# endif
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIRUNNABLE
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
NS_DECL_NSINAMED
# endif
Runnable() = delete;
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
explicit Runnable(const char* aName) : mName(aName) {}
# else
explicit Runnable(const char* aName) {}
# endif
protected:
virtual ~Runnable() = default;
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
const char* mName = nullptr;
# endif
private:
Runnable(const Runnable&) = delete;
Runnable& operator=(const Runnable&) = delete;
Runnable& operator=(const Runnable&&) = delete;
};
// This is a base class for tasks that might not be run, such as those that may
// be dispatched to workers.
// The owner of an event target will call either Run() or OnDiscard()
// exactly once.
// Derived classes should override Run(). An OnDiscard() override may
// provide cleanup when Run() will not be called.
class DiscardableRunnable : public Runnable, public nsIDiscardableRunnable {
public:
NS_DECL_ISUPPORTS_INHERITED
// nsIDiscardableRunnable
void OnDiscard() override {}
DiscardableRunnable() = delete;
explicit DiscardableRunnable(const char* aName) : Runnable(aName) {}
protected:
virtual ~DiscardableRunnable() = default;
private:
DiscardableRunnable(const DiscardableRunnable&) = delete;
DiscardableRunnable& operator=(const DiscardableRunnable&) = delete;
DiscardableRunnable& operator=(const DiscardableRunnable&&) = delete;
};
// This class is designed to be subclassed.
// Derived classes should override Run() and Cancel() to provide that
// calling Run() after Cancel() is a no-op.
class CancelableRunnable : public DiscardableRunnable,
public nsICancelableRunnable {
public:
NS_DECL_ISUPPORTS_INHERITED
// nsIDiscardableRunnable
void OnDiscard() override;
// nsICancelableRunnable
virtual nsresult Cancel() override = 0;
CancelableRunnable() = delete;
explicit CancelableRunnable(const char* aName) : DiscardableRunnable(aName) {}
protected:
virtual ~CancelableRunnable() = default;
private:
CancelableRunnable(const CancelableRunnable&) = delete;
CancelableRunnable& operator=(const CancelableRunnable&) = delete;
CancelableRunnable& operator=(const CancelableRunnable&&) = delete;
};
// This class is designed to be subclassed.
class IdleRunnable : public DiscardableRunnable, public nsIIdleRunnable {
public:
NS_DECL_ISUPPORTS_INHERITED
explicit IdleRunnable(const char* aName) : DiscardableRunnable(aName) {}
protected:
virtual ~IdleRunnable() = default;
private:
IdleRunnable(const IdleRunnable&) = delete;
IdleRunnable& operator=(const IdleRunnable&) = delete;
IdleRunnable& operator=(const IdleRunnable&&) = delete;
};
// This class is designed to be subclassed.
class CancelableIdleRunnable : public CancelableRunnable,
public nsIIdleRunnable {
public:
NS_DECL_ISUPPORTS_INHERITED
CancelableIdleRunnable() : CancelableRunnable("CancelableIdleRunnable") {}
explicit CancelableIdleRunnable(const char* aName)
: CancelableRunnable(aName) {}
protected:
virtual ~CancelableIdleRunnable() = default;
private:
CancelableIdleRunnable(const CancelableIdleRunnable&) = delete;
CancelableIdleRunnable& operator=(const CancelableIdleRunnable&) = delete;
CancelableIdleRunnable& operator=(const CancelableIdleRunnable&&) = delete;
};
// This class is designed to be a wrapper of a real runnable to support event
// prioritizable.
class PrioritizableRunnable : public Runnable, public nsIRunnablePriority {
public:
PrioritizableRunnable(already_AddRefed<nsIRunnable>&& aRunnable,
uint32_t aPriority);
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
NS_IMETHOD GetName(nsACString& aName) override;
# endif
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIRUNNABLE
NS_DECL_NSIRUNNABLEPRIORITY
protected:
virtual ~PrioritizableRunnable() = default;
nsCOMPtr<nsIRunnable> mRunnable;
uint32_t mPriority;
};
class PrioritizableCancelableRunnable : public CancelableRunnable,
public nsIRunnablePriority {
public:
PrioritizableCancelableRunnable(uint32_t aPriority, const char* aName)
: CancelableRunnable(aName), mPriority(aPriority) {}
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIRUNNABLEPRIORITY
protected:
virtual ~PrioritizableCancelableRunnable() = default;
const uint32_t mPriority;
};
extern already_AddRefed<nsIRunnable> CreateRenderBlockingRunnable(
already_AddRefed<nsIRunnable>&& aRunnable);
namespace detail {
// An event that can be used to call a C++11 functions or function objects,
// including lambdas. The function must have no required arguments, and must
// return void.
template <typename StoredFunction>
class RunnableFunction : public Runnable {
public:
template <typename F>
explicit RunnableFunction(const char* aName, F&& aFunction)
: Runnable(aName), mFunction(std::forward<F>(aFunction)) {}
NS_IMETHOD Run() override {
static_assert(std::is_void_v<decltype(mFunction())>,
"The lambda must return void!");
mFunction();
return NS_OK;
}
private:
StoredFunction mFunction;
};
// Type alias for NS_NewRunnableFunction
template <typename Function>
using RunnableFunctionImpl =
// Make sure we store a non-reference in nsRunnableFunction.
typename detail::RunnableFunction<std::remove_reference_t<Function>>;
} // namespace detail
namespace detail {
template <typename T>
struct RemoveSmartPointerHelper {
using Type = T;
};
template <typename T>
struct RemoveSmartPointerHelper<RefPtr<T>> {
using Type = T;
};
template <typename T>
struct RemoveSmartPointerHelper<nsCOMPtr<T>> {
using Type = T;
};
template <typename T>
struct RemoveRawOrSmartPointerHelper {
using Type = typename RemoveSmartPointerHelper<T>::Type;
};
template <typename T>
struct RemoveRawOrSmartPointerHelper<T*> {
using Type = T;
};
} // namespace detail
template <typename T>
using RemoveSmartPointer =
typename detail::RemoveSmartPointerHelper<std::remove_cv_t<T>>::Type;
template <typename T>
using RemoveRawOrSmartPointer =
typename detail::RemoveRawOrSmartPointerHelper<std::remove_cv_t<T>>::Type;
} // namespace mozilla
inline nsISupports* ToSupports(mozilla::Runnable* p) {
return static_cast<nsIRunnable*>(p);
}
template <typename Function>
already_AddRefed<mozilla::Runnable> NS_NewRunnableFunction(
const char* aName, Function&& aFunction) {
// We store a non-reference in RunnableFunction, but still forward aFunction
// to move if possible.
return do_AddRef(new mozilla::detail::RunnableFunctionImpl<Function>(
aName, std::forward<Function>(aFunction)));
}
// Creates a new object implementing nsIRunnable and nsICancelableRunnable,
// which runs a given function on Run and clears the stored function object on a
// call to `Cancel` (and thus destroys all objects it holds).
template <typename Function>
already_AddRefed<mozilla::CancelableRunnable> NS_NewCancelableRunnableFunction(
const char* aName, Function&& aFunc) {
class FuncCancelableRunnable final : public mozilla::CancelableRunnable {
public:
static_assert(
std::is_void_v<
decltype(std::declval<std::remove_reference_t<Function>>()())>);
NS_INLINE_DECL_REFCOUNTING_INHERITED(FuncCancelableRunnable,
CancelableRunnable)
explicit FuncCancelableRunnable(const char* aName, Function&& aFunc)
: CancelableRunnable{aName},
mFunc{mozilla::Some(std::forward<Function>(aFunc))} {}
NS_IMETHOD Run() override {
if (mFunc) {
(*mFunc)();
}
return NS_OK;
}
nsresult Cancel() override {
mFunc.reset();
return NS_OK;
}
private:
~FuncCancelableRunnable() = default;
mozilla::Maybe<std::remove_reference_t<Function>> mFunc;
};
return mozilla::MakeAndAddRef<FuncCancelableRunnable>(
aName, std::forward<Function>(aFunc));
}
namespace mozilla {
namespace detail {
template <RunnableKind Kind>
class TimerBehaviour {
public:
nsITimer* GetTimer() { return nullptr; }
void CancelTimer() {}
protected:
~TimerBehaviour() = default;
};
template <>
class TimerBehaviour<RunnableKind::IdleWithTimer> {
public:
nsITimer* GetTimer() {
if (!mTimer) {
mTimer = NS_NewTimer();
}
return mTimer;
}
void CancelTimer() {
if (mTimer) {
mTimer->Cancel();
}
}
protected:
~TimerBehaviour() { CancelTimer(); }
private:
nsCOMPtr<nsITimer> mTimer;
};
} // namespace detail
} // namespace mozilla
// An event that can be used to call a method on a class. The class type must
// support reference counting. This event supports Revoke for use
// with nsRevocableEventPtr.
template <class ClassType, typename ReturnType = void, bool Owning = true,
mozilla::RunnableKind Kind = mozilla::RunnableKind::Standard>
class nsRunnableMethod
: public std::conditional_t<
Kind == mozilla::RunnableKind::Standard, mozilla::Runnable,
std::conditional_t<Kind == mozilla::RunnableKind::Cancelable,
mozilla::CancelableRunnable,
mozilla::CancelableIdleRunnable>>,
protected mozilla::detail::TimerBehaviour<Kind> {
using BaseType = std::conditional_t<
Kind == mozilla::RunnableKind::Standard, mozilla::Runnable,
std::conditional_t<Kind == mozilla::RunnableKind::Cancelable,
mozilla::CancelableRunnable,
mozilla::CancelableIdleRunnable>>;
public:
nsRunnableMethod(const char* aName) : BaseType(aName) {}
virtual void Revoke() = 0;
// These ReturnTypeEnforcer classes disallow return types that
// we know are not safe. The default ReturnTypeEnforcer compiles just fine but
// already_AddRefed will not.
template <typename OtherReturnType>
class ReturnTypeEnforcer {
public:
typedef int ReturnTypeIsSafe;
};
template <class T>
class ReturnTypeEnforcer<already_AddRefed<T>> {
// No ReturnTypeIsSafe makes this illegal!
};
// Make sure this return type is safe.
typedef typename ReturnTypeEnforcer<ReturnType>::ReturnTypeIsSafe check;
};
template <class ClassType, bool Owning>
struct nsRunnableMethodReceiver {
RefPtr<ClassType> mObj;
explicit nsRunnableMethodReceiver(ClassType* aObj) : mObj(aObj) {}
explicit nsRunnableMethodReceiver(RefPtr<ClassType>&& aObj)
: mObj(std::move(aObj)) {}
~nsRunnableMethodReceiver() { Revoke(); }
ClassType* Get() const { return mObj.get(); }
void Revoke() { mObj = nullptr; }
};
template <class ClassType>
struct nsRunnableMethodReceiver<ClassType, false> {
ClassType* MOZ_NON_OWNING_REF mObj;
explicit nsRunnableMethodReceiver(ClassType* aObj) : mObj(aObj) {}
ClassType* Get() const { return mObj; }
void Revoke() { mObj = nullptr; }
};
static inline constexpr bool IsIdle(mozilla::RunnableKind aKind) {
return aKind == mozilla::RunnableKind::Idle ||
aKind == mozilla::RunnableKind::IdleWithTimer;
}
template <typename PtrType, typename Method, bool Owning,
mozilla::RunnableKind Kind>
struct nsRunnableMethodTraits;
template <typename PtrType, class C, typename R, bool Owning,
mozilla::RunnableKind Kind, typename... As>
struct nsRunnableMethodTraits<PtrType, R (C::*)(As...), Owning, Kind> {
using class_type = mozilla::RemoveRawOrSmartPointer<PtrType>;
static_assert(std::is_base_of<C, class_type>::value,
"Stored class must inherit from method's class");
using return_type = R;
using base_type = nsRunnableMethod<C, R, Owning, Kind>;
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
};
template <typename PtrType, class C, typename R, bool Owning,
mozilla::RunnableKind Kind, typename... As>
struct nsRunnableMethodTraits<PtrType, R (C::*)(As...) const, Owning, Kind> {
using class_type = const mozilla::RemoveRawOrSmartPointer<PtrType>;
static_assert(std::is_base_of<C, class_type>::value,
"Stored class must inherit from method's class");
using return_type = R;
using base_type = nsRunnableMethod<C, R, Owning, Kind>;
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
};
# ifdef NS_HAVE_STDCALL
template <typename PtrType, class C, typename R, bool Owning,
mozilla::RunnableKind Kind, typename... As>
struct nsRunnableMethodTraits<PtrType, R (__stdcall C::*)(As...), Owning,
Kind> {
using class_type = mozilla::RemoveRawOrSmartPointer<PtrType>;
static_assert(std::is_base_of<C, class_type>::value,
"Stored class must inherit from method's class");
using return_type = R;
using base_type = nsRunnableMethod<C, R, Owning, Kind>;
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
};
template <typename PtrType, class C, typename R, bool Owning,
mozilla::RunnableKind Kind>
struct nsRunnableMethodTraits<PtrType, R (NS_STDCALL C::*)(), Owning, Kind> {
using class_type = mozilla::RemoveRawOrSmartPointer<PtrType>;
static_assert(std::is_base_of<C, class_type>::value,
"Stored class must inherit from method's class");
using return_type = R;
using base_type = nsRunnableMethod<C, R, Owning, Kind>;
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
};
template <typename PtrType, class C, typename R, bool Owning,
mozilla::RunnableKind Kind, typename... As>
struct nsRunnableMethodTraits<PtrType, R (__stdcall C::*)(As...) const, Owning,
Kind> {
using class_type = const mozilla::RemoveRawOrSmartPointer<PtrType>;
static_assert(std::is_base_of<C, class_type>::value,
"Stored class must inherit from method's class");
using return_type = R;
using base_type = nsRunnableMethod<C, R, Owning, Kind>;
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
};
template <typename PtrType, class C, typename R, bool Owning,
mozilla::RunnableKind Kind>
struct nsRunnableMethodTraits<PtrType, R (NS_STDCALL C::*)() const, Owning,
Kind> {
using class_type = const mozilla::RemoveRawOrSmartPointer<PtrType>;
static_assert(std::is_base_of<C, class_type>::value,
"Stored class must inherit from method's class");
using return_type = R;
using base_type = nsRunnableMethod<C, R, Owning, Kind>;
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
};
# endif
// IsParameterStorageClass<T>::value is true if T is a parameter-storage class
// that will be recognized by NS_New[NonOwning]RunnableMethodWithArg[s] to
// force a specific storage&passing strategy (instead of inferring one,
// see ParameterStorage).
// When creating a new storage class, add a specialization for it to be
// recognized.
template <typename T>
struct IsParameterStorageClass : public std::false_type {};
// StoreXPassByY structs used to inform nsRunnableMethodArguments how to
// store arguments, and how to pass them to the target method.
template <typename T>
struct StoreCopyPassByConstLRef {
using stored_type = std::decay_t<T>;
typedef const stored_type& passed_type;
stored_type m;
template <typename A>
MOZ_IMPLICIT StoreCopyPassByConstLRef(A&& a) : m(std::forward<A>(a)) {}
passed_type PassAsParameter() { return m; }
};
template <typename S>
struct IsParameterStorageClass<StoreCopyPassByConstLRef<S>>
: public std::true_type {};
template <typename T>
struct StoreCopyPassByRRef {
using stored_type = std::decay_t<T>;
typedef stored_type&& passed_type;
stored_type m;
template <typename A>
MOZ_IMPLICIT StoreCopyPassByRRef(A&& a) : m(std::forward<A>(a)) {}
passed_type PassAsParameter() { return std::move(m); }
};
template <typename S>
struct IsParameterStorageClass<StoreCopyPassByRRef<S>> : public std::true_type {
};
template <typename T>
struct StoreRefPassByLRef {
typedef T& stored_type;
typedef T& passed_type;
stored_type m;
template <typename A>
MOZ_IMPLICIT StoreRefPassByLRef(A& a) : m(a) {}
passed_type PassAsParameter() { return m; }
};