2014-06-30 19:39:45 +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: */
|
2012-05-21 15:12:37 +04: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/. */
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#ifndef nsThreadUtils_h__
|
|
|
|
#define nsThreadUtils_h__
|
|
|
|
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "prinrval.h"
|
2013-09-19 17:54:41 +04:00
|
|
|
#include "MainThreadUtils.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIThreadManager.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsIRunnable.h"
|
2012-12-11 22:13:29 +04:00
|
|
|
#include "nsICancelableRunnable.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2010-05-11 05:05:40 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2015-03-10 21:41:45 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2015-04-10 15:13:00 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// These methods are alternatives to the methods on nsIThreadManager, provided
|
|
|
|
// for convenience.
|
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
/**
|
|
|
|
* Set name of the target thread. This operation is asynchronous.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern void NS_SetThreadName(nsIThread* aThread, const nsACString& aName);
|
2012-06-12 21:06:20 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static length version of the above function checking length of the
|
|
|
|
* name at compile time.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template<size_t LEN>
|
2014-08-28 02:47:27 +04:00
|
|
|
inline void
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_SetThreadName(nsIThread* aThread, const char (&aName)[LEN])
|
2012-06-12 21:06:20 +04:00
|
|
|
{
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(LEN <= 16,
|
|
|
|
"Thread name must be no more than 16 characters");
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_SetThreadName(aThread, nsDependentCString(aName));
|
2012-06-12 21:06:20 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
/**
|
|
|
|
* Create a new thread, and optionally provide an initial event for the thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aResult
|
2006-05-10 21:30:15 +04:00
|
|
|
* The resulting nsIThread object.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aInitialEvent
|
2006-05-10 21:30:15 +04:00
|
|
|
* The initial event to run on this thread. This parameter may be null.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aStackSize
|
2011-07-27 07:26:47 +04:00
|
|
|
* The size in bytes to reserve for the thread's stack.
|
2006-05-10 21:30:15 +04:00
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* Indicates that the given name is not unique.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern NS_METHOD
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_NewThread(nsIThread** aResult,
|
|
|
|
nsIRunnable* aInitialEvent = nullptr,
|
|
|
|
uint32_t aStackSize = nsIThreadManager::DEFAULT_STACK_SIZE);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
/**
|
|
|
|
* Creates a named thread, otherwise the same as NS_NewThread
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template<size_t LEN>
|
2012-06-12 21:06:20 +04:00
|
|
|
inline NS_METHOD
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_NewNamedThread(const char (&aName)[LEN],
|
|
|
|
nsIThread** aResult,
|
|
|
|
nsIRunnable* aInitialEvent = nullptr,
|
|
|
|
uint32_t aStackSize = nsIThreadManager::DEFAULT_STACK_SIZE)
|
2012-06-12 21:06:20 +04:00
|
|
|
{
|
2014-04-17 00:39:16 +04:00
|
|
|
// Hold a ref while dispatching the initial event to match NS_NewThread()
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(thread), nullptr, aStackSize);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
NS_SetThreadName<LEN>(thread, aName);
|
|
|
|
if (aInitialEvent) {
|
|
|
|
rv = thread->Dispatch(aInitialEvent, NS_DISPATCH_NORMAL);
|
2013-08-27 04:36:45 +04:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Initial event dispatch failed");
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
*aResult = nullptr;
|
|
|
|
thread.swap(*aResult);
|
2013-08-27 04:36:45 +04:00
|
|
|
return rv;
|
2012-06-12 21:06:20 +04:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
/**
|
|
|
|
* Get a reference to the current thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aResult
|
2006-05-10 21:30:15 +04:00
|
|
|
* The resulting nsIThread object.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern NS_METHOD NS_GetCurrentThread(nsIThread** aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event to the current thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aEvent
|
2006-05-10 21:30:15 +04:00
|
|
|
* The event to dispatch.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern NS_METHOD NS_DispatchToCurrentThread(nsIRunnable* aEvent);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event to the main thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aEvent
|
2006-05-10 21:30:15 +04:00
|
|
|
* The event to dispatch.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aDispatchFlags
|
2006-05-10 21:30:15 +04:00
|
|
|
* The flags to pass to the main thread's dispatch method.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern NS_METHOD
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_DispatchToMainThread(nsIRunnable* aEvent,
|
|
|
|
uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2007-04-02 19:29:41 +04:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
2006-05-10 21:30:15 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aThread
|
2006-05-10 21:30:15 +04:00
|
|
|
* The thread object for which to process pending events. If null, then
|
|
|
|
* events will be processed for the current thread.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aTimeout
|
2006-05-10 21:30:15 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern NS_METHOD
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_ProcessPendingEvents(nsIThread* aThread,
|
|
|
|
PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT);
|
2007-04-02 19:29:41 +04:00
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for nsIThread::HasPendingEvents.
|
|
|
|
*
|
|
|
|
* It is an error to call this function when the given thread is not the
|
2011-10-17 18:59:28 +04:00
|
|
|
* current thread. This function will return false if called from some
|
2006-05-10 21:30:15 +04:00
|
|
|
* other thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aThread
|
2006-05-10 21:30:15 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern bool NS_HasPendingEvents(nsIThread* aThread = nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for nsIThread::ProcessNextEvent.
|
2014-06-27 05:35:39 +04:00
|
|
|
*
|
2006-05-10 21:30:15 +04:00
|
|
|
* It is an error to call this function when the given thread is not the
|
2011-10-17 18:59:28 +04:00
|
|
|
* current thread. This function will simply return false if called
|
2006-05-10 21:30:15 +04:00
|
|
|
* from some other thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aThread
|
2006-05-10 21:30:15 +04:00
|
|
|
* The current thread or null.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aMayWait
|
2006-05-10 21:30:15 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern bool NS_ProcessNextEvent(nsIThread* aThread = nullptr,
|
|
|
|
bool aMayWait = true);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Helpers that work with nsCOMPtr:
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIThread>
|
2014-06-27 05:35:39 +04:00
|
|
|
do_GetCurrentThread()
|
|
|
|
{
|
|
|
|
nsIThread* thread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_GetCurrentThread(&thread);
|
|
|
|
return already_AddRefed<nsIThread>(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIThread>
|
2014-06-27 05:35:39 +04:00
|
|
|
do_GetMainThread()
|
|
|
|
{
|
|
|
|
nsIThread* thread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_GetMainThread(&thread);
|
|
|
|
return already_AddRefed<nsIThread>(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
// Fast access to the current thread. 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.
|
2014-08-28 02:47:27 +04:00
|
|
|
extern nsIThread* NS_GetCurrentThread();
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
|
|
|
// This class is designed to be subclassed.
|
2014-08-28 02:47:27 +04:00
|
|
|
class nsRunnable : public nsIRunnable
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsRunnable() {}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
protected:
|
2014-06-27 05:35:39 +04:00
|
|
|
virtual ~nsRunnable() {}
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2012-12-11 22:13:29 +04:00
|
|
|
// This class is designed to be subclassed.
|
2014-08-28 02:47:27 +04:00
|
|
|
class nsCancelableRunnable : public nsICancelableRunnable
|
2012-12-11 22:13:29 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-12-11 22:13:29 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSICANCELABLERUNNABLE
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCancelableRunnable() {}
|
2012-12-11 22:13:29 +04:00
|
|
|
|
|
|
|
protected:
|
2014-06-27 05:35:39 +04:00
|
|
|
virtual ~nsCancelableRunnable() {}
|
2012-12-11 22:13:29 +04:00
|
|
|
};
|
|
|
|
|
2015-04-10 15:13:00 +03:00
|
|
|
// 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 Function>
|
|
|
|
class nsRunnableFunction : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit nsRunnableFunction(const Function& aFunction)
|
|
|
|
: mFunction(aFunction)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
static_assert(mozilla::IsVoid<decltype(mFunction())>::value,
|
|
|
|
"The lambda must return void!");
|
|
|
|
mFunction();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Function mFunction;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Function>
|
|
|
|
nsRunnableFunction<Function>* NS_NewRunnableFunction(const Function& aFunction)
|
|
|
|
{
|
|
|
|
return new nsRunnableFunction<Function>(aFunction);
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// An event that can be used to call a method on a class. The class type must
|
2008-08-18 06:10:28 +04:00
|
|
|
// support reference counting. This event supports Revoke for use
|
|
|
|
// with nsRevocableEventPtr.
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class ClassType,
|
|
|
|
typename ReturnType = void,
|
|
|
|
bool Owning = true>
|
2006-05-10 21:30:15 +04:00
|
|
|
class nsRunnableMethod : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-04-21 03:21:32 +04:00
|
|
|
virtual void Revoke() = 0;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2009-02-18 07:11:09 +03:00
|
|
|
// These ReturnTypeEnforcer classes set up a blacklist for return types that
|
|
|
|
// we know are not safe. The default ReturnTypeEnforcer compiles just fine but
|
|
|
|
// already_AddRefed will not.
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename OtherReturnType>
|
2009-02-18 07:11:09 +03:00
|
|
|
class ReturnTypeEnforcer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef int ReturnTypeIsSafe;
|
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T>
|
|
|
|
class ReturnTypeEnforcer<already_AddRefed<T>>
|
2009-02-18 07:11:09 +03:00
|
|
|
{
|
|
|
|
// No ReturnTypeIsSafe makes this illegal!
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure this return type is safe.
|
|
|
|
typedef typename ReturnTypeEnforcer<ReturnType>::ReturnTypeIsSafe check;
|
2010-04-21 03:21:32 +04:00
|
|
|
};
|
2009-02-18 07:11:09 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class ClassType, bool Owning>
|
2015-03-01 20:12:00 +03:00
|
|
|
struct nsRunnableMethodReceiver
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2014-12-24 01:30:55 +03:00
|
|
|
nsRefPtr<ClassType> mObj;
|
2015-03-01 20:12:00 +03:00
|
|
|
explicit nsRunnableMethodReceiver(ClassType* aObj) : mObj(aObj) {}
|
2013-09-07 05:29:24 +04:00
|
|
|
~nsRunnableMethodReceiver() { Revoke(); }
|
2015-03-01 20:12:00 +03:00
|
|
|
ClassType* Get() const { return mObj.get(); }
|
2014-12-24 01:30:55 +03:00
|
|
|
void Revoke() { mObj = nullptr; }
|
2010-04-21 03:21:32 +04:00
|
|
|
};
|
2006-05-13 00:18:13 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class ClassType>
|
2015-03-01 20:12:00 +03:00
|
|
|
struct nsRunnableMethodReceiver<ClassType, false>
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2014-12-24 05:17:50 +03:00
|
|
|
ClassType* MOZ_NON_OWNING_REF mObj;
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsRunnableMethodReceiver(ClassType* aObj) : mObj(aObj) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
ClassType* Get() const { return mObj; }
|
2012-07-30 18:20:58 +04:00
|
|
|
void Revoke() { mObj = nullptr; }
|
2010-04-21 03:21:32 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename Method, bool Owning> struct nsRunnableMethodTraits;
|
2010-04-21 03:21:32 +04:00
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template<class C, typename R, bool Owning, typename... As>
|
|
|
|
struct nsRunnableMethodTraits<R(C::*)(As...), Owning>
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2013-09-07 05:29:24 +04:00
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
2010-04-21 03:21:32 +04:00
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
|
2013-07-17 06:01:50 +04:00
|
|
|
#ifdef NS_HAVE_STDCALL
|
2015-03-01 20:12:00 +03:00
|
|
|
template<class C, typename R, bool Owning, typename... As>
|
|
|
|
struct nsRunnableMethodTraits<R(__stdcall C::*)(As...), Owning>
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2013-09-07 05:29:24 +04:00
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class C, typename R, bool Owning>
|
|
|
|
struct nsRunnableMethodTraits<R(NS_STDCALL C::*)(), Owning>
|
|
|
|
{
|
2010-04-21 03:21:32 +04:00
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
|
|
|
|
// 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 mozilla::FalseType {};
|
|
|
|
|
|
|
|
// StoreXPassByY structs used to inform nsRunnableMethodArguments how to
|
|
|
|
// store arguments, and how to pass them to the target method.
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreCopyPassByValue
|
2010-04-21 03:21:32 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
typedef T stored_type;
|
|
|
|
typedef T passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreCopyPassByValue(A&& a) : m(mozilla::Forward<A>(a)) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByValue<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreCopyPassByConstLRef
|
|
|
|
{
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef const T& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreCopyPassByConstLRef(A&& a) : m(mozilla::Forward<A>(a)) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByConstLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreCopyPassByLRef
|
|
|
|
{
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef T& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreCopyPassByLRef(A&& a) : m(mozilla::Forward<A>(a)) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreCopyPassByRRef
|
|
|
|
{
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef T&& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreCopyPassByRRef(A&& a) : m(mozilla::Forward<A>(a)) {}
|
|
|
|
passed_type PassAsParameter() { return mozilla::Move(m); }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByRRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreRefPassByLRef
|
|
|
|
{
|
|
|
|
typedef T& stored_type;
|
|
|
|
typedef T& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreRefPassByLRef(A& a) : m(a) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreRefPassByLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreConstRefPassByConstLRef
|
|
|
|
{
|
|
|
|
typedef const T& stored_type;
|
|
|
|
typedef const T& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreConstRefPassByConstLRef(const A& a) : m(a) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreConstRefPassByConstLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StorensRefPtrPassByPtr
|
|
|
|
{
|
|
|
|
typedef nsRefPtr<T> stored_type;
|
|
|
|
typedef T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StorensRefPtrPassByPtr(A a) : m(a) {}
|
|
|
|
passed_type PassAsParameter() { return m.get(); }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StorensRefPtrPassByPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StorePtrPassByPtr
|
|
|
|
{
|
|
|
|
typedef T* stored_type;
|
|
|
|
typedef T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StorePtrPassByPtr(A a) : m(a) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StorePtrPassByPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreConstPtrPassByConstPtr
|
|
|
|
{
|
|
|
|
typedef const T* stored_type;
|
|
|
|
typedef const T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreConstPtrPassByConstPtr(A a) : m(a) {}
|
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreConstPtrPassByConstPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreCopyPassByConstPtr
|
|
|
|
{
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef const T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreCopyPassByConstPtr(A&& a) : m(mozilla::Forward<A>(a)) {}
|
|
|
|
passed_type PassAsParameter() { return &m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByConstPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct StoreCopyPassByPtr
|
|
|
|
{
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
|
|
|
StoreCopyPassByPtr(A&& a) : m(mozilla::Forward<A>(a)) {}
|
|
|
|
passed_type PassAsParameter() { return &m; }
|
|
|
|
};
|
|
|
|
template<typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<typename TWithoutPointer>
|
|
|
|
struct NonnsISupportsPointerStorageClass
|
|
|
|
: mozilla::Conditional<mozilla::IsConst<TWithoutPointer>::value,
|
|
|
|
StoreConstPtrPassByConstPtr<
|
|
|
|
typename mozilla::RemoveConst<TWithoutPointer>::Type>,
|
|
|
|
StorePtrPassByPtr<TWithoutPointer>>
|
|
|
|
{};
|
|
|
|
|
2015-04-26 11:52:00 +03:00
|
|
|
template<typename>
|
|
|
|
struct SFINAE1True : mozilla::TrueType
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static auto HasRefCountMethodsTest(int)
|
|
|
|
-> SFINAE1True<decltype(mozilla::DeclVal<T>().AddRef(),
|
|
|
|
mozilla::DeclVal<T>().Release())>;
|
|
|
|
template<class>
|
|
|
|
static auto HasRefCountMethodsTest(long) -> mozilla::FalseType;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
struct HasRefCountMethods : decltype(HasRefCountMethodsTest<T>(0))
|
|
|
|
{};
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template<typename TWithoutPointer>
|
|
|
|
struct PointerStorageClass
|
2015-04-26 11:52:00 +03:00
|
|
|
: mozilla::Conditional<HasRefCountMethods<TWithoutPointer>::value,
|
2015-03-01 20:12:00 +03:00
|
|
|
StorensRefPtrPassByPtr<TWithoutPointer>,
|
|
|
|
typename NonnsISupportsPointerStorageClass<
|
|
|
|
TWithoutPointer
|
|
|
|
>::Type>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename TWithoutRef>
|
|
|
|
struct LValueReferenceStorageClass
|
|
|
|
: mozilla::Conditional<mozilla::IsConst<TWithoutRef>::value,
|
|
|
|
StoreConstRefPassByConstLRef<
|
|
|
|
typename mozilla::RemoveConst<TWithoutRef>::Type>,
|
|
|
|
StoreRefPassByLRef<TWithoutRef>>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct NonLValueReferenceStorageClass
|
|
|
|
: mozilla::Conditional<mozilla::IsRvalueReference<T>::value,
|
|
|
|
StoreCopyPassByRRef<
|
|
|
|
typename mozilla::RemoveReference<T>::Type>,
|
|
|
|
StoreCopyPassByValue<T>>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct NonPointerStorageClass
|
|
|
|
: mozilla::Conditional<mozilla::IsLvalueReference<T>::value,
|
|
|
|
typename LValueReferenceStorageClass<
|
|
|
|
typename mozilla::RemoveReference<T>::Type
|
|
|
|
>::Type,
|
|
|
|
typename NonLValueReferenceStorageClass<T>::Type>
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct NonParameterStorageClass
|
|
|
|
: mozilla::Conditional<mozilla::IsPointer<T>::value,
|
|
|
|
typename PointerStorageClass<
|
|
|
|
typename mozilla::RemovePointer<T>::Type
|
|
|
|
>::Type,
|
|
|
|
typename NonPointerStorageClass<T>::Type>
|
|
|
|
{};
|
|
|
|
|
|
|
|
// Choose storage&passing strategy based on preferred storage type:
|
|
|
|
// - If IsParameterStorageClass<T>::value is true, use as-is.
|
2015-04-26 11:52:00 +03:00
|
|
|
// - RC* -> StorensRefPtrPassByPtr<RC> : Store nsRefPtr<RC>, pass RC*
|
|
|
|
// ^^ RC quacks like a ref-counted type (i.e., has AddRef and Release methods)
|
2015-03-01 20:12:00 +03:00
|
|
|
// - const T* -> StoreConstPtrPassByConstPtr<T> : Store const T*, pass const T*
|
|
|
|
// - T* -> StorePtrPassByPtr<T> : Store T*, pass T*.
|
|
|
|
// - const T& -> StoreConstRefPassByConstLRef<T>: Store const T&, pass const T&.
|
|
|
|
// - T& -> StoreRefPassByLRef<T> : Store T&, pass T&.
|
|
|
|
// - T&& -> StoreCopyPassByRRef<T> : Store T, pass Move(T).
|
|
|
|
// - Other T -> StoreCopyPassByValue<T> : Store T, pass T.
|
|
|
|
// Other available explicit options:
|
|
|
|
// - StoreCopyPassByConstLRef<T> : Store T, pass const T&.
|
|
|
|
// - StoreCopyPassByLRef<T> : Store T, pass T& (of copy!)
|
|
|
|
// - StoreCopyPassByConstPtr<T> : Store T, pass const T*
|
|
|
|
// - StoreCopyPassByPtr<T> : Store T, pass T* (of copy!)
|
|
|
|
// Or create your own class with PassAsParameter() method, optional
|
|
|
|
// clean-up in destructor, and with associated IsParameterStorageClass<>.
|
|
|
|
template<typename T>
|
|
|
|
struct ParameterStorage
|
|
|
|
: mozilla::Conditional<IsParameterStorageClass<T>::value,
|
|
|
|
T,
|
|
|
|
typename NonParameterStorageClass<T>::Type>
|
|
|
|
{};
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
|
|
|
// struct used to store arguments and later apply them to a method.
|
|
|
|
template <typename... Ts> struct nsRunnableMethodArguments;
|
|
|
|
|
2015-04-14 18:36:35 +03:00
|
|
|
// Specializations for 0-8 arguments, add more as required.
|
|
|
|
// TODO Use tuple instead; And/or use lambdas (see bug 1152753)
|
2015-03-01 20:12:00 +03:00
|
|
|
template <>
|
|
|
|
struct nsRunnableMethodArguments<>
|
|
|
|
{
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
((*o).*m)();
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2015-03-01 20:12:00 +03:00
|
|
|
};
|
|
|
|
template <typename T0>
|
|
|
|
struct nsRunnableMethodArguments<T0>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
template<typename A0>
|
|
|
|
nsRunnableMethodArguments(A0&& a0)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
((*o).*m)(m0.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename T0, typename T1>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
template<typename A0, typename A1>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1, T2>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
typename ::detail::ParameterStorage<T2>::Type m2;
|
|
|
|
template<typename A0, typename A1, typename A2>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1, A2&& a2)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
, m2(mozilla::Forward<A2>(a2))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter(), m2.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2, typename T3>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1, T2, T3>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
typename ::detail::ParameterStorage<T2>::Type m2;
|
|
|
|
typename ::detail::ParameterStorage<T3>::Type m3;
|
|
|
|
template<typename A0, typename A1, typename A2, typename A3>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1, A2&& a2, A3&& a3)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
, m2(mozilla::Forward<A2>(a2))
|
|
|
|
, m3(mozilla::Forward<A3>(a3))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter(),
|
|
|
|
m2.PassAsParameter(), m3.PassAsParameter());
|
2013-09-07 05:29:24 +04:00
|
|
|
}
|
|
|
|
};
|
2015-04-14 18:36:35 +03:00
|
|
|
template <typename T0, typename T1, typename T2, typename T3, typename T4>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1, T2, T3, T4>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
typename ::detail::ParameterStorage<T2>::Type m2;
|
|
|
|
typename ::detail::ParameterStorage<T3>::Type m3;
|
|
|
|
typename ::detail::ParameterStorage<T4>::Type m4;
|
|
|
|
template<typename A0, typename A1, typename A2, typename A3, typename A4>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1, A2&& a2, A3&& a3, A4&& a4)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
, m2(mozilla::Forward<A2>(a2))
|
|
|
|
, m3(mozilla::Forward<A3>(a3))
|
|
|
|
, m4(mozilla::Forward<A4>(a4))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter(),
|
|
|
|
m2.PassAsParameter(), m3.PassAsParameter(),
|
|
|
|
m4.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2, typename T3, typename T4,
|
|
|
|
typename T5>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1, T2, T3, T4, T5>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
typename ::detail::ParameterStorage<T2>::Type m2;
|
|
|
|
typename ::detail::ParameterStorage<T3>::Type m3;
|
|
|
|
typename ::detail::ParameterStorage<T4>::Type m4;
|
|
|
|
typename ::detail::ParameterStorage<T5>::Type m5;
|
|
|
|
template<typename A0, typename A1, typename A2, typename A3, typename A4,
|
|
|
|
typename A5>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1, A2&& a2, A3&& a3, A4&& a4,
|
|
|
|
A5&& a5)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
, m2(mozilla::Forward<A2>(a2))
|
|
|
|
, m3(mozilla::Forward<A3>(a3))
|
|
|
|
, m4(mozilla::Forward<A4>(a4))
|
|
|
|
, m5(mozilla::Forward<A5>(a5))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter(),
|
|
|
|
m2.PassAsParameter(), m3.PassAsParameter(),
|
|
|
|
m4.PassAsParameter(), m5.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2, typename T3, typename T4,
|
|
|
|
typename T5, typename T6>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1, T2, T3, T4, T5, T6>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
typename ::detail::ParameterStorage<T2>::Type m2;
|
|
|
|
typename ::detail::ParameterStorage<T3>::Type m3;
|
|
|
|
typename ::detail::ParameterStorage<T4>::Type m4;
|
|
|
|
typename ::detail::ParameterStorage<T5>::Type m5;
|
|
|
|
typename ::detail::ParameterStorage<T6>::Type m6;
|
|
|
|
template<typename A0, typename A1, typename A2, typename A3, typename A4,
|
|
|
|
typename A5, typename A6>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1, A2&& a2, A3&& a3, A4&& a4,
|
|
|
|
A5&& a5, A6&& a6)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
, m2(mozilla::Forward<A2>(a2))
|
|
|
|
, m3(mozilla::Forward<A3>(a3))
|
|
|
|
, m4(mozilla::Forward<A4>(a4))
|
|
|
|
, m5(mozilla::Forward<A5>(a5))
|
|
|
|
, m6(mozilla::Forward<A6>(a6))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter(),
|
|
|
|
m2.PassAsParameter(), m3.PassAsParameter(),
|
|
|
|
m4.PassAsParameter(), m5.PassAsParameter(),
|
|
|
|
m6.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2, typename T3, typename T4,
|
|
|
|
typename T5, typename T6, typename T7>
|
|
|
|
struct nsRunnableMethodArguments<T0, T1, T2, T3, T4, T5, T6, T7>
|
|
|
|
{
|
|
|
|
typename ::detail::ParameterStorage<T0>::Type m0;
|
|
|
|
typename ::detail::ParameterStorage<T1>::Type m1;
|
|
|
|
typename ::detail::ParameterStorage<T2>::Type m2;
|
|
|
|
typename ::detail::ParameterStorage<T3>::Type m3;
|
|
|
|
typename ::detail::ParameterStorage<T4>::Type m4;
|
|
|
|
typename ::detail::ParameterStorage<T5>::Type m5;
|
|
|
|
typename ::detail::ParameterStorage<T6>::Type m6;
|
|
|
|
typename ::detail::ParameterStorage<T7>::Type m7;
|
|
|
|
template<typename A0, typename A1, typename A2, typename A3, typename A4,
|
|
|
|
typename A5, typename A6, typename A7>
|
|
|
|
nsRunnableMethodArguments(A0&& a0, A1&& a1, A2&& a2, A3&& a3, A4&& a4,
|
|
|
|
A5&& a5, A6&& a6, A7&& a7)
|
|
|
|
: m0(mozilla::Forward<A0>(a0))
|
|
|
|
, m1(mozilla::Forward<A1>(a1))
|
|
|
|
, m2(mozilla::Forward<A2>(a2))
|
|
|
|
, m3(mozilla::Forward<A3>(a3))
|
|
|
|
, m4(mozilla::Forward<A4>(a4))
|
|
|
|
, m5(mozilla::Forward<A5>(a5))
|
|
|
|
, m6(mozilla::Forward<A6>(a6))
|
|
|
|
, m7(mozilla::Forward<A7>(a7))
|
|
|
|
{}
|
|
|
|
template<class C, typename M> void apply(C* o, M m)
|
|
|
|
{
|
|
|
|
((*o).*m)(m0.PassAsParameter(), m1.PassAsParameter(),
|
|
|
|
m2.PassAsParameter(), m3.PassAsParameter(),
|
|
|
|
m4.PassAsParameter(), m5.PassAsParameter(),
|
|
|
|
m6.PassAsParameter(), m7.PassAsParameter());
|
|
|
|
}
|
|
|
|
};
|
2013-09-07 05:29:24 +04:00
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template<typename Method, bool Owning, typename... Storages>
|
|
|
|
class nsRunnableMethodImpl
|
2013-09-07 05:29:24 +04:00
|
|
|
: public nsRunnableMethodTraits<Method, Owning>::base_type
|
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
typedef typename nsRunnableMethodTraits<Method, Owning>::class_type
|
|
|
|
ClassType;
|
|
|
|
nsRunnableMethodReceiver<ClassType, Owning> mReceiver;
|
2009-02-18 07:11:09 +03:00
|
|
|
Method mMethod;
|
2015-03-01 20:12:00 +03:00
|
|
|
nsRunnableMethodArguments<Storages...> mArgs;
|
2010-04-21 03:21:32 +04:00
|
|
|
public:
|
2015-03-01 20:12:00 +03:00
|
|
|
virtual ~nsRunnableMethodImpl() { Revoke(); };
|
|
|
|
template<typename... Args>
|
|
|
|
explicit nsRunnableMethodImpl(ClassType* aObj, Method aMethod,
|
|
|
|
Args&&... aArgs)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mReceiver(aObj)
|
|
|
|
, mMethod(aMethod)
|
2015-03-01 20:12:00 +03:00
|
|
|
, mArgs(mozilla::Forward<Args>(aArgs)...)
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args), "Storages and Args should have equal sizes");
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
if (MOZ_LIKELY(mReceiver.Get())) {
|
|
|
|
mArgs.apply(mReceiver.Get(), mMethod);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-04-21 03:21:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
void Revoke() { mReceiver.Revoke(); }
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2010-04-21 03:21:32 +04:00
|
|
|
// Use this template function like so:
|
2006-05-10 21:30:15 +04:00
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
2010-04-21 03:21:32 +04:00
|
|
|
// NS_NewRunnableMethod(myObject, &MyClass::HandleEvent);
|
2006-05-10 21:30:15 +04:00
|
|
|
// NS_DispatchToCurrentThread(event);
|
|
|
|
//
|
2010-04-21 03:21:32 +04:00
|
|
|
// Statically enforced constraints:
|
|
|
|
// - myObject must be of (or implicitly convertible to) type MyClass
|
2006-05-10 21:30:15 +04:00
|
|
|
// - MyClass must defined AddRef and Release methods
|
|
|
|
//
|
2010-04-21 03:21:32 +04:00
|
|
|
template<typename PtrType, typename Method>
|
|
|
|
typename nsRunnableMethodTraits<Method, true>::base_type*
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_NewRunnableMethod(PtrType aPtr, Method aMethod)
|
2010-04-21 03:21:32 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
return new nsRunnableMethodImpl<Method, true>(aPtr, aMethod);
|
2013-09-07 05:29:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template<typename PtrType, typename Method>
|
|
|
|
typename nsRunnableMethodTraits<Method, false>::base_type*
|
|
|
|
NS_NewNonOwningRunnableMethod(PtrType&& aPtr, Method aMethod)
|
2013-09-07 05:29:24 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
return new nsRunnableMethodImpl<Method, false>(aPtr, aMethod);
|
|
|
|
}
|
2013-09-07 05:29:24 +04:00
|
|
|
|
|
|
|
// Similar to NS_NewRunnableMethod. Call like so:
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
|
|
|
// NS_NewRunnableMethodWithArg<Type>(myObject, &MyClass::HandleEvent, myArg);
|
2015-03-01 20:12:00 +03:00
|
|
|
// 'Type' is the stored type for the argument, see ParameterStorage for details.
|
|
|
|
template<typename Storage, typename Method, typename PtrType, typename Arg>
|
2013-09-07 05:29:24 +04:00
|
|
|
typename nsRunnableMethodTraits<Method, true>::base_type*
|
2015-03-01 20:12:00 +03:00
|
|
|
NS_NewRunnableMethodWithArg(PtrType&& aPtr, Method aMethod, Arg&& aArg)
|
2013-09-07 05:29:24 +04:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
return new nsRunnableMethodImpl<Method, true, Storage>(
|
|
|
|
aPtr, aMethod, mozilla::Forward<Arg>(aArg));
|
2010-04-21 03:21:32 +04:00
|
|
|
}
|
2009-02-18 07:11:09 +03:00
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
// Similar to NS_NewRunnableMethod. Call like so:
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
|
|
|
// NS_NewRunnableMethodWithArg<Types,...>(myObject, &MyClass::HandleEvent, myArg1,...);
|
|
|
|
// 'Types' are the stored type for each argument, see ParameterStorage for details.
|
|
|
|
template<typename... Storages, typename Method, typename PtrType, typename... Args>
|
|
|
|
typename nsRunnableMethodTraits<Method, true>::base_type*
|
|
|
|
NS_NewRunnableMethodWithArgs(PtrType&& aPtr, Method aMethod, Args&&... aArgs)
|
|
|
|
{
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
|
|
|
return new nsRunnableMethodImpl<Method, true, Storages...>(
|
|
|
|
aPtr, aMethod, mozilla::Forward<Args>(aArgs)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Storages, typename Method, typename PtrType, typename... Args>
|
2015-03-24 05:28:09 +03:00
|
|
|
typename nsRunnableMethodTraits<Method, false>::base_type*
|
2015-03-01 20:12:00 +03:00
|
|
|
NS_NewNonOwningRunnableMethodWithArgs(PtrType&& aPtr, Method aMethod,
|
|
|
|
Args&&... aArgs)
|
2009-02-18 07:11:09 +03:00
|
|
|
{
|
2015-03-01 20:12:00 +03:00
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
|
|
|
return new nsRunnableMethodImpl<Method, false, Storages...>(
|
|
|
|
aPtr, aMethod, mozilla::Forward<Args>(aArgs)...);
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2006-05-13 00:18:13 +04:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// This class is designed to be used when you have an event class E that has a
|
|
|
|
// pointer back to resource class R. If R goes away while E is still pending,
|
|
|
|
// then it is important to "revoke" E so that it does not try use R after R has
|
|
|
|
// been destroyed. nsRevocableEventPtr makes it easy for R to manage such
|
|
|
|
// situations:
|
|
|
|
//
|
|
|
|
// class R;
|
|
|
|
//
|
|
|
|
// class E : public nsRunnable {
|
|
|
|
// public:
|
|
|
|
// void Revoke() {
|
2012-07-30 18:20:58 +04:00
|
|
|
// mResource = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
// }
|
|
|
|
// private:
|
|
|
|
// R *mResource;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// class R {
|
|
|
|
// public:
|
|
|
|
// void EventHandled() {
|
|
|
|
// mEvent.Forget();
|
|
|
|
// }
|
|
|
|
// private:
|
|
|
|
// nsRevocableEventPtr<E> mEvent;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// void R::PostEvent() {
|
|
|
|
// // Make sure any pending event is revoked.
|
|
|
|
// mEvent->Revoke();
|
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event = new E();
|
|
|
|
// if (NS_SUCCEEDED(NS_DispatchToCurrentThread(event))) {
|
|
|
|
// // Keep pointer to event so we can revoke it.
|
|
|
|
// mEvent = event;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// NS_IMETHODIMP E::Run() {
|
|
|
|
// if (!mResource)
|
|
|
|
// return NS_OK;
|
|
|
|
// ...
|
|
|
|
// mResource->EventHandled();
|
|
|
|
// return NS_OK;
|
|
|
|
// }
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T>
|
|
|
|
class nsRevocableEventPtr
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
nsRevocableEventPtr() : mEvent(nullptr) {}
|
|
|
|
~nsRevocableEventPtr() { Revoke(); }
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
const nsRevocableEventPtr& operator=(T* aEvent)
|
|
|
|
{
|
|
|
|
if (mEvent != aEvent) {
|
2010-05-11 05:05:40 +04:00
|
|
|
Revoke();
|
2014-06-27 05:35:39 +04:00
|
|
|
mEvent = aEvent;
|
2010-05-11 05:05:40 +04:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void Revoke()
|
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
if (mEvent) {
|
|
|
|
mEvent->Revoke();
|
2012-07-30 18:20:58 +04:00
|
|
|
mEvent = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void Forget() { mEvent = nullptr; }
|
|
|
|
bool IsPending() { return mEvent != nullptr; }
|
|
|
|
T* get() { return mEvent; }
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Not implemented
|
|
|
|
nsRevocableEventPtr(const nsRevocableEventPtr&);
|
|
|
|
nsRevocableEventPtr& operator=(const nsRevocableEventPtr&);
|
|
|
|
|
2010-05-11 05:05:40 +04:00
|
|
|
nsRefPtr<T> mEvent;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
/**
|
|
|
|
* A simple helper to suffix thread pool name
|
|
|
|
* with incremental numbers.
|
|
|
|
*/
|
|
|
|
class nsThreadPoolNaming
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsThreadPoolNaming() : mCounter(0) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates and sets next thread name as "<aPoolName> #<n>"
|
|
|
|
* on the specified thread. If no thread is specified (aThread
|
|
|
|
* is null) then the name is synchronously set on the current thread.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
void SetThreadPoolName(const nsACString& aPoolName,
|
|
|
|
nsIThread* aThread = nullptr);
|
2012-06-12 21:06:20 +04:00
|
|
|
|
|
|
|
private:
|
2015-03-10 21:41:45 +03:00
|
|
|
mozilla::Atomic<uint32_t> mCounter;
|
2012-06-12 21:06:20 +04:00
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
nsThreadPoolNaming(const nsThreadPoolNaming&) = delete;
|
|
|
|
void operator=(const nsThreadPoolNaming&) = delete;
|
2012-06-12 21:06:20 +04:00
|
|
|
};
|
|
|
|
|
2012-07-25 18:25:08 +04:00
|
|
|
/**
|
|
|
|
* Thread priority in most operating systems affect scheduling, not IO. This
|
|
|
|
* helper is used to set the current thread to low IO priority for the lifetime
|
|
|
|
* of the created object. You can only use this low priority IO setting within
|
|
|
|
* the context of the current thread.
|
|
|
|
*/
|
2013-04-12 07:21:40 +04:00
|
|
|
class MOZ_STACK_CLASS nsAutoLowPriorityIO
|
2012-07-25 18:25:08 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAutoLowPriorityIO();
|
|
|
|
~nsAutoLowPriorityIO();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool lowIOPrioritySet;
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
int oldPriority;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-07-04 14:34:15 +04:00
|
|
|
void
|
|
|
|
NS_SetMainThread();
|
2012-07-25 18:25:08 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif // nsThreadUtils_h__
|