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