Bumping manifests a=b2g-bump
[gecko.git] / ipc / glue / CrossProcessMutex.h
blob58092ab39370495aff2108ced28701a3fd2ad2c5
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 #include "nsAutoPtr.h"
17 #endif
19 namespace IPC {
20 template<typename T>
21 struct ParamTraits;
25 // Provides:
27 // - CrossProcessMutex, a non-recursive mutex that can be shared across processes
28 // - CrossProcessMutexAutoLock, an RAII class for ensuring that Mutexes are
29 // properly locked and unlocked
31 // Using CrossProcessMutexAutoLock/CrossProcessMutexAutoUnlock is MUCH
32 // preferred to making bare calls to CrossProcessMutex.Lock and Unlock.
34 namespace mozilla {
35 #ifdef XP_WIN
36 typedef HANDLE CrossProcessMutexHandle;
37 #elif defined(OS_LINUX)
38 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessMutexHandle;
39 #else
40 // Stub for other platforms. We can't use uintptr_t here since different
41 // processes could disagree on its size.
42 typedef uintptr_t CrossProcessMutexHandle;
43 #endif
45 class CrossProcessMutex
47 public:
48 /**
49 * CrossProcessMutex
50 * @param name A name which can reference this lock (currently unused)
51 **/
52 explicit CrossProcessMutex(const char* aName);
53 /**
54 * CrossProcessMutex
55 * @param handle A handle of an existing cross process mutex that can be
56 * opened.
58 explicit CrossProcessMutex(CrossProcessMutexHandle aHandle);
60 /**
61 * ~CrossProcessMutex
62 **/
63 ~CrossProcessMutex();
65 /**
66 * Lock
67 * This will lock the mutex. Any other thread in any other process that
68 * has access to this mutex calling lock will block execution until the
69 * initial caller of lock has made a call to Unlock.
71 * If the owning process is terminated unexpectedly the mutex will be
72 * released.
73 **/
74 void Lock();
76 /**
77 * Unlock
78 * This will unlock the mutex. A single thread currently waiting on a lock
79 * call will resume execution and aquire ownership of the lock. No
80 * guarantees are made as to the order in which waiting threads will resume
81 * execution.
82 **/
83 void Unlock();
85 /**
86 * ShareToProcess
87 * This function is called to generate a serializable structure that can
88 * be sent to the specified process and opened on the other side.
90 * @returns A handle that can be shared to another process
92 CrossProcessMutexHandle ShareToProcess(base::ProcessHandle aTarget);
94 private:
95 friend struct IPC::ParamTraits<CrossProcessMutex>;
97 CrossProcessMutex();
98 CrossProcessMutex(const CrossProcessMutex&);
99 CrossProcessMutex &operator=(const CrossProcessMutex&);
101 #ifdef XP_WIN
102 HANDLE mMutex;
103 #elif defined(OS_LINUX)
104 nsRefPtr<mozilla::ipc::SharedMemoryBasic> mSharedBuffer;
105 pthread_mutex_t* mMutex;
106 mozilla::Atomic<int32_t>* mCount;
107 #endif
110 typedef BaseAutoLock<CrossProcessMutex> CrossProcessMutexAutoLock;
111 typedef BaseAutoUnlock<CrossProcessMutex> CrossProcessMutexAutoUnlock;
114 #endif