Bug 1834537 - Part 6: Simplify GCRuntime::checkAllocatorState a little r=sfink
[gecko.git] / ipc / glue / CrossProcessSemaphore.h
blob297552853ccab0673b6cef3f0fa02f8d0a8ab8dd
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_CrossProcessSemaphore_h
8 #define mozilla_CrossProcessSemaphore_h
10 #include "base/process.h"
11 #include "mozilla/TimeStamp.h"
12 #include "mozilla/Maybe.h"
14 #if defined(OS_WIN)
15 # include "mozilla/UniquePtrExtensions.h"
16 #endif
17 #if !defined(OS_WIN) && !defined(OS_MACOSX)
18 # include <pthread.h>
19 # include <semaphore.h>
20 # include "mozilla/ipc/SharedMemoryBasic.h"
21 # include "mozilla/Atomics.h"
22 #endif
24 namespace IPC {
25 template <typename T>
26 struct ParamTraits;
27 } // namespace IPC
30 // Provides:
32 // - CrossProcessSemaphore, a semaphore that can be shared across processes
33 namespace mozilla {
35 template <typename T>
36 inline bool IsHandleValid(const T& handle) {
37 return bool(handle);
40 #if defined(OS_WIN)
41 typedef mozilla::UniqueFileHandle CrossProcessSemaphoreHandle;
42 #elif !defined(OS_MACOSX)
43 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessSemaphoreHandle;
45 template <>
46 inline bool IsHandleValid<CrossProcessSemaphoreHandle>(
47 const CrossProcessSemaphoreHandle& handle) {
48 return !(handle == mozilla::ipc::SharedMemoryBasic::NULLHandle());
50 #else
51 // Stub for other platforms. We can't use uintptr_t here since different
52 // processes could disagree on its size.
53 typedef uintptr_t CrossProcessSemaphoreHandle;
54 #endif
56 class CrossProcessSemaphore {
57 public:
58 /**
59 * CrossProcessSemaphore
60 * @param name A name which can reference this lock (currently unused)
61 **/
62 static CrossProcessSemaphore* Create(const char* aName,
63 uint32_t aInitialValue);
65 /**
66 * CrossProcessSemaphore
67 * @param handle A handle of an existing cross process semaphore that can be
68 * opened.
70 static CrossProcessSemaphore* Create(CrossProcessSemaphoreHandle aHandle);
72 ~CrossProcessSemaphore();
74 /**
75 * Decrements the current value of the semaphore. This will block if the
76 * current value of the semaphore is 0, up to a maximum of aWaitTime (if
77 * specified).
78 * Returns true if the semaphore was succesfully decremented, false otherwise.
79 **/
80 bool Wait(const Maybe<TimeDuration>& aWaitTime = Nothing());
82 /**
83 * Increments the current value of the semaphore.
84 **/
85 void Signal();
87 /**
88 * CloneHandle
89 * This function is called to generate a serializable structure that can
90 * be sent to the specified process and opened on the other side.
92 * @returns A handle that can be shared to another process
94 CrossProcessSemaphoreHandle CloneHandle();
96 void CloseHandle();
98 private:
99 friend struct IPC::ParamTraits<CrossProcessSemaphore>;
101 CrossProcessSemaphore();
102 CrossProcessSemaphore(const CrossProcessSemaphore&);
103 CrossProcessSemaphore& operator=(const CrossProcessSemaphore&);
105 #if defined(OS_WIN)
106 explicit CrossProcessSemaphore(HANDLE aSemaphore);
108 HANDLE mSemaphore;
109 #elif !defined(OS_MACOSX)
110 RefPtr<mozilla::ipc::SharedMemoryBasic> mSharedBuffer;
111 sem_t* mSemaphore;
112 mozilla::Atomic<int32_t>* mRefCount;
113 #endif
116 } // namespace mozilla
118 #endif