Bug 1333974, part 1 - Add new API for releasing on main thread using the system group. r=froydnj draft
authorAndrew McCreight <continuation@gmail.com>
Tue, 31 Jan 2017 11:35:10 -0800
changeset 468639 7a910be6c072f6c711db0cd88f162299825141fc
parent 468498 ee975d32deb9eaa5641f45428cd6a4b5b555a8f5
child 468640 4977d52237477fa3a3802c38e09274b602c34e85
push id43544
push userbmo:continuation@gmail.com
push dateTue, 31 Jan 2017 22:45:39 +0000
reviewersfroydnj
bugs1333974
milestone54.0a1
Bug 1333974, part 1 - Add new API for releasing on main thread using the system group. r=froydnj MozReview-Commit-ID: KP71tgpaXaY
xpcom/threads/nsProxyRelease.h
--- 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