Bug 1451363 - part 2a - move protocol register/lookup functions into ProtocolState; r=mccr8
authorNathan Froyd <froydnj@mozilla.com>
Mon, 23 Apr 2018 14:13:37 -0400
changeset 468680 78353bf75968371ecbdcb447b5452f28dd757e85
parent 468679 5b5ce8fcfdffe3631186be9e1d293f86fa68e51c
child 468681 3978808c33d927f83929f76688e9c39e185a8a4b
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1451363
milestone61.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 1451363 - part 2a - move protocol register/lookup functions into ProtocolState; r=mccr8 This functionality is base functionality for top-level and non-toplevel protocols; nobody overrides this stuff, so it's safe to move into ProtocolState.
ipc/glue/ProtocolUtils.cpp
ipc/glue/ProtocolUtils.h
--- a/ipc/glue/ProtocolUtils.cpp
+++ b/ipc/glue/ProtocolUtils.cpp
@@ -393,40 +393,40 @@ IProtocol::ReadActor(const IPC::Message*
         MismatchedActorTypeError(aActorDescription);
         return Nothing();
     }
 
     return Some(listener);
 }
 
 int32_t
-IProtocol::Register(IProtocol* aRouted)
+IProtocol::ManagedState::Register(IProtocol* aRouted)
 {
-  return Manager()->Register(aRouted);
+  return mProtocol->Manager()->Register(aRouted);
 }
 
 int32_t
-IProtocol::RegisterID(IProtocol* aRouted, int32_t aId)
+IProtocol::ManagedState::RegisterID(IProtocol* aRouted, int32_t aId)
 {
-  return Manager()->RegisterID(aRouted, aId);
+  return mProtocol->Manager()->RegisterID(aRouted, aId);
 }
 
 IProtocol*
-IProtocol::Lookup(int32_t aId)
+IProtocol::ManagedState::Lookup(int32_t aId)
 {
-  return Manager()->Lookup(aId);
+  return mProtocol->Manager()->Lookup(aId);
 }
 
 void
-IProtocol::Unregister(int32_t aId)
+IProtocol::ManagedState::Unregister(int32_t aId)
 {
-  if (mId == aId) {
-    mId = kFreedActorId;
+  if (mProtocol->mId == aId) {
+    mProtocol->mId = kFreedActorId;
   }
-  Manager()->Unregister(aId);
+  mProtocol->Manager()->Unregister(aId);
 }
 
 Shmem::SharedMemory*
 IProtocol::ManagedState::CreateSharedMemory(size_t aSize,
                                             SharedMemory::SharedMemoryType aType,
                                             bool aUnsafe,
                                             int32_t* aId)
 {
@@ -577,17 +577,16 @@ IProtocol::GetActorEventTargetInternal(I
 }
 
 IToplevelProtocol::IToplevelProtocol(ProtocolId aProtoId, Side aSide)
  : IProtocol(aSide, MakeUnique<ToplevelState>(this, aSide)),
    mMonitor("mozilla.ipc.IToplevelProtocol.mMonitor"),
    mProtocolId(aProtoId),
    mOtherPid(mozilla::ipc::kInvalidProcessId),
    mOtherPidState(ProcessIdState::eUnstarted),
-   mLastRouteId(aSide == ParentSide ? kFreedActorId : kNullActorId),
    mEventTargetMutex("ProtocolEventTargetMutex")
 {
 }
 
 IToplevelProtocol::~IToplevelProtocol()
 {
   if (mTrans) {
     RefPtr<DeleteTask<Transport>> task = new DeleteTask<Transport>(mTrans.release());
@@ -689,63 +688,64 @@ IToplevelProtocol::SetReplyTimeoutMs(int
 
 bool
 IToplevelProtocol::IsOnCxxStack() const
 {
   return GetIPCChannel()->IsOnCxxStack();
 }
 
 int32_t
-IToplevelProtocol::Register(IProtocol* aRouted)
+IToplevelProtocol::ToplevelState::Register(IProtocol* aRouted)
 {
   if (aRouted->Id() != kNullActorId && aRouted->Id() != kFreedActorId) {
     // If there's already an ID, just return that.
     return aRouted->Id();
   }
-  int32_t id = GetSide() == ParentSide ? ++mLastRouteId : --mLastRouteId;
+  int32_t id = mProtocol->GetSide() == ParentSide ? ++mLastRouteId : --mLastRouteId;
   mActorMap.AddWithID(aRouted, id);
   aRouted->SetId(id);
 
   // Inherit our event target from our manager.
   if (IProtocol* manager = aRouted->Manager()) {
     MutexAutoLock lock(mEventTargetMutex);
     if (nsCOMPtr<nsIEventTarget> target = mEventTargetMap.Lookup(manager->Id())) {
       mEventTargetMap.AddWithID(target, id);
     }
   }
 
   return id;
 }
 
 int32_t
-IToplevelProtocol::RegisterID(IProtocol* aRouted,
-                              int32_t aId)
+IToplevelProtocol::ToplevelState::RegisterID(IProtocol* aRouted,
+                                     int32_t aId)
 {
   mActorMap.AddWithID(aRouted, aId);
   aRouted->SetId(aId);
   return aId;
 }
 
 IProtocol*
-IToplevelProtocol::Lookup(int32_t aId)
+IToplevelProtocol::ToplevelState::Lookup(int32_t aId)
 {
   return mActorMap.Lookup(aId);
 }
 
 void
-IToplevelProtocol::Unregister(int32_t aId)
+IToplevelProtocol::ToplevelState::Unregister(int32_t aId)
 {
   mActorMap.Remove(aId);
 
   MutexAutoLock lock(mEventTargetMutex);
   mEventTargetMap.RemoveIfPresent(aId);
 }
 
 IToplevelProtocol::ToplevelState::ToplevelState(IToplevelProtocol* aProtocol, Side aSide)
   : mProtocol(aProtocol)
+  , mLastRouteId(aSide == ParentSide ? kFreedActorId : kNullActorId)
   , mLastShmemId(aSide == ParentSide ? kFreedActorId : kNullActorId)
 {
 }
 
 Shmem::SharedMemory*
 IToplevelProtocol::ToplevelState::CreateSharedMemory(size_t aSize,
                                                      Shmem::SharedMemory::SharedMemoryType aType,
                                                      bool aUnsafe,
--- a/ipc/glue/ProtocolUtils.h
+++ b/ipc/glue/ProtocolUtils.h
@@ -154,22 +154,30 @@ public:
     // This class (and its two subclasses) ensure that we don't have a bunch
     // of redundant entries in protocol vtables: we have a single vtable per
     // subclass, and then each protocol has its own instance of one of the
     // subclasses.  This setup makes things a bit slower, but the space
     // savings are worth it.
     class ProtocolState
     {
     public:
+        virtual ~ProtocolState() = default;
+
+        // Shared memory functions.
         virtual Shmem::SharedMemory* CreateSharedMemory(
             size_t, SharedMemory::SharedMemoryType, bool, int32_t*) = 0;
         virtual Shmem::SharedMemory* LookupSharedMemory(int32_t) = 0;
         virtual bool IsTrackingSharedMemory(Shmem::SharedMemory*) = 0;
         virtual bool DestroySharedMemory(Shmem&) = 0;
-        virtual ~ProtocolState() = default;
+
+        // Protocol management functions.
+        virtual int32_t Register(IProtocol*) = 0;
+        virtual int32_t RegisterID(IProtocol*, int32_t) = 0;
+        virtual IProtocol* Lookup(int32_t) = 0;
+        virtual void Unregister(int32_t) = 0;
     };
 
     // Managed protocols just forward all of their operations to the topmost
     // managing protocol.
     class ManagedState final : public ProtocolState
     {
     public:
         explicit ManagedState(IProtocol* aProtocol)
@@ -177,32 +185,50 @@ public:
         {}
 
         Shmem::SharedMemory* CreateSharedMemory(
             size_t, SharedMemory::SharedMemoryType, bool, int32_t*) override;
         Shmem::SharedMemory* LookupSharedMemory(int32_t) override;
         bool IsTrackingSharedMemory(Shmem::SharedMemory*) override;
         bool DestroySharedMemory(Shmem&) override;
 
+        int32_t Register(IProtocol*) override;
+        int32_t RegisterID(IProtocol*, int32_t) override;
+        IProtocol* Lookup(int32_t) override;
+        void Unregister(int32_t) override;
+
     private:
         IProtocol* const mProtocol;
     };
 
     typedef base::ProcessId ProcessId;
     typedef IPC::Message Message;
     typedef IPC::MessageInfo MessageInfo;
 
     explicit IProtocol(Side aSide)
         : IProtocol(aSide, MakeUnique<ManagedState>(this))
     {}
 
-    virtual int32_t Register(IProtocol*);
-    virtual int32_t RegisterID(IProtocol*, int32_t);
-    virtual IProtocol* Lookup(int32_t);
-    virtual void Unregister(int32_t);
+    int32_t Register(IProtocol* aRouted)
+    {
+        return mState->Register(aRouted);
+    }
+    int32_t RegisterID(IProtocol* aRouted, int32_t aId)
+    {
+        return mState->RegisterID(aRouted, aId);
+    }
+    IProtocol* Lookup(int32_t aId)
+    {
+        return mState->Lookup(aId);
+    }
+    void Unregister(int32_t aId)
+    {
+        return mState->Unregister(aId);
+    }
+
     virtual void RemoveManagee(int32_t, IProtocol*) = 0;
 
     Shmem::SharedMemory* CreateSharedMemory(
         size_t aSize, SharedMemory::SharedMemoryType aType, bool aUnsafe, int32_t* aId)
     {
         return mState->CreateSharedMemory(aSize, aType, aUnsafe, aId);
     }
     Shmem::SharedMemory* LookupSharedMemory(int32_t aId)
@@ -354,18 +380,25 @@ public:
         bool IsTrackingSharedMemory(Shmem::SharedMemory*) override;
         bool DestroySharedMemory(Shmem&) override;
 
         void DeallocShmems();
 
         bool ShmemCreated(const Message& aMsg);
         bool ShmemDestroyed(const Message& aMsg);
 
+        int32_t Register(IProtocol*) override;
+        int32_t RegisterID(IProtocol*, int32_t) override;
+        IProtocol* Lookup(int32_t) override;
+        void Unregister(int32_t) override;
+
     private:
         IToplevelProtocol* const mProtocol;
+        IDMap<IProtocol*> mActorMap;
+        int32_t mLastRouteId;
         IDMap<Shmem::SharedMemory*> mShmemMap;
         Shmem::id_t mLastShmemId;
     };
 
     using SchedulerGroupSet = nsILabelableRunnable::SchedulerGroupSet;
 
     void SetTransport(UniquePtr<Transport> aTrans)
     {
@@ -403,21 +436,16 @@ public:
     bool OpenWithAsyncPid(mozilla::ipc::Transport* aTransport,
                           MessageLoop* aThread = nullptr,
                           mozilla::ipc::Side aSide = mozilla::ipc::UnknownSide);
 
     void Close();
 
     void SetReplyTimeoutMs(int32_t aTimeoutMs);
 
-    virtual int32_t Register(IProtocol*) override;
-    virtual int32_t RegisterID(IProtocol*, int32_t) override;
-    virtual IProtocol* Lookup(int32_t) override;
-    virtual void Unregister(int32_t) override;
-
     void DeallocShmems() { DowncastState()->DeallocShmems(); }
 
     bool ShmemCreated(const Message& aMsg) { return DowncastState()->ShmemCreated(aMsg); }
     bool ShmemDestroyed(const Message& aMsg) { return DowncastState()->ShmemDestroyed(aMsg); }
 
     virtual bool ShouldContinueFromReplyTimeout() {
         return false;
     }
@@ -535,18 +563,16 @@ protected:
     mutable mozilla::Monitor mMonitor;
   private:
     base::ProcessId OtherPidMaybeInvalid() const;
 
     ProtocolId mProtocolId;
     UniquePtr<Transport> mTrans;
     base::ProcessId mOtherPid;
     ProcessIdState mOtherPidState;
-    IDMap<IProtocol*> mActorMap;
-    int32_t mLastRouteId;
     bool mIsMainThreadProtocol;
 
     Mutex mEventTargetMutex;
     IDMap<nsCOMPtr<nsIEventTarget>> mEventTargetMap;
 };
 
 class IShmemAllocator
 {