sbThreadUtils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #ifndef __SB_THREAD_UTILS_H__
28 #define __SB_THREAD_UTILS_H__
29 
30 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
32 //
33 // Songbird thread utilities defs.
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
43 //------------------------------------------------------------------------------
44 //
45 // Songbird thread utilities imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Mozilla imports.
50 #include <mozilla/Monitor.h>
51 #include <nsAutoLock.h>
52 #include <nsAutoPtr.h>
53 #include <nsIThreadPool.h>
54 #include <nsThreadUtils.h>
55 
56 //------------------------------------------------------------------------------
57 //
58 // Songbird thread utilities classes.
59 //
60 //------------------------------------------------------------------------------
61 
73 template <class ClassType, typename ReturnType, typename Arg1Type>
74 class sbRunnableMethod1 : public nsRunnable
75 {
76  //----------------------------------------------------------------------------
77  //
78  // Public interface.
79  //
80  //----------------------------------------------------------------------------
81 
82 public:
83 
84  //
85  // SelfType Type for this class.
86  // MethodType Type of method to invoke.
87  //
88 
90  typedef ReturnType (ClassType::*MethodType)(Arg1Type aArg1Value);
91 
92 
97  NS_IMETHOD Run()
98  {
99  // Do nothing if no object was provided.
100  if (!mObject)
101  return NS_OK;
102 
103  // Ensure lock is available.
104  NS_ENSURE_TRUE(mLock, mFailureReturnValue);
105 
106  // Invoke method.
107  ReturnType returnValue = (mObject->*mMethod)(mArg1Value);
108  {
109  nsAutoLock autoLock(mLock);
110  mReturnValue = returnValue;
111  }
112 
113  return NS_OK;
114  }
115 
116 
133  static nsresult New(SelfType** aRunnable,
134  ClassType* aObject,
135  MethodType aMethod,
136  ReturnType aFailureReturnValue,
137  Arg1Type aArg1Value)
138  {
139  // Validate arguments.
140  NS_ENSURE_ARG_POINTER(aRunnable);
141  NS_ENSURE_ARG_POINTER(aObject);
142  NS_ENSURE_ARG_POINTER(aMethod);
143 
144  // Function variables.
145  nsresult rv;
146 
147  // Create a Songbird runnable method.
148  nsRefPtr<SelfType> runnable = new SelfType(aObject,
149  aMethod,
150  aFailureReturnValue,
151  aArg1Value);
152  NS_ENSURE_TRUE(runnable, aFailureReturnValue);
153 
154  // Initialize the Songbird runnable method.
155  rv = runnable->Initialize();
156  NS_ENSURE_SUCCESS(rv, rv);
157 
158  // Return results.
159  runnable.forget(aRunnable);
160 
161  return NS_OK;
162  }
163 
164 
180  static ReturnType InvokeOnMainThread(ClassType* aObject,
181  MethodType aMethod,
182  ReturnType aFailureReturnValue,
183  Arg1Type aArg1Value)
184  {
185  nsresult rv;
186 
187  // Create a Songbird runnable method.
188  nsRefPtr<SelfType> runnable;
189  rv = New(getter_AddRefs(runnable),
190  aObject,
191  aMethod,
192  aFailureReturnValue,
193  aArg1Value);
194  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
195 
196  // Dispatch the runnable method on the main thread.
197  rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
198  NS_ENSURE_SUCCESS(rv, rv);
199 
200  return runnable->GetReturnValue();
201  }
202 
219  static ReturnType InvokeOnThread(ClassType* aObject,
220  MethodType aMethod,
221  ReturnType aFailureReturnValue,
222  Arg1Type aArg1Value,
223  nsIEventTarget* aThread)
224  {
225  nsresult rv;
226 
227  // Create a Songbird runnable method.
228  nsRefPtr<SelfType> runnable;
229  rv = New(getter_AddRefs(runnable),
230  aObject,
231  aMethod,
232  aFailureReturnValue,
233  aArg1Value);
234  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
235 
236  // Dispatch the runnable method on the thread.
237  rv = aThread->Dispatch(runnable, NS_DISPATCH_SYNC);
238  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
239 
240  return runnable->GetReturnValue();
241  }
242 
243 
257  static nsresult InvokeOnMainThreadAsync(ClassType* aObject,
258  MethodType aMethod,
259  ReturnType aFailureReturnValue,
260  Arg1Type aArg1Value)
261  {
262  nsresult rv;
263 
264  // Create a Songbird runnable method.
265  nsRefPtr<SelfType> runnable;
266  rv = New(getter_AddRefs(runnable),
267  aObject,
268  aMethod,
269  aFailureReturnValue,
270  aArg1Value);
271  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
272 
273  // Dispatch the runnable method on the main thread.
274  rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL);
275  NS_ENSURE_SUCCESS(rv, rv);
276 
277  return NS_OK;
278  }
279 
294  static nsresult InvokeOnThreadAsync(ClassType* aObject,
295  MethodType aMethod,
296  ReturnType aFailureReturnValue,
297  Arg1Type aArg1Value,
298  nsIEventTarget* aThread)
299  {
300  nsresult rv;
301 
302  // Create a Songbird runnable method.
303  nsRefPtr<SelfType> runnable;
304  rv = New(getter_AddRefs(runnable),
305  aObject,
306  aMethod,
307  aFailureReturnValue,
308  aArg1Value);
309  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
310 
311  // Dispatch the runnable method on the thread.
312  rv = aThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
313  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
314 
315  return NS_OK;
316  }
317 
324  ReturnType GetReturnValue()
325  {
326  NS_ENSURE_TRUE(mLock, mFailureReturnValue);
327  nsAutoLock autoLock(mLock);
328  return mReturnValue;
329  }
330 
331 
332  //----------------------------------------------------------------------------
333  //
334  // Protected interface.
335  //
336  //----------------------------------------------------------------------------
337 
338 protected:
339 
354  sbRunnableMethod1(ClassType* aObject,
355  MethodType aMethod,
356  ReturnType aFailureReturnValue,
357  Arg1Type aArg1Value) :
358  mLock(nsnull),
359  mObject(aObject),
360  mMethod(aMethod),
361  mReturnValue(aFailureReturnValue),
362  mFailureReturnValue(aFailureReturnValue),
363  mArg1Value(aArg1Value)
364  {
365  }
366 
367 
373  {
374  // Dispose of the Songbird runnable method lock.
375  if (mLock)
376  nsAutoLock::DestroyLock(mLock);
377  }
378 
379 
384  nsresult Initialize()
385  {
386  // Create the runnable lock.
387  mLock = nsAutoLock::NewLock("sbRunnableMethod1::mLock");
388  NS_ENSURE_TRUE(mLock, NS_ERROR_OUT_OF_MEMORY);
389 
390  return NS_OK;
391  }
392 
393 
394  //
395  // mLock Lock used to serialize field access.
396  // mObject Object for which to invoke method.
397  // mMethod Method to invoke.
398  // mReturnValue Method return value.
399  // mFailureReturnValue Method return value to use on failure.
400  // mArg1Value Method argument 1 value.
401  //
402 
403  PRLock* mLock;
404  nsRefPtr<ClassType> mObject;
406  ReturnType mReturnValue;
408  Arg1Type mArg1Value;
409 };
410 
411 
416 template <class ClassType,
417  typename ReturnType,
418  typename Arg1Type,
419  typename Arg2Type>
421  public sbRunnableMethod1<ClassType, ReturnType, Arg1Type>
422 {
423  //----------------------------------------------------------------------------
424  //
425  // Public interface.
426  //
427  //----------------------------------------------------------------------------
428 
429 public:
430 
431  //
432  // SelfType Type for this class.
433  // BaseType Base type for this class.
434  // MethodType Type of method to invoke.
435  //
436 
439  typedef ReturnType (ClassType::*MethodType)(Arg1Type aArg1Value,
440  Arg2Type aArg2Value);
441 
442 
447  NS_IMETHOD Run()
448  {
449  // Do nothing if no object was provided.
450  if (!BaseType::mObject)
451  return NS_OK;
452 
453  // Invoke method.
454  ReturnType
456  mArg2Value);
457  {
458  nsAutoLock autoLock(BaseType::mLock);
459  BaseType::mReturnValue = returnValue;
460  }
461 
462  return NS_OK;
463  }
464 
465 
483  static nsresult New(SelfType** aRunnable,
484  ClassType* aObject,
485  MethodType aMethod,
486  ReturnType aFailureReturnValue,
487  Arg1Type aArg1Value,
488  Arg2Type aArg2Value)
489  {
490  // Validate arguments.
491  NS_ENSURE_ARG_POINTER(aRunnable);
492  NS_ENSURE_ARG_POINTER(aObject);
493  NS_ENSURE_ARG_POINTER(aMethod);
494 
495  // Function variables.
496  nsresult rv;
497 
498  // Create a Songbird runnable method.
499  nsRefPtr<SelfType> runnable = new SelfType(aObject,
500  aMethod,
501  aFailureReturnValue,
502  aArg1Value,
503  aArg2Value);
504  NS_ENSURE_TRUE(runnable, aFailureReturnValue);
505 
506  // Initialize the Songbird runnable method.
507  rv = runnable->Initialize();
508  NS_ENSURE_SUCCESS(rv, rv);
509 
510  // Return results.
511  runnable.forget(aRunnable);
512 
513  return NS_OK;
514  }
515 
516 
533  static ReturnType InvokeOnMainThread(ClassType* aObject,
534  MethodType aMethod,
535  ReturnType aFailureReturnValue,
536  Arg1Type aArg1Value,
537  Arg2Type aArg2Value)
538  {
539  nsresult rv;
540 
541  // Create a Songbird runnable method.
542  nsRefPtr<SelfType> runnable;
543  rv = New(getter_AddRefs(runnable),
544  aObject,
545  aMethod,
546  aFailureReturnValue,
547  aArg1Value,
548  aArg2Value);
549  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
550 
551  // Dispatch the runnable method on the main thread.
552  rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_SYNC);
553  NS_ENSURE_SUCCESS(rv, rv);
554 
555  return runnable->GetReturnValue();
556  }
557 
575  static ReturnType InvokeOnThread(ClassType* aObject,
576  MethodType aMethod,
577  ReturnType aFailureReturnValue,
578  Arg1Type aArg1Value,
579  Arg2Type aArg2Value,
580  nsIEventTarget* aThread)
581  {
582  nsresult rv;
583 
584  // Create a Songbird runnable method.
585  nsRefPtr<SelfType> runnable;
586  rv = New(getter_AddRefs(runnable),
587  aObject,
588  aMethod,
589  aFailureReturnValue,
590  aArg1Value,
591  aArg2Value);
592  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
593 
594  // Dispatch the runnable method on the main thread.
595  rv = aThread->Dispatch(runnable, NS_DISPATCH_SYNC);
596  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
597 
598  return runnable->GetReturnValue();
599  }
600 
615  static nsresult InvokeOnMainThreadAsync(ClassType* aObject,
616  MethodType aMethod,
617  ReturnType aFailureReturnValue,
618  Arg1Type aArg1Value,
619  Arg2Type aArg2Value)
620  {
621  nsresult rv;
622 
623  // Create a Songbird runnable method.
624  nsRefPtr<SelfType> runnable;
625  rv = New(getter_AddRefs(runnable),
626  aObject,
627  aMethod,
628  aFailureReturnValue,
629  aArg1Value,
630  aArg2Value);
631  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
632 
633  // Dispatch the runnable method on the main thread.
634  rv = NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL);
635  NS_ENSURE_SUCCESS(rv, rv);
636 
637  return NS_OK;
638  }
639 
655  static nsresult InvokeOnThreadAsync(ClassType* aObject,
656  MethodType aMethod,
657  ReturnType aFailureReturnValue,
658  Arg1Type aArg1Value,
659  Arg2Type aArg2Value,
660  nsIEventTarget* aThread)
661  {
662  nsresult rv;
663 
664  // Create a Songbird runnable method.
665  nsRefPtr<SelfType> runnable;
666  rv = New(getter_AddRefs(runnable),
667  aObject,
668  aMethod,
669  aFailureReturnValue,
670  aArg1Value,
671  aArg2Value);
672  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
673 
674  // Dispatch the runnable method on the main thread.
675  rv = aThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
676  NS_ENSURE_SUCCESS(rv, aFailureReturnValue);
677 
678  return NS_OK;
679  }
680 
681  //----------------------------------------------------------------------------
682  //
683  // Protected interface.
684  //
685  //----------------------------------------------------------------------------
686 
687 protected:
688 
703  sbRunnableMethod2(ClassType* aObject,
704  MethodType aMethod,
705  ReturnType aFailureReturnValue,
706  Arg1Type aArg1Value,
707  Arg2Type aArg2Value) :
708  BaseType(aObject, nsnull, aFailureReturnValue, aArg1Value),
709  mMethod(aMethod),
710  mArg2Value(aArg2Value)
711  {
712  }
713 
714 
720  {
721  }
722 
723 
724  //
725  // mMethod Method to invoke.
726  // mArg2Value Method argument 2 value.
727  //
728 
730  Arg2Type mArg2Value;
731 };
732 
733 
734 
739 class sbRunnable : public nsRunnable
740 {
741 public:
743  const char * aName) :
744  mMonitor(aName ? aName : "sbRunnable"),
745  mDone(false)
746  {}
747 
748 
755  NS_IMETHOD Run();
756 
761  PRBool Wait(PRIntervalTime aTimeout);
762 
763 private:
764  mozilla::Monitor mMonitor;
765  PRBool mDone;
766 };
767 
768 
769 
773 template <typename ResultType>
774 class sbRunnable_ : public sbRunnable
775 {
776 public:
777  using sbRunnable::Wait;
778 
779 public:
781  const char * aName) :
782  sbRunnable(aName)
783  {}
784 
785 
790  NS_IMETHOD Run()
791  {
792  // Invoke method.
793  mResult = OnRun();
794  return sbRunnable::Run();
795  }
796 
797 
801  ResultType
803  {
804  Wait(PR_INTERVAL_NO_TIMEOUT);
805  return mResult;
806  }
807 
808 
814  PRBool
815  Wait(PRIntervalTime aTimeout, ResultType & aResult)
816  {
817  if (!Wait(aTimeout)) {
818  return PR_FALSE;
819  }
820 
821  aResult = mResult;
822  return PR_TRUE;
823  }
824 
825 
826 protected:
830  virtual ResultType OnRun() = 0;
831 
832 private:
833  ResultType mResult;
834 };
835 
836 
837 
841 template <>
842 class sbRunnable_<void> : public sbRunnable
843 {
844 public:
846  const char * aName) :
847  sbRunnable(aName)
848  {}
849 
850 
855  NS_IMETHOD Run()
856  {
857  // Invoke method.
858  OnRun();
859  return sbRunnable::Run();
860  }
861 
862 
863 protected:
867  virtual void OnRun() = 0;
868 };
869 
870 
871 
878 template <typename ResultType,
879  typename TargetType,
880  typename MethodType>
881 class sbRunnableMethod_ : public sbRunnable_<ResultType>
882 {
883 public:
885  TargetType & aTarget,
886  MethodType aMethod,
887  const char * aName) :
888  sbRunnable_<ResultType>(aName),
889  mTarget(&aTarget),
890  mMethod(aMethod)
891  {}
892 
893 
894 protected:
899  virtual ResultType OnRun() = 0;
900 
901  nsRefPtr<TargetType> mTarget;
902  MethodType mMethod;
903 };
904 
905 
906 
916 template <typename ResultType,
917  typename TargetType,
918  typename Param1Type,
919  typename Arg1Type = Param1Type,
920  typename MethodType = ResultType (TargetType::*) (Param1Type)>
923 {
924 public:
926 
927 
932  TargetType & aTarget,
933  MethodType aMethod,
934  Param1Type aArg1,
935  const char * aName = NULL) :
936  BaseType(aTarget, aMethod, aName),
937  mArg1(aArg1)
938  {}
939 
940 
941 protected:
947  virtual ResultType OnRun()
948  {
949  return (BaseType::mTarget->*BaseType::mMethod)(mArg1);
950  }
951 
952 
953 private:
954  Arg1Type mArg1;
955 };
956 
957 
958 
968 template <typename ResultType,
969  typename TargetType,
970  typename Param1Type,
971  typename Param2Type,
972  typename Arg1Type = Param1Type,
973  typename Arg2Type = Param2Type,
974  typename MethodType = ResultType (TargetType::*) (Param1Type,
975  Param2Type)>
978 {
979 public:
981 
982 
987  TargetType & aTarget,
988  MethodType aMethod,
989  Param1Type aArg1,
990  Param2Type aArg2,
991  const char * aName = NULL) :
992  BaseType(aTarget, aMethod, aName),
993  mArg1(aArg1),
994  mArg2(aArg2)
995  {}
996 
997 
998 protected:
1004  virtual ResultType OnRun()
1005  {
1006  return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2);
1007  }
1008 
1009 
1010 private:
1011  Arg1Type mArg1;
1012  Arg2Type mArg2;
1013 };
1014 
1015 
1016 
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,
1035  Param2Type,
1036  Param3Type)>
1039 {
1040 public:
1042 
1043 
1048  TargetType & aTarget,
1049  MethodType aMethod,
1050  Param1Type aArg1,
1051  Param2Type aArg2,
1052  Param3Type aArg3,
1053  const char * aName = NULL) :
1054  BaseType(aTarget, aMethod, aName),
1055  mArg1(aArg1),
1056  mArg2(aArg2),
1057  mArg3(aArg3)
1058  {}
1059 
1060 
1061 protected:
1067  virtual ResultType OnRun()
1068  {
1069  return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2, mArg3);
1070  }
1071 
1072 
1073 private:
1074  Arg1Type mArg1;
1075  Arg2Type mArg2;
1076  Arg3Type mArg3;
1077 };
1078 
1079 
1080 
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,
1101  Param2Type,
1102  Param3Type,
1103  Param4Type)>
1106 {
1107 public:
1109 
1110 
1115  TargetType & aTarget,
1116  MethodType aMethod,
1117  Param1Type aArg1,
1118  Param2Type aArg2,
1119  Param3Type aArg3,
1120  Param4Type aArg4,
1121  const char * aName = NULL) :
1122  BaseType(aTarget, aMethod, aName),
1123  mArg1(aArg1),
1124  mArg2(aArg2),
1125  mArg3(aArg3),
1126  mArg4(aArg4)
1127  {}
1128 
1129 
1130 protected:
1136  virtual ResultType OnRun()
1137  {
1138  return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2, mArg3, mArg4);
1139  }
1140 
1141 
1142 private:
1143  Arg1Type mArg1;
1144  Arg2Type mArg2;
1145  Arg3Type mArg3;
1146  Arg4Type mArg4;
1147 };
1148 
1149 
1150 
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,
1173  Param2Type,
1174  Param3Type,
1175  Param4Type,
1176  Param5Type)>
1179 {
1180 public:
1182 
1183 
1188  TargetType & aTarget,
1189  MethodType aMethod,
1190  Param1Type aArg1,
1191  Param2Type aArg2,
1192  Param3Type aArg3,
1193  Param4Type aArg4,
1194  Param5Type aArg5,
1195  const char * aName = NULL) :
1196  BaseType(aTarget, aMethod, aName),
1197  mArg1(aArg1),
1198  mArg2(aArg2),
1199  mArg3(aArg3),
1200  mArg4(aArg4),
1201  mArg5(aArg5)
1202  {}
1203 
1204 
1205 protected:
1211  virtual ResultType OnRun()
1212  {
1213  return (BaseType::mTarget->*BaseType::mMethod)(mArg1, mArg2, mArg3, mArg4, mArg5);
1214  }
1215 
1216 
1217 private:
1218  Arg1Type mArg1;
1219  Arg2Type mArg2;
1220  Arg3Type mArg3;
1221  Arg4Type mArg4;
1222  Arg5Type mArg5;
1223 };
1224 
1225 
1226 
1227 //------------------------------------------------------------------------------
1228 //
1229 // Songbird thread utilities macros.
1230 //
1231 //------------------------------------------------------------------------------
1232 
1252 #define SB_INVOKE_ON_MAIN_THREAD1(aClassType, \
1253  aObject, \
1254  aMethod, \
1255  aReturnType, \
1256  aFailureReturnValue, \
1257  aArg1Type, \
1258  aArg1Value) \
1259  sbRunnableMethod1<aClassType, aReturnType, aArg1Type> \
1260  ::InvokeOnMainThread(aObject, \
1261  &aClassType::aMethod, \
1262  aFailureReturnValue, \
1263  aArg1Value)
1264 
1279 template <class T, class MT, class RT, class A1>
1280 inline
1281 RT sbInvokeOnMainThread1(T & aObject,
1282  MT aMethod,
1283  RT aFailureReturnValue,
1284  A1 aArg1)
1285 {
1287  aMethod,
1288  aFailureReturnValue,
1289  aArg1);
1290 }
1291 
1307 template <class T, class MT, class RT, class A1, class TH>
1308 inline
1309 RT sbInvokeOnThread1(T & aObject,
1310  MT aMethod,
1311  RT aFailureReturnValue,
1312  A1 aArg1,
1313  TH aThread)
1314 {
1316  aMethod,
1317  aFailureReturnValue,
1318  aArg1,
1319  aThread);
1320 }
1321 
1335 template <class T, class MT, class RT, class A1>
1336 inline
1337 nsresult sbInvokeOnMainThread1Async(T & aObject,
1338  MT aMethod,
1339  RT aFailureReturnValue,
1340  A1 aArg1)
1341 {
1343  &aObject,
1344  aMethod,
1345  aFailureReturnValue,
1346  aArg1);
1347 }
1348 
1363 template <class T, class MT, class RT, class A1, class TH>
1364 inline
1365 nsresult sbInvokeOnThread1Async(T & aObject,
1366  MT aMethod,
1367  RT aFailureReturnValue,
1368  A1 aArg1,
1369  TH aThread)
1370 {
1372  aMethod,
1373  aFailureReturnValue,
1374  aArg1,
1375  aThread);
1376 }
1377 
1378 #define SB_INVOKE_ON_MAIN_THREAD2(aClassType, \
1379  aObject, \
1380  aMethod, \
1381  aReturnType, \
1382  aFailureReturnValue, \
1383  aArg1Type, \
1384  aArg1Value, \
1385  aArg2Type, \
1386  aArg2Value) \
1387  sbRunnableMethod2<aClassType, aReturnType, aArg1Type, aArg2Type> \
1388  ::InvokeOnMainThread(aObject, \
1389  &aClassType::aMethod, \
1390  aFailureReturnValue, \
1391  aArg1Value, \
1392  aArg2Value)
1393 
1408 template <class T, class MT, class RT, class A1, class A2>
1409 inline
1410 RT sbInvokeOnMainThread2(T & aObject,
1411  MT aMethod,
1412  RT aFailureReturnValue,
1413  A1 aArg1,
1414  A2 aArg2)
1415 {
1417  &aObject,
1418  aMethod,
1419  aFailureReturnValue,
1420  aArg1,
1421  aArg2);
1422 }
1423 
1439 template <class T, class MT, class RT, class A1, class A2, class TH>
1440 inline
1441 RT sbInvokeOnThread2(T & aObject,
1442  MT aMethod,
1443  RT aFailureReturnValue,
1444  A1 aArg1,
1445  A2 aArg2,
1446  TH aThread)
1447 {
1449  aMethod,
1450  aFailureReturnValue,
1451  aArg1,
1452  aArg2,
1453  aThread);
1454 }
1455 
1471 template <class T, class MT, class RT, class A1, class A2>
1472 inline
1473 nsresult sbInvokeOnMainThread2Async(T & aObject,
1474  MT aMethod,
1475  RT aFailureReturnValue,
1476  A1 aArg1,
1477  A2 aArg2)
1478 {
1480  &aObject,
1481  aMethod,
1482  aFailureReturnValue,
1483  aArg1,
1484  aArg2);
1485 }
1486 
1503 template <class T, class MT, class RT, class A1, class A2, class TH>
1504 inline
1505 nsresult sbInvokeOnThread2Async(T & aObject,
1506  MT aMethod,
1507  RT aFailureReturnValue,
1508  A1 aArg1,
1509  A2 aArg2,
1510  TH aThread)
1511 {
1513  &aObject,
1514  aMethod,
1515  aFailureReturnValue,
1516  aArg1,
1517  aArg2,
1518  aThread);
1519 }
1520 
1541 #define SB_INVOKE_ON_MAIN_THREAD_ASYNC1(aClassType, \
1542  aObject, \
1543  aMethod, \
1544  aReturnType, \
1545  aFailureReturnValue, \
1546  aArg1Type, \
1547  aArg1Value) \
1548  sbRunnableMethod1<aClassType, aReturnType, aArg1Type> \
1549  ::InvokeOnMainThreadAsync(aObject, \
1550  &aClassType::aMethod, \
1551  aFailureReturnValue, \
1552  aArg1Value)
1553 
1554 #define SB_INVOKE_ON_MAIN_THREAD_ASYNC2(aClassType, \
1555  aObject, \
1556  aMethod, \
1557  aReturnType, \
1558  aFailureReturnValue, \
1559  aArg1Type, \
1560  aArg1Value, \
1561  aArg2Type, \
1562  aArg2Value) \
1563  sbRunnableMethod2<aClassType, aReturnType, aArg1Type, aArg2Type> \
1564  ::InvokeOnMainThreadAsync(aObject, \
1565  &aClassType::aMethod, \
1566  aFailureReturnValue, \
1567  aArg1Value, \
1568  aArg2Value)
1569 
1570 
1571 //------------------------------------------------------------------------------
1572 //
1573 // Songbird thread utilities service prototypes.
1574 //
1575 //------------------------------------------------------------------------------
1576 
1586 PRBool SB_IsMainThread(nsIThreadManager* aThreadManager = nsnull);
1587 
1588 
1589 #endif // __SB_THREAD_UTILS_H__
1590 
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)
return NS_OK
virtual ResultType OnRun()
MethodType mMethod
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
sbRunnableMethod_< ResultType, TargetType, MethodType > BaseType
NS_IMETHOD Run()
Definition: sbThreadUtils.h:97
static ReturnType InvokeOnMainThread(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, Arg2Type aArg2Value)
sbRunnableMethod2< ClassType, ReturnType, Arg1Type, Arg2Type > SelfType
MethodType mMethod
static ReturnType InvokeOnThread(ClassType *aObject, MethodType aMethod, ReturnType aFailureReturnValue, Arg1Type aArg1Value, nsIEventTarget *aThread)
ReturnType mReturnValue
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)
nsresult Initialize()
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)
NS_IMETHOD Run()
ReturnType(ClassType::* MethodType)(Arg1Type aArg1Value)
Definition: sbThreadUtils.h:90
PRBool Wait(PRIntervalTime aTimeout, ResultType &aResult)
sbRunnableMethod1< ClassType, ReturnType, Arg1Type > SelfType
Definition: sbThreadUtils.h:89
virtual ResultType OnRun()
this _document false
Definition: FeedWriter.js:1085
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)
_updateCookies aName
nsRefPtr< TargetType > mTarget
ReturnType(ClassType::* MethodType)(Arg1Type aArg1Value, Arg2Type aArg2Value)
RT sbInvokeOnMainThread2(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1, A2 aArg2)
ResultType Wait()
MethodType mMethod
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)
NS_IMETHOD Run()
virtual ResultType OnRun()
NS_IMETHOD Run()