ipc/glue/CrossProcessMutex.h

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

mercurial