Bug 1152753 - Add a wrapper to be able to dispatch C++11 lambdas to nsIThread. r=froydnj
☠☠ backed out by 7de210d3a493 ☠ ☠
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 10 Apr 2015 08:13:00 -0400
changeset 238520 35cae02f15520f58b607fe2031fbc912f8c51320
parent 238519 0b456166808e4f4119ada95c490692854978ba07
child 238521 07d4c10bdd35b72d23f043e1392244b472b98656
push id58243
push userkgupta@mozilla.com
push dateFri, 10 Apr 2015 12:14:46 +0000
treeherdermozilla-inbound@35cae02f1552 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1152753
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1152753 - Add a wrapper to be able to dispatch C++11 lambdas to nsIThread. r=froydnj
xpcom/glue/nsThreadUtils.h
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -14,16 +14,17 @@
 #include "nsIThread.h"
 #include "nsIRunnable.h"
 #include "nsICancelableRunnable.h"
 #include "nsStringGlue.h"
 #include "nsCOMPtr.h"
 #include "nsAutoPtr.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/Likely.h"
+#include "mozilla/TypeTraits.h"
 
 //-----------------------------------------------------------------------------
 // These methods are alternatives to the methods on nsIThreadManager, provided
 // for convenience.
 
 /**
  * Set name of the target thread.  This operation is asynchronous.
  */
@@ -233,16 +234,43 @@ public:
   NS_DECL_NSICANCELABLERUNNABLE
 
   nsCancelableRunnable() {}
 
 protected:
   virtual ~nsCancelableRunnable() {}
 };
 
+// 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);
+}
+
 // An event that can be used to call a method on a class.  The class type must
 // support reference counting. This event supports Revoke for use
 // with nsRevocableEventPtr.
 template<class ClassType,
          typename ReturnType = void,
          bool Owning = true>
 class nsRunnableMethod : public nsRunnable
 {