27 #ifndef __SB_THREAD_UTILS_H__
28 #define __SB_THREAD_UTILS_H__
50 #include <mozilla/Monitor.h>
51 #include <nsAutoLock.h>
52 #include <nsAutoPtr.h>
53 #include <nsIThreadPool.h>
54 #include <nsThreadUtils.h>
73 template <
class ClassType,
typename ReturnType,
typename Arg1Type>
90 typedef ReturnType (ClassType::*
MethodType)(Arg1Type aArg1Value);
109 nsAutoLock autoLock(
mLock);
136 ReturnType aFailureReturnValue,
140 NS_ENSURE_ARG_POINTER(aRunnable);
141 NS_ENSURE_ARG_POINTER(aObject);
142 NS_ENSURE_ARG_POINTER(aMethod);
148 nsRefPtr<SelfType> runnable =
new SelfType(aObject,
152 NS_ENSURE_TRUE(runnable, aFailureReturnValue);
155 rv = runnable->Initialize();
156 NS_ENSURE_SUCCESS(rv, rv);
159 runnable.forget(aRunnable);
182 ReturnType aFailureReturnValue,
188 nsRefPtr<SelfType> runnable;
189 rv =
New(getter_AddRefs(runnable),
194 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
197 rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
198 NS_ENSURE_SUCCESS(rv, rv);
200 return runnable->GetReturnValue();
221 ReturnType aFailureReturnValue,
223 nsIEventTarget* aThread)
228 nsRefPtr<SelfType> runnable;
229 rv =
New(getter_AddRefs(runnable),
234 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
237 rv = aThread->Dispatch(runnable, NS_DISPATCH_SYNC);
238 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
240 return runnable->GetReturnValue();
259 ReturnType aFailureReturnValue,
265 nsRefPtr<SelfType> runnable;
266 rv =
New(getter_AddRefs(runnable),
271 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
274 rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL);
275 NS_ENSURE_SUCCESS(rv, rv);
296 ReturnType aFailureReturnValue,
298 nsIEventTarget* aThread)
303 nsRefPtr<SelfType> runnable;
304 rv =
New(getter_AddRefs(runnable),
309 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
312 rv = aThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
313 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
327 nsAutoLock autoLock(
mLock);
356 ReturnType aFailureReturnValue,
357 Arg1Type aArg1Value) :
376 nsAutoLock::DestroyLock(
mLock);
387 mLock = nsAutoLock::NewLock(
"sbRunnableMethod1::mLock");
388 NS_ENSURE_TRUE(
mLock, NS_ERROR_OUT_OF_MEMORY);
416 template <
class ClassType,
439 typedef ReturnType (ClassType::*
MethodType)(Arg1Type aArg1Value,
440 Arg2Type aArg2Value);
486 ReturnType aFailureReturnValue,
491 NS_ENSURE_ARG_POINTER(aRunnable);
492 NS_ENSURE_ARG_POINTER(aObject);
493 NS_ENSURE_ARG_POINTER(aMethod);
499 nsRefPtr<SelfType> runnable =
new SelfType(aObject,
504 NS_ENSURE_TRUE(runnable, aFailureReturnValue);
507 rv = runnable->Initialize();
508 NS_ENSURE_SUCCESS(rv, rv);
511 runnable.forget(aRunnable);
535 ReturnType aFailureReturnValue,
542 nsRefPtr<SelfType> runnable;
543 rv =
New(getter_AddRefs(runnable),
549 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
552 rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
553 NS_ENSURE_SUCCESS(rv, rv);
555 return runnable->GetReturnValue();
577 ReturnType aFailureReturnValue,
580 nsIEventTarget* aThread)
585 nsRefPtr<SelfType> runnable;
586 rv =
New(getter_AddRefs(runnable),
592 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
595 rv = aThread->Dispatch(runnable, NS_DISPATCH_SYNC);
596 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
598 return runnable->GetReturnValue();
617 ReturnType aFailureReturnValue,
624 nsRefPtr<SelfType> runnable;
625 rv =
New(getter_AddRefs(runnable),
631 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
634 rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL);
635 NS_ENSURE_SUCCESS(rv, rv);
657 ReturnType aFailureReturnValue,
660 nsIEventTarget* aThread)
665 nsRefPtr<SelfType> runnable;
666 rv =
New(getter_AddRefs(runnable),
672 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
675 rv = aThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
676 NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
705 ReturnType aFailureReturnValue,
707 Arg2Type aArg2Value) :
708 BaseType(aObject, nsnull, aFailureReturnValue, aArg1Value),
743 const char *
aName) :
744 mMonitor(aName ? aName :
"sbRunnable"),
761 PRBool
Wait(PRIntervalTime aTimeout);
764 mozilla::Monitor mMonitor;
773 template <
typename ResultType>
781 const char *
aName) :
804 Wait(PR_INTERVAL_NO_TIMEOUT);
815 Wait(PRIntervalTime aTimeout, ResultType & aResult)
817 if (!
Wait(aTimeout)) {
830 virtual ResultType
OnRun() = 0;
846 const char *
aName) :
867 virtual void OnRun() = 0;
878 template <
typename ResultType,
885 TargetType & aTarget,
887 const char *
aName) :
899 virtual ResultType
OnRun() = 0;
916 template <
typename ResultType,
919 typename Arg1Type = Param1Type,
920 typename MethodType = ResultType (TargetType::*) (Param1Type)>
932 TargetType & aTarget,
935 const char *
aName = NULL) :
936 BaseType(aTarget, aMethod,
aName),
949 return (BaseType::mTarget->*BaseType::mMethod)(mArg1);
968 template <
typename ResultType,
972 typename Arg1Type = Param1Type,
973 typename Arg2Type = Param2Type,
974 typename MethodType = ResultType (TargetType::*) (Param1Type,
987 TargetType & aTarget,
991 const char *
aName = NULL) :
992 BaseType(aTarget, aMethod,
aName),
1006 return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2);
1026 template <
typename ResultType,
1027 typename TargetType,
1028 typename Param1Type,
1029 typename Param2Type,
1030 typename Param3Type,
1031 typename Arg1Type = Param1Type,
1032 typename Arg2Type = Param2Type,
1033 typename Arg3Type = Param3Type,
1034 typename MethodType = ResultType (TargetType::*) (Param1Type,
1048 TargetType & aTarget,
1053 const char *
aName = NULL) :
1054 BaseType(aTarget, aMethod,
aName),
1069 return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2, mArg3);
1090 template <
typename ResultType,
1091 typename TargetType,
1092 typename Param1Type,
1093 typename Param2Type,
1094 typename Param3Type,
1095 typename Param4Type,
1096 typename Arg1Type = Param1Type,
1097 typename Arg2Type = Param2Type,
1098 typename Arg3Type = Param3Type,
1099 typename Arg4Type = Param4Type,
1100 typename MethodType = ResultType (TargetType::*) (Param1Type,
1115 TargetType & aTarget,
1121 const char *
aName = NULL) :
1122 BaseType(aTarget, aMethod,
aName),
1138 return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2, mArg3, mArg4);
1160 template <
typename ResultType,
1161 typename TargetType,
1162 typename Param1Type,
1163 typename Param2Type,
1164 typename Param3Type,
1165 typename Param4Type,
1166 typename Param5Type,
1167 typename Arg1Type = Param1Type,
1168 typename Arg2Type = Param2Type,
1169 typename Arg3Type = Param3Type,
1170 typename Arg4Type = Param4Type,
1171 typename Arg5Type = Param5Type,
1172 typename MethodType = ResultType (TargetType::*) (Param1Type,
1188 TargetType & aTarget,
1195 const char *
aName = NULL) :
1196 BaseType(aTarget, aMethod,
aName),
1213 return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2, mArg3, mArg4, mArg5);
1252 #define SB_INVOKE_ON_MAIN_THREAD1(aClassType, \
1256 aFailureReturnValue, \
1259 sbRunnableMethod1<aClassType, aReturnType, aArg1Type> \
1260 ::InvokeOnMainThread(aObject, \
1261 &aClassType::aMethod, \
1262 aFailureReturnValue, \
1279 template <
class T,
class MT,
class RT,
class A1>
1283 RT aFailureReturnValue,
1288 aFailureReturnValue,
1307 template <
class T,
class MT,
class RT,
class A1,
class TH>
1311 RT aFailureReturnValue,
1317 aFailureReturnValue,
1335 template <
class T,
class MT,
class RT,
class A1>
1339 RT aFailureReturnValue,
1345 aFailureReturnValue,
1363 template <
class T,
class MT,
class RT,
class A1,
class TH>
1367 RT aFailureReturnValue,
1373 aFailureReturnValue,
1378 #define SB_INVOKE_ON_MAIN_THREAD2(aClassType, \
1382 aFailureReturnValue, \
1387 sbRunnableMethod2<aClassType, aReturnType, aArg1Type, aArg2Type> \
1388 ::InvokeOnMainThread(aObject, \
1389 &aClassType::aMethod, \
1390 aFailureReturnValue, \
1408 template <
class T,
class MT,
class RT,
class A1,
class A2>
1412 RT aFailureReturnValue,
1419 aFailureReturnValue,
1439 template <
class T,
class MT,
class RT,
class A1,
class A2,
class TH>
1443 RT aFailureReturnValue,
1450 aFailureReturnValue,
1471 template <
class T,
class MT,
class RT,
class A1,
class A2>
1475 RT aFailureReturnValue,
1482 aFailureReturnValue,
1503 template <
class T,
class MT,
class RT,
class A1,
class A2,
class TH>
1507 RT aFailureReturnValue,
1515 aFailureReturnValue,
1541 #define SB_INVOKE_ON_MAIN_THREAD_ASYNC1(aClassType, \
1545 aFailureReturnValue, \
1548 sbRunnableMethod1<aClassType, aReturnType, aArg1Type> \
1549 ::InvokeOnMainThreadAsync(aObject, \
1550 &aClassType::aMethod, \
1551 aFailureReturnValue, \
1554 #define SB_INVOKE_ON_MAIN_THREAD_ASYNC2(aClassType, \
1558 aFailureReturnValue, \
1563 sbRunnableMethod2<aClassType, aReturnType, aArg1Type, aArg2Type> \
1564 ::InvokeOnMainThreadAsync(aObject, \
1565 &aClassType::aMethod, \
1566 aFailureReturnValue, \
1589 #endif // __SB_THREAD_UTILS_H__
virtual ResultType OnRun()
ReturnType GetReturnValue()
nsresult sbInvokeOnMainThread1Async(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1)
RT sbInvokeOnThread2(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, A2 aArg2, TH aThread)
sbRunnable_(const char *aName)
sbRunnable(const char *aName)
sbRunnableMethod2_(TargetType &aTarget, MethodType aMethod, Param1Type aArg1, Param2Type aArg2, const char *aName=NULL)
virtual ResultType OnRun()
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
static ReturnType InvokeOnMainThread(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value)
sbRunnableMethod2< ClassType, ReturnType, Arg1Type, Arg2Type > SelfType
static ReturnType InvokeOnThread(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, nsIEventTarget *aThread)
PRBool SB_IsMainThread(nsIThreadManager *aThreadManager=nsnull)
sbRunnableMethod2(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value)
static nsresult InvokeOnMainThreadAsync(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value)
nsresult sbInvokeOnMainThread2Async(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, A2 aArg2)
virtual ~sbRunnableMethod1()
PRBool Wait(PRIntervalTime aTimeout)
static nsresult InvokeOnThreadAsync(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value, nsIEventTarget *aThread)
static nsresult InvokeOnMainThreadAsync(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value)
sbRunnableMethod4_(TargetType &aTarget, MethodType aMethod, Param1Type aArg1, Param2Type aArg2, Param3Type aArg3, Param4Type aArg4, const char *aName=NULL)
sbRunnableMethod_(TargetType &aTarget, MethodType aMethod, const char *aName)
static nsresult New(SelfType **aRunnable, ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value)
ReturnType(ClassType::* MethodType)(Arg1Type aArg1Value)
PRBool Wait(PRIntervalTime aTimeout, ResultType &aResult)
sbRunnableMethod1< ClassType, ReturnType, Arg1Type > SelfType
virtual ResultType OnRun()
virtual ResultType OnRun()
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
virtual ResultType OnRun()=0
virtual ResultType OnRun()=0
sbRunnableMethod3_(TargetType &aTarget, MethodType aMethod, Param1Type aArg1, Param2Type aArg2, Param3Type aArg3, const char *aName=NULL)
nsresult sbInvokeOnThread1Async(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, TH aThread)
RT sbInvokeOnThread1(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, TH aThread)
nsresult sbInvokeOnThread2Async(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, A2 aArg2, TH aThread)
static ReturnType InvokeOnMainThread(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value)
sbRunnableMethod1_(TargetType &aTarget, MethodType aMethod, Param1Type aArg1, const char *aName=NULL)
virtual ~sbRunnableMethod2()
static nsresult New(SelfType **aRunnable, ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value)
sbRunnable_(const char *aName)
static ReturnType InvokeOnThread(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value, nsIEventTarget *aThread)
sbRunnableMethod1(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value)
nsRefPtr< TargetType > mTarget
ReturnType(ClassType::* MethodType)(Arg1Type aArg1Value, Arg2Type aArg2Value)
RT sbInvokeOnMainThread2(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, A2 aArg2)
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
ReturnType mFailureReturnValue
sbRunnableMethod5_(TargetType &aTarget, MethodType aMethod, Param1Type aArg1, Param2Type aArg2, Param3Type aArg3, Param4Type aArg4, Param5Type aArg5, const char *aName=NULL)
nsRefPtr< ClassType > mObject
sbRunnableMethod1< ClassType, ReturnType, Arg1Type > BaseType
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
RT sbInvokeOnMainThread1(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1)
static nsresult InvokeOnThreadAsync(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, nsIEventTarget *aThread)
virtual ResultType OnRun()