Bug 1271593 - NS_NewRunnableFunction should forward its arg - r?froydnj draft
authorGerald Squelart <gsquelart@mozilla.com>
Tue, 10 May 2016 18:13:14 +1000
changeset 365501 5b7ff1d05f4735f04b3bd49d30aaca5e4af48efe
parent 365494 674a552743785c28c75866969aad513bd8eaf6ae
child 520576 7256f3d9ca76b4392717a75432760112b4883711
push id17764
push usergsquelart@mozilla.com
push dateTue, 10 May 2016 23:09:04 +0000
reviewersfroydnj
bugs1271593
milestone49.0a1
Bug 1271593 - NS_NewRunnableFunction should forward its arg - r?froydnj By perfect-forwarding its argument, we can automatically gain move semantics optimization when storing the given function object into nsRunnableFunction. MozReview-Commit-ID: 9EZK84ZhMvR
xpcom/glue/nsThreadUtils.h
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -15,16 +15,17 @@
 #include "nsIRunnable.h"
 #include "nsICancelableRunnable.h"
 #include "nsStringGlue.h"
 #include "nsCOMPtr.h"
 #include "nsAutoPtr.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/IndexSequence.h"
 #include "mozilla/Likely.h"
+#include "mozilla/Move.h"
 #include "mozilla/Tuple.h"
 #include "mozilla/TypeTraits.h"
 
 //-----------------------------------------------------------------------------
 // These methods are alternatives to the methods on nsIThreadManager, provided
 // for convenience.
 
 /**
@@ -253,34 +254,35 @@ protected:
 
 // 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 mozilla::Runnable
 {
 public:
-  explicit nsRunnableFunction(const Function& aFunction)
-    : mFunction(aFunction)
+  template <typename F>
+  explicit nsRunnableFunction(F&& aFunction)
+    : mFunction(mozilla::Forward<F>(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)
+nsRunnableFunction<Function>* NS_NewRunnableFunction(Function&& aFunction)
 {
-  return new nsRunnableFunction<Function>(aFunction);
+  return new nsRunnableFunction<Function>(mozilla::Forward<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,