Bug 1363877 - Label IPC shmem messages as SystemGroup (r=dvander)
authorBill McCloskey <billm@mozilla.com>
Wed, 10 May 2017 12:52:20 -0700
changeset 359395 465ef5e3914ab51f534be16eac3f3f05f0f30bfc
parent 359394 2184331d4ac0fe53be3759873d6eefa72a1c4f7a
child 359396 2db29775e18f03707f78b4831564c3dda1a4ce23
push id90497
push userwmccloskey@mozilla.com
push dateFri, 19 May 2017 23:38:21 +0000
treeherdermozilla-inbound@465ef5e3914a [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/MessageChannel.cpp
ipc/glue/ProtocolUtils.cpp
ipc/glue/ProtocolUtils.h
xpcom/threads/SystemGroup.cpp
xpcom/threads/SystemGroup.h
--- a/ipc/glue/MessageChannel.cpp
+++ b/ipc/glue/MessageChannel.cpp
@@ -769,16 +769,17 @@ bool
 MessageChannel::Open(Transport* aTransport, MessageLoop* aIOLoop, Side aSide)
 {
     NS_PRECONDITION(!mLink, "Open() called > once");
 
     mMonitor = new RefCountedMonitor();
     mWorkerLoop = MessageLoop::current();
     mWorkerLoopID = mWorkerLoop->id();
     mWorkerLoop->AddDestructionObserver(this);
+    mListener->SetIsMainThreadProtocol();
 
     if (!AbstractThread::GetCurrent()) {
         mWorkerLoop->AddDestructionObserver(
             new AbstractThreadWrapperCleanup(
                 MessageLoopAbstractThreadWrapper::Create(mWorkerLoop)));
     }
 
 
@@ -854,16 +855,17 @@ MessageChannel::OnOpenAsSlave(MessageCha
 }
 
 void
 MessageChannel::CommonThreadOpenInit(MessageChannel *aTargetChan, Side aSide)
 {
     mWorkerLoop = MessageLoop::current();
     mWorkerLoopID = mWorkerLoop->id();
     mWorkerLoop->AddDestructionObserver(this);
+    mListener->SetIsMainThreadProtocol();
 
     if (!AbstractThread::GetCurrent()) {
         mWorkerLoop->AddDestructionObserver(
             new AbstractThreadWrapperCleanup(
                 MessageLoopAbstractThreadWrapper::Create(mWorkerLoop)));
     }
 
     mLink = new ThreadLink(this, aTargetChan);
--- 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
 
@@ -810,16 +811,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,20 @@ public:
 
     already_AddRefed<nsIEventTarget>
     GetActorEventTarget(IProtocol* aActor);
 
     virtual nsIEventTarget*
     GetActorEventTarget();
 
     virtual void OnChannelReceivedMessage(const Message& aMsg) {}
+
+    bool IsMainThreadProtocol() const { return mIsMainThreadProtocol; }
+    void SetIsMainThreadProtocol() { mIsMainThreadProtocol = NS_IsMainThread(); }
+
 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 +412,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