1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_CrossProcessMutex_h
8 #define mozilla_CrossProcessMutex_h
10 #include "base/process.h"
11 #include "mozilla/Mutex.h"
14 # include "mozilla/UniquePtrExtensions.h"
16 #if !defined(XP_WIN) && !defined(XP_NETBSD) && !defined(XP_OPENBSD)
18 # include "mozilla/ipc/SharedMemory.h"
19 # include "mozilla/Atomics.h"
30 // - CrossProcessMutex, a non-recursive mutex that can be shared across
32 // - CrossProcessMutexAutoLock, an RAII class for ensuring that Mutexes are
33 // properly locked and unlocked
35 // Using CrossProcessMutexAutoLock/CrossProcessMutexAutoUnlock is MUCH
36 // preferred to making bare calls to CrossProcessMutex.Lock and Unlock.
40 typedef mozilla::UniqueFileHandle CrossProcessMutexHandle
;
41 #elif !defined(XP_NETBSD) && !defined(XP_OPENBSD)
42 typedef mozilla::ipc::SharedMemory::Handle CrossProcessMutexHandle
;
44 // Stub for other platforms. We can't use uintptr_t here since different
45 // processes could disagree on its size.
46 typedef uintptr_t CrossProcessMutexHandle
;
49 class CrossProcessMutex
{
53 * @param name A name which can reference this lock (currently unused)
55 explicit CrossProcessMutex(const char* aName
);
58 * @param handle A handle of an existing cross process mutex that can be
61 explicit CrossProcessMutex(CrossProcessMutexHandle aHandle
);
70 * This will lock the mutex. Any other thread in any other process that
71 * has access to this mutex calling lock will block execution until the
72 * initial caller of lock has made a call to Unlock.
74 * If the owning process is terminated unexpectedly the mutex will be
81 * This will unlock the mutex. A single thread currently waiting on a lock
82 * call will resume execution and aquire ownership of the lock. No
83 * guarantees are made as to the order in which waiting threads will resume
90 * This function is called to generate a serializable structure that can
91 * be sent to the specified process and opened on the other side.
93 * @returns A handle that can be shared to another process
95 CrossProcessMutexHandle
CloneHandle();
98 friend struct IPC::ParamTraits
<CrossProcessMutex
>;
101 CrossProcessMutex(const CrossProcessMutex
&);
102 CrossProcessMutex
& operator=(const CrossProcessMutex
&);
106 #elif !defined(XP_NETBSD) && !defined(XP_OPENBSD)
107 RefPtr
<mozilla::ipc::SharedMemory
> mSharedBuffer
;
108 pthread_mutex_t
* mMutex
;
109 mozilla::Atomic
<int32_t>* mCount
;
113 typedef detail::BaseAutoLock
<CrossProcessMutex
&> CrossProcessMutexAutoLock
;
114 typedef detail::BaseAutoUnlock
<CrossProcessMutex
&> CrossProcessMutexAutoUnlock
;
116 } // namespace mozilla