ipc/glue/CrossProcessMutex.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef mozilla_CrossProcessMutex_h
     7 #define mozilla_CrossProcessMutex_h
     9 #include "base/process.h"
    10 #include "mozilla/Mutex.h"
    12 #if defined(OS_LINUX)
    13 #include <pthread.h>
    14 #include "SharedMemoryBasic.h"
    15 #include "mozilla/Atomics.h"
    16 #endif
    18 namespace IPC {
    19 template<typename T>
    20 struct ParamTraits;
    21 }
    23 //
    24 // Provides:
    25 //
    26 //  - CrossProcessMutex, a non-recursive mutex that can be shared across processes
    27 //  - CrossProcessMutexAutoLock, an RAII class for ensuring that Mutexes are
    28 //    properly locked and unlocked
    29 //
    30 // Using CrossProcessMutexAutoLock/CrossProcessMutexAutoUnlock is MUCH
    31 // preferred to making bare calls to CrossProcessMutex.Lock and Unlock.
    32 //
    33 namespace mozilla {
    34 #ifdef XP_WIN
    35 typedef HANDLE CrossProcessMutexHandle;
    36 #elif defined(OS_LINUX)
    37 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessMutexHandle;
    38 #else
    39 // Stub for other platforms. We can't use uintptr_t here since different
    40 // processes could disagree on its size.
    41 typedef uintptr_t CrossProcessMutexHandle;
    42 #endif
    44 class NS_COM_GLUE CrossProcessMutex
    45 {
    46 public:
    47   /**
    48    * CrossProcessMutex
    49    * @param name A name which can reference this lock (currently unused)
    50    **/
    51   CrossProcessMutex(const char* aName);
    52   /**
    53    * CrossProcessMutex
    54    * @param handle A handle of an existing cross process mutex that can be
    55    *               opened.
    56    */
    57   CrossProcessMutex(CrossProcessMutexHandle aHandle);
    59   /**
    60    * ~CrossProcessMutex
    61    **/
    62   ~CrossProcessMutex();
    64   /**
    65    * Lock
    66    * This will lock the mutex. Any other thread in any other process that
    67    * has access to this mutex calling lock will block execution until the
    68    * initial caller of lock has made a call to Unlock.
    69    *
    70    * If the owning process is terminated unexpectedly the mutex will be
    71    * released.
    72    **/
    73   void Lock();
    75   /**
    76    * Unlock
    77    * This will unlock the mutex. A single thread currently waiting on a lock
    78    * call will resume execution and aquire ownership of the lock. No
    79    * guarantees are made as to the order in which waiting threads will resume
    80    * execution.
    81    **/
    82   void Unlock();
    84   /**
    85    * ShareToProcess
    86    * This function is called to generate a serializable structure that can
    87    * be sent to the specified process and opened on the other side.
    88    *
    89    * @returns A handle that can be shared to another process
    90    */
    91   CrossProcessMutexHandle ShareToProcess(base::ProcessHandle aTarget);
    93 private:
    94   friend struct IPC::ParamTraits<CrossProcessMutex>;
    96   CrossProcessMutex();
    97   CrossProcessMutex(const CrossProcessMutex&);
    98   CrossProcessMutex &operator=(const CrossProcessMutex&);
   100 #ifdef XP_WIN
   101   HANDLE mMutex;
   102 #elif defined(OS_LINUX)
   103   mozilla::ipc::SharedMemoryBasic* mSharedBuffer;
   104   pthread_mutex_t* mMutex;
   105   mozilla::Atomic<int32_t>* mCount;
   106 #endif
   107 };
   109 typedef BaseAutoLock<CrossProcessMutex> CrossProcessMutexAutoLock;
   110 typedef BaseAutoUnlock<CrossProcessMutex> CrossProcessMutexAutoUnlock;
   112 }
   113 #endif

mercurial