Bug 1333974, part 1 - Add new API for releasing on main thread using the system group. r=froydnj
MozReview-Commit-ID: KP71tgpaXaY
--- a/xpcom/threads/nsProxyRelease.h
+++ b/xpcom/threads/nsProxyRelease.h
@@ -10,16 +10,17 @@
#include "nsIEventTarget.h"
#include "nsIThread.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "MainThreadUtils.h"
#include "nsThreadUtils.h"
#include "mozilla/Likely.h"
#include "mozilla/Move.h"
+#include "mozilla/SystemGroup.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Unused.h"
#ifdef XPCOM_GLUE_AVOID_NSPR
#error NS_ProxyRelease implementation depends on NSPR.
#endif
namespace detail {
@@ -154,16 +155,42 @@ NS_ReleaseOnMainThread(already_AddRefed<
return;
}
}
NS_ProxyRelease(mainThread, mozilla::Move(aDoomed), aAlwaysProxy);
}
/**
+ * This is the same as NS_ReleaseOnMainThread, except that the
+ * runnable for the deletion will be dispatched to the system group.
+ */
+template<class T>
+inline NS_HIDDEN_(void)
+NS_ReleaseOnMainThreadSystemGroup(already_AddRefed<T> aDoomed,
+ bool aAlwaysProxy = false)
+{
+ // NS_ProxyRelease treats a null event target as "the current thread". So a
+ // handle on the main thread is only necessary when we're not already on the
+ // main thread or the release must happen asynchronously.
+ nsCOMPtr<nsIEventTarget> systemGroupEventTarget;
+ if (!NS_IsMainThread() || aAlwaysProxy) {
+ systemGroupEventTarget = mozilla::SystemGroup::EventTargetFor(mozilla::TaskCategory::Other);
+
+ if (!systemGroupEventTarget) {
+ MOZ_ASSERT_UNREACHABLE("Could not get main thread; leaking an object!");
+ mozilla::Unused << aDoomed.take();
+ return;
+ }
+ }
+
+ NS_ProxyRelease(systemGroupEventTarget, mozilla::Move(aDoomed), aAlwaysProxy);
+}
+
+/**
* Class to safely handle main-thread-only pointers off the main thread.
*
* Classes like XPCWrappedJS are main-thread-only, which means that it is
* forbidden to call methods on instances of these classes off the main thread.
* For various reasons (see bug 771074), this restriction recently began to
* apply to AddRef/Release as well.
*
* This presents a problem for consumers that wish to hold a callback alive