Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / ipc / glue / CrossProcessMutex.h
blob3e16166c4bf656d9667330b733b3d1bec07d7857
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"
13 #if defined(XP_WIN)
14 # include "mozilla/UniquePtrExtensions.h"
15 #endif
16 #if !defined(XP_WIN) && !defined(XP_NETBSD) && !defined(XP_OPENBSD)
17 # include <pthread.h>
18 # include "mozilla/ipc/SharedMemoryBasic.h"
19 # include "mozilla/Atomics.h"
20 #endif
22 namespace IPC {
23 template <typename T>
24 struct ParamTraits;
25 } // namespace IPC
28 // Provides:
30 // - CrossProcessMutex, a non-recursive mutex that can be shared across
31 // processes
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.
38 namespace mozilla {
39 #if defined(XP_WIN)
40 typedef mozilla::UniqueFileHandle CrossProcessMutexHandle;
41 #elif !defined(XP_NETBSD) && !defined(XP_OPENBSD)
42 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessMutexHandle;
43 #else
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;
47 #endif
49 class CrossProcessMutex {
50 public:
51 /**
52 * CrossProcessMutex
53 * @param name A name which can reference this lock (currently unused)
54 **/
55 explicit CrossProcessMutex(const char* aName);
56 /**
57 * CrossProcessMutex
58 * @param handle A handle of an existing cross process mutex that can be
59 * opened.
61 explicit CrossProcessMutex(CrossProcessMutexHandle aHandle);
63 /**
64 * ~CrossProcessMutex
65 **/
66 ~CrossProcessMutex();
68 /**
69 * Lock
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
75 * released.
76 **/
77 void Lock();
79 /**
80 * Unlock
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
84 * execution.
85 **/
86 void Unlock();
88 /**
89 * CloneHandle
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();
97 private:
98 friend struct IPC::ParamTraits<CrossProcessMutex>;
100 CrossProcessMutex();
101 CrossProcessMutex(const CrossProcessMutex&);
102 CrossProcessMutex& operator=(const CrossProcessMutex&);
104 #if defined(XP_WIN)
105 HANDLE mMutex;
106 #elif !defined(XP_NETBSD) && !defined(XP_OPENBSD)
107 RefPtr<mozilla::ipc::SharedMemoryBasic> mSharedBuffer;
108 pthread_mutex_t* mMutex;
109 mozilla::Atomic<int32_t>* mCount;
110 #endif
113 typedef detail::BaseAutoLock<CrossProcessMutex&> CrossProcessMutexAutoLock;
114 typedef detail::BaseAutoUnlock<CrossProcessMutex&> CrossProcessMutexAutoUnlock;
116 } // namespace mozilla
118 #endif