diff --git a/xpcom/threads/nsThreadUtils.h b/xpcom/threads/nsThreadUtils.h index c0ad804d0065..83159bbfc206 100644 --- a/xpcom/threads/nsThreadUtils.h +++ b/xpcom/threads/nsThreadUtils.h @@ -324,6 +324,16 @@ using RunnableFunctionImpl = // Make sure we store a non-reference in nsRunnableFunction. typename detail::RunnableFunction::Type>; +template +inline already_AddRefed +SetRunnableName(already_AddRefed&& aObj, const char* aName) +{ + MOZ_RELEASE_ASSERT(aName); + RefPtr ref(aObj); + ref->SetName(aName); + return ref.forget(); +} + } // namespace detail namespace detail { @@ -422,6 +432,14 @@ NS_NewRunnableFunction(Function&& aFunction) (mozilla::Forward(aFunction))); } +template +already_AddRefed +NS_NewRunnableFunction(const char* aName, Function&& aFunction) +{ + return mozilla::detail::SetRunnableName( + NS_NewRunnableFunction(mozilla::Forward(aFunction)), aName); +} + // 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. @@ -935,6 +953,14 @@ NewRunnableMethod(PtrType&& aPtr, Method aMethod) (Forward(aPtr), aMethod)); } +template +already_AddRefed> +NewRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) +{ + return detail::SetRunnableName( + NewRunnableMethod(Forward(aPtr), aMethod), aName); +} + template already_AddRefed> NewCancelableRunnableMethod(PtrType&& aPtr, Method aMethod) @@ -944,6 +970,14 @@ NewCancelableRunnableMethod(PtrType&& aPtr, Method aMethod) (Forward(aPtr), aMethod)); } +template +already_AddRefed> +NewCancelableRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) +{ + return detail::SetRunnableName( + NewCancelableRunnableMethod(Forward(aPtr), aMethod), aName); +} + template already_AddRefed> NewNonOwningRunnableMethod(PtrType&& aPtr, Method aMethod) @@ -953,6 +987,14 @@ NewNonOwningRunnableMethod(PtrType&& aPtr, Method aMethod) (Forward(aPtr), aMethod)); } +template +already_AddRefed> +NewNonOwningRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) +{ + return detail::SetRunnableName( + NewNonOwningRunnableMethod(Forward(aPtr), aMethod), aName); +} + template already_AddRefed> NewNonOwningCancelableRunnableMethod(PtrType&& aPtr, Method aMethod) @@ -962,6 +1004,15 @@ NewNonOwningCancelableRunnableMethod(PtrType&& aPtr, Method aMethod) (Forward(aPtr), aMethod)); } +template +already_AddRefed> +NewNonOwningCancelableRunnableMethod(const char* aName, PtrType&& aPtr, + Method aMethod) +{ + return detail::SetRunnableName( + NewNonOwningCancelableRunnableMethod(Forward(aPtr), aMethod), aName); +} + // Similar to NewRunnableMethod. Call like so: // nsCOMPtr event = // NewRunnableMethod(myObject, &MyClass::HandleEvent, myArg1,...); @@ -977,6 +1028,17 @@ NewRunnableMethod(PtrType&& aPtr, Method aMethod, Args&&... aArgs) (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...)); } +template +already_AddRefed> +NewRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod, Args&&... aArgs) +{ + static_assert(sizeof...(Storages) == sizeof...(Args), + " size should be equal to number of arguments"); + return detail::SetRunnableName( + NewRunnableMethod + (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...), aName); +} + template already_AddRefed> NewNonOwningRunnableMethod(PtrType&& aPtr, Method aMethod, Args&&... aArgs) @@ -988,6 +1050,18 @@ NewNonOwningRunnableMethod(PtrType&& aPtr, Method aMethod, Args&&... aArgs) (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...)); } +template +already_AddRefed> +NewNonOwningRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod, + Args&&... aArgs) +{ + static_assert(sizeof...(Storages) == sizeof...(Args), + " size should be equal to number of arguments"); + return detail::SetRunnableName( + NewNonOwningRunnableMethod + (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...), aName); +} + template already_AddRefed> NewCancelableRunnableMethod(PtrType&& aPtr, Method aMethod, Args&&... aArgs) @@ -999,6 +1073,18 @@ NewCancelableRunnableMethod(PtrType&& aPtr, Method aMethod, Args&&... aArgs) (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...)); } +template +already_AddRefed> +NewCancelableRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod, + Args&&... aArgs) +{ + static_assert(sizeof...(Storages) == sizeof...(Args), + " size should be equal to number of arguments"); + return detail::SetRunnableName( + NewCancelableRunnableMethod + (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...), aName); +} + template already_AddRefed> NewNonOwningCancelableRunnableMethod(PtrType&& aPtr, Method aMethod, @@ -1011,6 +1097,18 @@ NewNonOwningCancelableRunnableMethod(PtrType&& aPtr, Method aMethod, (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...)); } +template +already_AddRefed> +NewNonOwningCancelableRunnableMethod(const char* aName, PtrType&& aPtr, + Method aMethod, Args&&... aArgs) +{ + static_assert(sizeof...(Storages) == sizeof...(Args), + " size should be equal to number of arguments"); + return detail::SetRunnableName( + NewNonOwningCancelableRunnableMethod + (Forward(aPtr), aMethod, mozilla::Forward(aArgs)...), aName); +} + } // namespace mozilla #endif // XPCOM_GLUE_AVOID_NSPR