michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_CrossProcessMutex_h michael@0: #define mozilla_CrossProcessMutex_h michael@0: michael@0: #include "base/process.h" michael@0: #include "mozilla/Mutex.h" michael@0: michael@0: #if defined(OS_LINUX) michael@0: #include michael@0: #include "SharedMemoryBasic.h" michael@0: #include "mozilla/Atomics.h" michael@0: #endif michael@0: michael@0: namespace IPC { michael@0: template michael@0: struct ParamTraits; michael@0: } michael@0: michael@0: // michael@0: // Provides: michael@0: // michael@0: // - CrossProcessMutex, a non-recursive mutex that can be shared across processes michael@0: // - CrossProcessMutexAutoLock, an RAII class for ensuring that Mutexes are michael@0: // properly locked and unlocked michael@0: // michael@0: // Using CrossProcessMutexAutoLock/CrossProcessMutexAutoUnlock is MUCH michael@0: // preferred to making bare calls to CrossProcessMutex.Lock and Unlock. michael@0: // michael@0: namespace mozilla { michael@0: #ifdef XP_WIN michael@0: typedef HANDLE CrossProcessMutexHandle; michael@0: #elif defined(OS_LINUX) michael@0: typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessMutexHandle; michael@0: #else michael@0: // Stub for other platforms. We can't use uintptr_t here since different michael@0: // processes could disagree on its size. michael@0: typedef uintptr_t CrossProcessMutexHandle; michael@0: #endif michael@0: michael@0: class NS_COM_GLUE CrossProcessMutex michael@0: { michael@0: public: michael@0: /** michael@0: * CrossProcessMutex michael@0: * @param name A name which can reference this lock (currently unused) michael@0: **/ michael@0: CrossProcessMutex(const char* aName); michael@0: /** michael@0: * CrossProcessMutex michael@0: * @param handle A handle of an existing cross process mutex that can be michael@0: * opened. michael@0: */ michael@0: CrossProcessMutex(CrossProcessMutexHandle aHandle); michael@0: michael@0: /** michael@0: * ~CrossProcessMutex michael@0: **/ michael@0: ~CrossProcessMutex(); michael@0: michael@0: /** michael@0: * Lock michael@0: * This will lock the mutex. Any other thread in any other process that michael@0: * has access to this mutex calling lock will block execution until the michael@0: * initial caller of lock has made a call to Unlock. michael@0: * michael@0: * If the owning process is terminated unexpectedly the mutex will be michael@0: * released. michael@0: **/ michael@0: void Lock(); michael@0: michael@0: /** michael@0: * Unlock michael@0: * This will unlock the mutex. A single thread currently waiting on a lock michael@0: * call will resume execution and aquire ownership of the lock. No michael@0: * guarantees are made as to the order in which waiting threads will resume michael@0: * execution. michael@0: **/ michael@0: void Unlock(); michael@0: michael@0: /** michael@0: * ShareToProcess michael@0: * This function is called to generate a serializable structure that can michael@0: * be sent to the specified process and opened on the other side. michael@0: * michael@0: * @returns A handle that can be shared to another process michael@0: */ michael@0: CrossProcessMutexHandle ShareToProcess(base::ProcessHandle aTarget); michael@0: michael@0: private: michael@0: friend struct IPC::ParamTraits; michael@0: michael@0: CrossProcessMutex(); michael@0: CrossProcessMutex(const CrossProcessMutex&); michael@0: CrossProcessMutex &operator=(const CrossProcessMutex&); michael@0: michael@0: #ifdef XP_WIN michael@0: HANDLE mMutex; michael@0: #elif defined(OS_LINUX) michael@0: mozilla::ipc::SharedMemoryBasic* mSharedBuffer; michael@0: pthread_mutex_t* mMutex; michael@0: mozilla::Atomic* mCount; michael@0: #endif michael@0: }; michael@0: michael@0: typedef BaseAutoLock CrossProcessMutexAutoLock; michael@0: typedef BaseAutoUnlock CrossProcessMutexAutoUnlock; michael@0: michael@0: } michael@0: #endif