1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/CrossProcessMutex.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_CrossProcessMutex_h 1.10 +#define mozilla_CrossProcessMutex_h 1.11 + 1.12 +#include "base/process.h" 1.13 +#include "mozilla/Mutex.h" 1.14 + 1.15 +#if defined(OS_LINUX) 1.16 +#include <pthread.h> 1.17 +#include "SharedMemoryBasic.h" 1.18 +#include "mozilla/Atomics.h" 1.19 +#endif 1.20 + 1.21 +namespace IPC { 1.22 +template<typename T> 1.23 +struct ParamTraits; 1.24 +} 1.25 + 1.26 +// 1.27 +// Provides: 1.28 +// 1.29 +// - CrossProcessMutex, a non-recursive mutex that can be shared across processes 1.30 +// - CrossProcessMutexAutoLock, an RAII class for ensuring that Mutexes are 1.31 +// properly locked and unlocked 1.32 +// 1.33 +// Using CrossProcessMutexAutoLock/CrossProcessMutexAutoUnlock is MUCH 1.34 +// preferred to making bare calls to CrossProcessMutex.Lock and Unlock. 1.35 +// 1.36 +namespace mozilla { 1.37 +#ifdef XP_WIN 1.38 +typedef HANDLE CrossProcessMutexHandle; 1.39 +#elif defined(OS_LINUX) 1.40 +typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessMutexHandle; 1.41 +#else 1.42 +// Stub for other platforms. We can't use uintptr_t here since different 1.43 +// processes could disagree on its size. 1.44 +typedef uintptr_t CrossProcessMutexHandle; 1.45 +#endif 1.46 + 1.47 +class NS_COM_GLUE CrossProcessMutex 1.48 +{ 1.49 +public: 1.50 + /** 1.51 + * CrossProcessMutex 1.52 + * @param name A name which can reference this lock (currently unused) 1.53 + **/ 1.54 + CrossProcessMutex(const char* aName); 1.55 + /** 1.56 + * CrossProcessMutex 1.57 + * @param handle A handle of an existing cross process mutex that can be 1.58 + * opened. 1.59 + */ 1.60 + CrossProcessMutex(CrossProcessMutexHandle aHandle); 1.61 + 1.62 + /** 1.63 + * ~CrossProcessMutex 1.64 + **/ 1.65 + ~CrossProcessMutex(); 1.66 + 1.67 + /** 1.68 + * Lock 1.69 + * This will lock the mutex. Any other thread in any other process that 1.70 + * has access to this mutex calling lock will block execution until the 1.71 + * initial caller of lock has made a call to Unlock. 1.72 + * 1.73 + * If the owning process is terminated unexpectedly the mutex will be 1.74 + * released. 1.75 + **/ 1.76 + void Lock(); 1.77 + 1.78 + /** 1.79 + * Unlock 1.80 + * This will unlock the mutex. A single thread currently waiting on a lock 1.81 + * call will resume execution and aquire ownership of the lock. No 1.82 + * guarantees are made as to the order in which waiting threads will resume 1.83 + * execution. 1.84 + **/ 1.85 + void Unlock(); 1.86 + 1.87 + /** 1.88 + * ShareToProcess 1.89 + * This function is called to generate a serializable structure that can 1.90 + * be sent to the specified process and opened on the other side. 1.91 + * 1.92 + * @returns A handle that can be shared to another process 1.93 + */ 1.94 + CrossProcessMutexHandle ShareToProcess(base::ProcessHandle aTarget); 1.95 + 1.96 +private: 1.97 + friend struct IPC::ParamTraits<CrossProcessMutex>; 1.98 + 1.99 + CrossProcessMutex(); 1.100 + CrossProcessMutex(const CrossProcessMutex&); 1.101 + CrossProcessMutex &operator=(const CrossProcessMutex&); 1.102 + 1.103 +#ifdef XP_WIN 1.104 + HANDLE mMutex; 1.105 +#elif defined(OS_LINUX) 1.106 + mozilla::ipc::SharedMemoryBasic* mSharedBuffer; 1.107 + pthread_mutex_t* mMutex; 1.108 + mozilla::Atomic<int32_t>* mCount; 1.109 +#endif 1.110 +}; 1.111 + 1.112 +typedef BaseAutoLock<CrossProcessMutex> CrossProcessMutexAutoLock; 1.113 +typedef BaseAutoUnlock<CrossProcessMutex> CrossProcessMutexAutoUnlock; 1.114 + 1.115 +} 1.116 +#endif