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"
14 #include "SharedMemoryBasic.h"
15 #include "mozilla/Atomics.h"
16 #include "nsAutoPtr.h"
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.
36 typedef HANDLE CrossProcessMutexHandle
;
37 #elif defined(OS_LINUX)
38 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessMutexHandle
;
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
;
45 class CrossProcessMutex
50 * @param name A name which can reference this lock (currently unused)
52 explicit CrossProcessMutex(const char* aName
);
55 * @param handle A handle of an existing cross process mutex that can be
58 explicit CrossProcessMutex(CrossProcessMutexHandle aHandle
);
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
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
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
);
95 friend struct IPC::ParamTraits
<CrossProcessMutex
>;
98 CrossProcessMutex(const CrossProcessMutex
&);
99 CrossProcessMutex
&operator=(const CrossProcessMutex
&);
103 #elif defined(OS_LINUX)
104 nsRefPtr
<mozilla::ipc::SharedMemoryBasic
> mSharedBuffer
;
105 pthread_mutex_t
* mMutex
;
106 mozilla::Atomic
<int32_t>* mCount
;
110 typedef BaseAutoLock
<CrossProcessMutex
> CrossProcessMutexAutoLock
;
111 typedef BaseAutoUnlock
<CrossProcessMutex
> CrossProcessMutexAutoUnlock
;