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