Bug 1363877 - Label IPC shmem messages as SystemGroup (r=dvander)
authorBill McCloskey <billm@mozilla.com>
Wed, 10 May 2017 12:52:20 -0700
changeset 358805 546804e76ce7940edeca6d0bb8aacf2212544b0f
parent 358804 e062bbfde21585b49d7d9e25186481189658ea5b
child 358806 263b2121656872996cd76c8bf7f684f7e568cbf5
push id90395
push userwmccloskey@mozilla.com
push dateWed, 17 May 2017 18:47:54 +0000
treeherdermozilla-inbound@3aa433b56b2d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdvander
bugs1363877
milestone55.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 1363877 - Label IPC shmem messages as SystemGroup (r=dvander) MozReview-Commit-ID: 3I5ny0wxVHI
ipc/glue/ProtocolUtils.cpp
ipc/glue/ProtocolUtils.h
xpcom/threads/SystemGroup.cpp
xpcom/threads/SystemGroup.h
--- a/ipc/glue/ProtocolUtils.cpp
+++ b/ipc/glue/ProtocolUtils.cpp
@@ -14,16 +14,17 @@
 #include "mozilla/IntegerPrintfMacros.h"
 
 #include "mozilla/ipc/ProtocolUtils.h"
 
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/ipc/MessageChannel.h"
 #include "mozilla/ipc/Transport.h"
 #include "mozilla/StaticMutex.h"
+#include "mozilla/SystemGroup.h"
 #include "mozilla/Unused.h"
 #include "nsPrintfCString.h"
 
 #if defined(MOZ_SANDBOX) && defined(XP_WIN)
 #define TARGET_SANDBOX_EXPORTS
 #include "mozilla/sandboxTarget.h"
 #endif
 
@@ -627,25 +628,27 @@ IToplevelProtocol::TakeMinidump(nsIFile*
 }
 
 bool
 IToplevelProtocol::Open(mozilla::ipc::Transport* aTransport,
                         base::ProcessId aOtherPid,
                         MessageLoop* aThread,
                         mozilla::ipc::Side aSide)
 {
+  mIsMainThreadProtocol = NS_IsMainThread();
   SetOtherProcessId(aOtherPid);
   return GetIPCChannel()->Open(aTransport, aThread, aSide);
 }
 
 bool
 IToplevelProtocol::Open(MessageChannel* aChannel,
                         MessageLoop* aMessageLoop,
                         mozilla::ipc::Side aSide)
 {
+  mIsMainThreadProtocol = NS_IsMainThread();
   SetOtherProcessId(base::GetCurrentProcId());
   return GetIPCChannel()->Open(aChannel, aMessageLoop, aSide);
 }
 
 void
 IToplevelProtocol::Close()
 {
   GetIPCChannel()->Close();
@@ -810,16 +813,23 @@ IToplevelProtocol::ShmemDestroyed(const 
     Shmem::Dealloc(Shmem::IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead(), rawmem);
   }
   return true;
 }
 
 already_AddRefed<nsIEventTarget>
 IToplevelProtocol::GetMessageEventTarget(const Message& aMsg)
 {
+  if (IsMainThreadProtocol() && SystemGroup::Initialized()) {
+    if (aMsg.type() == SHMEM_CREATED_MESSAGE_TYPE ||
+        aMsg.type() == SHMEM_DESTROYED_MESSAGE_TYPE) {
+      return do_AddRef(SystemGroup::EventTargetFor(TaskCategory::Other));
+    }
+  }
+
   int32_t route = aMsg.routing_id();
 
   Maybe<MutexAutoLock> lock;
   lock.emplace(mEventTargetMutex);
 
   nsCOMPtr<nsIEventTarget> target = mEventTargetMap.Lookup(route);
 
   if (aMsg.is_constructor()) {
--- a/ipc/glue/ProtocolUtils.h
+++ b/ipc/glue/ProtocolUtils.h
@@ -381,16 +381,19 @@ public:
 
     already_AddRefed<nsIEventTarget>
     GetActorEventTarget(IProtocol* aActor);
 
     virtual nsIEventTarget*
     GetActorEventTarget();
 
     virtual void OnChannelReceivedMessage(const Message& aMsg) {}
+
+    bool IsMainThreadProtocol() const { return mIsMainThreadProtocol; }
+
 protected:
     // Override this method in top-level protocols to change the event target
     // for a new actor (and its sub-actors).
     virtual already_AddRefed<nsIEventTarget>
     GetConstructedEventTarget(const Message& aMsg) { return nullptr; }
 
     // Override this method in top-level protocols to change the event target
     // for specific messages.
@@ -408,16 +411,17 @@ protected:
   private:
     ProtocolId mProtocolId;
     UniquePtr<Transport> mTrans;
     base::ProcessId mOtherPid;
     IDMap<IProtocol*> mActorMap;
     int32_t mLastRouteId;
     IDMap<Shmem::SharedMemory*> mShmemMap;
     Shmem::id_t mLastShmemId;
+    bool mIsMainThreadProtocol;
 
     Mutex mEventTargetMutex;
     IDMap<nsCOMPtr<nsIEventTarget>> mEventTargetMap;
 };
 
 class IShmemAllocator
 {
 public:
--- a/xpcom/threads/SystemGroup.cpp
+++ b/xpcom/threads/SystemGroup.cpp
@@ -17,16 +17,18 @@ class SystemGroupImpl final : public Sch
 public:
   SystemGroupImpl();
   ~SystemGroupImpl() {}
 
   static void InitStatic();
   static void ShutdownStatic();
   static SystemGroupImpl* Get();
 
+  static bool Initialized() { return !!sSingleton; }
+
   NS_METHOD_(MozExternalRefCountType) AddRef(void)
   {
     return 2;
   }
   NS_METHOD_(MozExternalRefCountType) Release(void)
   {
     return 1;
   }
@@ -71,16 +73,22 @@ SystemGroup::InitStatic()
 }
 
 void
 SystemGroup::Shutdown()
 {
   SystemGroupImpl::ShutdownStatic();
 }
 
+bool
+SystemGroup::Initialized()
+{
+  return SystemGroupImpl::Initialized();
+}
+
 /* static */ nsresult
 SystemGroup::Dispatch(const char* aName,
                       TaskCategory aCategory,
                       already_AddRefed<nsIRunnable>&& aRunnable)
 {
   return SystemGroupImpl::Get()->Dispatch(aName, aCategory, Move(aRunnable));
 }
 
--- a/xpcom/threads/SystemGroup.h
+++ b/xpcom/threads/SystemGroup.h
@@ -29,14 +29,17 @@ class SystemGroup
   static nsIEventTarget* EventTargetFor(TaskCategory aCategory);
 
   // Must be called on the main thread. The AbstractThread can always be used
   // off the main thread.
   static AbstractThread* AbstractMainThreadFor(TaskCategory aCategory);
 
   static void InitStatic();
   static void Shutdown();
+
+  // Returns true if SystemGroup has been initialized.
+  static bool Initialized();
 };
 
 } // namespace mozilla
 
 #endif // mozilla_SystemGroup_h