2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsThreadUtils_h__
|
|
|
|
#define nsThreadUtils_h__
|
|
|
|
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "prinrval.h"
|
2013-09-19 06:54:41 -07:00
|
|
|
#include "MainThreadUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIThreadManager.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsIRunnable.h"
|
2012-12-11 10:13:29 -08:00
|
|
|
#include "nsICancelableRunnable.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2010-05-10 18:05:40 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2012-10-26 06:32:10 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// These methods are alternatives to the methods on nsIThreadManager, provided
|
|
|
|
// for convenience.
|
|
|
|
|
2012-06-12 10:06:20 -07:00
|
|
|
/**
|
|
|
|
* Set name of the target thread. This operation is asynchronous.
|
|
|
|
*/
|
|
|
|
extern NS_COM_GLUE void
|
|
|
|
NS_SetThreadName(nsIThread *thread, const nsACString &name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static length version of the above function checking length of the
|
|
|
|
* name at compile time.
|
|
|
|
*/
|
|
|
|
template <size_t LEN>
|
|
|
|
inline NS_COM_GLUE void
|
|
|
|
NS_SetThreadName(nsIThread *thread, const char (&name)[LEN])
|
|
|
|
{
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(LEN <= 16,
|
|
|
|
"Thread name must be no more than 16 characters");
|
2012-06-12 10:06:20 -07:00
|
|
|
NS_SetThreadName(thread, nsDependentCString(name));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Create a new thread, and optionally provide an initial event for the thread.
|
|
|
|
*
|
|
|
|
* @param result
|
|
|
|
* The resulting nsIThread object.
|
|
|
|
* @param initialEvent
|
|
|
|
* The initial event to run on this thread. This parameter may be null.
|
2011-07-26 20:26:47 -07:00
|
|
|
* @param stackSize
|
|
|
|
* The size in bytes to reserve for the thread's stack.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* Indicates that the given name is not unique.
|
|
|
|
*/
|
|
|
|
extern NS_COM_GLUE NS_METHOD
|
2011-07-26 20:26:47 -07:00
|
|
|
NS_NewThread(nsIThread **result,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIRunnable *initialEvent = nullptr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t stackSize = nsIThreadManager::DEFAULT_STACK_SIZE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 10:06:20 -07:00
|
|
|
/**
|
|
|
|
* Creates a named thread, otherwise the same as NS_NewThread
|
|
|
|
*/
|
|
|
|
template <size_t LEN>
|
|
|
|
inline NS_METHOD
|
|
|
|
NS_NewNamedThread(const char (&name)[LEN],
|
|
|
|
nsIThread **result,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIRunnable *initialEvent = nullptr,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t stackSize = nsIThreadManager::DEFAULT_STACK_SIZE)
|
2012-06-12 10:06:20 -07:00
|
|
|
{
|
2013-08-26 17:36:45 -07:00
|
|
|
nsresult rv = NS_NewThread(result, nullptr, stackSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_SetThreadName<LEN>(*result, name);
|
|
|
|
if (initialEvent) {
|
|
|
|
rv = (*result)->Dispatch(initialEvent, NS_DISPATCH_NORMAL);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Initial event dispatch failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2012-06-12 10:06:20 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Get a reference to the current thread.
|
|
|
|
*
|
|
|
|
* @param result
|
|
|
|
* The resulting nsIThread object.
|
|
|
|
*/
|
|
|
|
extern NS_COM_GLUE NS_METHOD
|
|
|
|
NS_GetCurrentThread(nsIThread **result);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event to the current thread.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event to dispatch.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
*/
|
|
|
|
extern NS_COM_GLUE NS_METHOD
|
|
|
|
NS_DispatchToCurrentThread(nsIRunnable *event);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event to the main thread.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
* The event to dispatch.
|
|
|
|
* @param dispatchFlags
|
|
|
|
* The flags to pass to the main thread's dispatch method.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
*/
|
|
|
|
extern NS_COM_GLUE NS_METHOD
|
|
|
|
NS_DispatchToMainThread(nsIRunnable *event,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dispatchFlags = NS_DISPATCH_NORMAL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-02 08:29:41 -07:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
*
|
|
|
|
* @param thread
|
|
|
|
* The thread object for which to process pending events. If null, then
|
|
|
|
* events will be processed for the current thread.
|
|
|
|
* @param timeout
|
|
|
|
* 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 NS_COM_GLUE NS_METHOD
|
|
|
|
NS_ProcessPendingEvents(nsIThread *thread,
|
|
|
|
PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT);
|
2007-04-02 08:29:41 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for nsIThread::HasPendingEvents.
|
|
|
|
*
|
|
|
|
* It is an error to call this function when the given thread is not the
|
2011-10-17 07:59:28 -07:00
|
|
|
* current thread. This function will return false if called from some
|
2007-03-22 10:30:00 -07:00
|
|
|
* other thread.
|
|
|
|
*
|
|
|
|
* @param thread
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
extern NS_COM_GLUE bool
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_HasPendingEvents(nsIThread *thread = nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for nsIThread::ProcessNextEvent.
|
|
|
|
*
|
|
|
|
* It is an error to call this function when the given thread is not the
|
2011-10-17 07:59:28 -07:00
|
|
|
* current thread. This function will simply return false if called
|
2007-03-22 10:30:00 -07:00
|
|
|
* from some other thread.
|
|
|
|
*
|
|
|
|
* @param thread
|
|
|
|
* The current thread or null.
|
|
|
|
* @param mayWait
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
extern NS_COM_GLUE bool
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ProcessNextEvent(nsIThread *thread = nullptr, bool mayWait = true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Helpers that work with nsCOMPtr:
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIThread>
|
|
|
|
do_GetCurrentThread() {
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIThread *thread = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_GetCurrentThread(&thread);
|
|
|
|
return already_AddRefed<nsIThread>(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIThread>
|
|
|
|
do_GetMainThread() {
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIThread *thread = nullptr;
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
extern NS_COM_GLUE nsIThread *NS_GetCurrentThread();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
|
|
|
#undef IMETHOD_VISIBILITY
|
2007-04-02 08:29:41 -07:00
|
|
|
#define IMETHOD_VISIBILITY NS_COM_GLUE
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This class is designed to be subclassed.
|
|
|
|
class NS_COM_GLUE nsRunnable : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
|
|
|
nsRunnable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsRunnable() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-11 10:13:29 -08:00
|
|
|
// This class is designed to be subclassed.
|
|
|
|
class NS_COM_GLUE nsCancelableRunnable : public nsICancelableRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-12-11 10:13:29 -08:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSICANCELABLERUNNABLE
|
|
|
|
|
|
|
|
nsCancelableRunnable() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsCancelableRunnable() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#undef IMETHOD_VISIBILITY
|
|
|
|
#define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
|
|
|
|
|
|
|
|
// An event that can be used to call a method on a class. The class type must
|
2008-08-17 19:10:28 -07:00
|
|
|
// support reference counting. This event supports Revoke for use
|
|
|
|
// with nsRevocableEventPtr.
|
2010-04-20 10:22:04 -07:00
|
|
|
template <class ClassType,
|
|
|
|
typename ReturnType = void,
|
|
|
|
bool Owning = true>
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsRunnableMethod : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2010-04-20 16:21:32 -07:00
|
|
|
virtual void Revoke() = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-17 20:11:09 -08: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.
|
|
|
|
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;
|
2010-04-20 16:21:32 -07:00
|
|
|
};
|
2009-02-17 20:11:09 -08:00
|
|
|
|
2013-09-06 18:29:24 -07:00
|
|
|
template <class ClassType, typename Arg, bool Owning>
|
2010-04-20 16:21:32 -07:00
|
|
|
struct nsRunnableMethodReceiver {
|
|
|
|
ClassType *mObj;
|
2013-09-06 18:29:24 -07:00
|
|
|
Arg mArg;
|
|
|
|
nsRunnableMethodReceiver(ClassType *obj, Arg arg)
|
|
|
|
: mObj(obj)
|
|
|
|
, mArg(arg)
|
|
|
|
{ NS_IF_ADDREF(mObj); }
|
|
|
|
~nsRunnableMethodReceiver() { Revoke(); }
|
|
|
|
void Revoke() { NS_IF_RELEASE(mObj); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ClassType, bool Owning>
|
|
|
|
struct nsRunnableMethodReceiver<ClassType, void, Owning> {
|
|
|
|
ClassType *mObj;
|
|
|
|
nsRunnableMethodReceiver(ClassType *obj) : mObj(obj)
|
|
|
|
{ NS_IF_ADDREF(mObj); }
|
|
|
|
~nsRunnableMethodReceiver() { Revoke(); }
|
2010-04-20 16:21:32 -07:00
|
|
|
void Revoke() { NS_IF_RELEASE(mObj); }
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-20 16:21:32 -07:00
|
|
|
template <class ClassType>
|
2013-09-06 18:29:24 -07:00
|
|
|
struct nsRunnableMethodReceiver<ClassType, void, false> {
|
2010-04-20 16:21:32 -07:00
|
|
|
ClassType *mObj;
|
|
|
|
nsRunnableMethodReceiver(ClassType *obj) : mObj(obj) {}
|
2012-07-30 07:20:58 -07:00
|
|
|
void Revoke() { mObj = nullptr; }
|
2010-04-20 16:21:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Method, bool Owning> struct nsRunnableMethodTraits;
|
|
|
|
|
2013-09-06 18:29:24 -07:00
|
|
|
template <class C, typename R, typename A, bool Owning>
|
|
|
|
struct nsRunnableMethodTraits<R (C::*)(A), Owning> {
|
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
|
|
|
typedef A arg_type;
|
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
|
2010-04-20 16:21:32 -07:00
|
|
|
template <class C, typename R, bool Owning>
|
|
|
|
struct nsRunnableMethodTraits<R (C::*)(), Owning> {
|
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
2013-09-06 18:29:24 -07:00
|
|
|
typedef void arg_type;
|
2010-04-20 16:21:32 -07:00
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
|
2013-07-16 19:01:50 -07:00
|
|
|
#ifdef NS_HAVE_STDCALL
|
2013-09-06 18:29:24 -07:00
|
|
|
template <class C, typename R, typename A, bool Owning>
|
|
|
|
struct nsRunnableMethodTraits<R (__stdcall C::*)(A), Owning> {
|
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
|
|
|
typedef A arg_type;
|
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
|
2010-04-20 16:21:32 -07:00
|
|
|
template <class C, typename R, bool Owning>
|
2013-07-16 19:01:50 -07:00
|
|
|
struct nsRunnableMethodTraits<R (NS_STDCALL C::*)(), Owning> {
|
2010-04-20 16:21:32 -07:00
|
|
|
typedef C class_type;
|
|
|
|
typedef R return_type;
|
2013-09-06 18:29:24 -07:00
|
|
|
typedef void arg_type;
|
2010-04-20 16:21:32 -07:00
|
|
|
typedef nsRunnableMethod<C, R, Owning> base_type;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-09-06 18:29:24 -07:00
|
|
|
template <typename Method, typename Arg, bool Owning>
|
2010-04-20 16:21:32 -07:00
|
|
|
class nsRunnableMethodImpl
|
|
|
|
: public nsRunnableMethodTraits<Method, Owning>::base_type
|
|
|
|
{
|
|
|
|
typedef typename nsRunnableMethodTraits<Method, Owning>::class_type ClassType;
|
2013-09-06 18:29:24 -07:00
|
|
|
nsRunnableMethodReceiver<ClassType, Arg, Owning> mReceiver;
|
|
|
|
Method mMethod;
|
|
|
|
public:
|
|
|
|
nsRunnableMethodImpl(ClassType *obj,
|
|
|
|
Method method,
|
|
|
|
Arg arg)
|
|
|
|
: mReceiver(obj, arg)
|
|
|
|
, mMethod(method)
|
|
|
|
{}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
if (MOZ_LIKELY(mReceiver.mObj))
|
|
|
|
((*mReceiver.mObj).*mMethod)(mReceiver.mArg);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
void Revoke() {
|
|
|
|
mReceiver.Revoke();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Method, bool Owning>
|
|
|
|
class nsRunnableMethodImpl<Method, void, Owning>
|
|
|
|
: public nsRunnableMethodTraits<Method, Owning>::base_type
|
|
|
|
{
|
|
|
|
typedef typename nsRunnableMethodTraits<Method, Owning>::class_type ClassType;
|
|
|
|
nsRunnableMethodReceiver<ClassType, void, Owning> mReceiver;
|
2009-02-17 20:11:09 -08:00
|
|
|
Method mMethod;
|
2010-04-20 16:21:32 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
nsRunnableMethodImpl(ClassType *obj,
|
|
|
|
Method method)
|
|
|
|
: mReceiver(obj)
|
|
|
|
, mMethod(method)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_LIKELY(mReceiver.mObj))
|
2010-04-20 16:21:32 -07:00
|
|
|
((*mReceiver.mObj).*mMethod)();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Revoke() {
|
|
|
|
mReceiver.Revoke();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-04-20 16:21:32 -07:00
|
|
|
// Use this template function like so:
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
2010-04-20 16:21:32 -07:00
|
|
|
// NS_NewRunnableMethod(myObject, &MyClass::HandleEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
// NS_DispatchToCurrentThread(event);
|
|
|
|
//
|
2010-04-20 16:21:32 -07:00
|
|
|
// Statically enforced constraints:
|
|
|
|
// - myObject must be of (or implicitly convertible to) type MyClass
|
2007-03-22 10:30:00 -07:00
|
|
|
// - MyClass must defined AddRef and Release methods
|
|
|
|
//
|
2010-04-20 16:21:32 -07:00
|
|
|
template<typename PtrType, typename Method>
|
|
|
|
typename nsRunnableMethodTraits<Method, true>::base_type*
|
|
|
|
NS_NewRunnableMethod(PtrType ptr, Method method)
|
|
|
|
{
|
2013-09-06 18:29:24 -07:00
|
|
|
return new nsRunnableMethodImpl<Method, void, true>(ptr, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct dependent_type
|
|
|
|
{
|
|
|
|
typedef T type;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Similar to NS_NewRunnableMethod. Call like so:
|
|
|
|
// Type myArg;
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
|
|
|
// NS_NewRunnableMethodWithArg<Type>(myObject, &MyClass::HandleEvent, myArg);
|
|
|
|
template<typename Arg, typename Method, typename PtrType>
|
|
|
|
typename nsRunnableMethodTraits<Method, true>::base_type*
|
|
|
|
NS_NewRunnableMethodWithArg(PtrType ptr, Method method, typename dependent_type<Arg>::type arg)
|
|
|
|
{
|
|
|
|
return new nsRunnableMethodImpl<Method, Arg, true>(ptr, method, arg);
|
2010-04-20 16:21:32 -07:00
|
|
|
}
|
2009-02-17 20:11:09 -08:00
|
|
|
|
2010-04-20 16:21:32 -07:00
|
|
|
template<typename PtrType, typename Method>
|
|
|
|
typename nsRunnableMethodTraits<Method, false>::base_type*
|
|
|
|
NS_NewNonOwningRunnableMethod(PtrType ptr, Method method)
|
2009-02-17 20:11:09 -08:00
|
|
|
{
|
2013-09-06 18:29:24 -07:00
|
|
|
return new nsRunnableMethodImpl<Method, void, false>(ptr, method);
|
2009-02-17 20:11:09 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
|
|
|
// 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 07:20:58 -07:00
|
|
|
// mResource = nullptr;
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
template <class T>
|
|
|
|
class nsRevocableEventPtr {
|
|
|
|
public:
|
|
|
|
nsRevocableEventPtr()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mEvent(nullptr) {
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~nsRevocableEventPtr() {
|
|
|
|
Revoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsRevocableEventPtr& operator=(T *event) {
|
2010-05-10 18:05:40 -07:00
|
|
|
if (mEvent != event) {
|
|
|
|
Revoke();
|
|
|
|
mEvent = event;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Revoke() {
|
|
|
|
if (mEvent) {
|
|
|
|
mEvent->Revoke();
|
2012-07-30 07:20:58 -07:00
|
|
|
mEvent = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Forget() {
|
2012-07-30 07:20:58 -07:00
|
|
|
mEvent = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPending() {
|
2012-07-30 07:20:58 -07:00
|
|
|
return mEvent != nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-08-17 19:10:28 -07:00
|
|
|
|
|
|
|
T *get() { return mEvent; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Not implemented
|
|
|
|
nsRevocableEventPtr(const nsRevocableEventPtr&);
|
|
|
|
nsRevocableEventPtr& operator=(const nsRevocableEventPtr&);
|
|
|
|
|
2010-05-10 18:05:40 -07:00
|
|
|
nsRefPtr<T> mEvent;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2012-06-12 10:06:20 -07: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.
|
|
|
|
*/
|
|
|
|
void SetThreadPoolName(const nsACString & aPoolName,
|
2012-07-30 07:20:58 -07:00
|
|
|
nsIThread * aThread = nullptr);
|
2012-06-12 10:06:20 -07:00
|
|
|
|
|
|
|
private:
|
2012-08-22 08:56:38 -07:00
|
|
|
volatile uint32_t mCounter;
|
2012-06-12 10:06:20 -07:00
|
|
|
|
|
|
|
nsThreadPoolNaming(const nsThreadPoolNaming &) MOZ_DELETE;
|
|
|
|
void operator=(const nsThreadPoolNaming &) MOZ_DELETE;
|
|
|
|
};
|
|
|
|
|
2012-07-25 07:25:08 -07: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-11 20:21:40 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoLowPriorityIO
|
2012-07-25 07:25:08 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAutoLowPriorityIO();
|
|
|
|
~nsAutoLowPriorityIO();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool lowIOPrioritySet;
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
int oldPriority;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif // nsThreadUtils_h__
|