Backed out 2 changesets (bug 1816628) for causing OS X mochitests-plain failures...
[gecko.git] / ipc / glue / CrossProcessSemaphore.h
blob466f88f8c39c3fcb9b425afa1aa5f5630baac32d
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(XP_WIN) || defined(XP_DARWIN)
15 # include "mozilla/UniquePtrExtensions.h"
16 #else
17 # include <pthread.h>
18 # include <semaphore.h>
19 # include "mozilla/ipc/SharedMemoryBasic.h"
20 # include "mozilla/Atomics.h"
21 #endif
23 namespace IPC {
24 template <typename T>
25 struct ParamTraits;
26 } // namespace IPC
29 // Provides:
31 // - CrossProcessSemaphore, a semaphore that can be shared across processes
32 namespace mozilla {
34 template <typename T>
35 inline bool IsHandleValid(const T& handle) {
36 return bool(handle);
39 #if defined(XP_WIN)
40 typedef mozilla::UniqueFileHandle CrossProcessSemaphoreHandle;
41 #elif defined(XP_DARWIN)
42 typedef mozilla::UniqueMachSendRight CrossProcessSemaphoreHandle;
43 #else
44 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessSemaphoreHandle;
46 template <>
47 inline bool IsHandleValid<CrossProcessSemaphoreHandle>(
48 const CrossProcessSemaphoreHandle& handle) {
49 return !(handle == mozilla::ipc::SharedMemoryBasic::NULLHandle());
51 #endif
53 class CrossProcessSemaphore {
54 public:
55 /**
56 * CrossProcessSemaphore
57 * @param name A name which can reference this lock (currently unused)
58 **/
59 static CrossProcessSemaphore* Create(const char* aName,
60 uint32_t aInitialValue);
62 /**
63 * CrossProcessSemaphore
64 * @param handle A handle of an existing cross process semaphore that can be
65 * opened.
67 static CrossProcessSemaphore* Create(CrossProcessSemaphoreHandle aHandle);
69 ~CrossProcessSemaphore();
71 /**
72 * Decrements the current value of the semaphore. This will block if the
73 * current value of the semaphore is 0, up to a maximum of aWaitTime (if
74 * specified).
75 * Returns true if the semaphore was succesfully decremented, false otherwise.
76 **/
77 bool Wait(const Maybe<TimeDuration>& aWaitTime = Nothing());
79 /**
80 * Increments the current value of the semaphore.
81 **/
82 void Signal();
84 /**
85 * CloneHandle
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.
89 * @returns A handle that can be shared to another process
91 CrossProcessSemaphoreHandle CloneHandle();
93 void CloseHandle();
95 private:
96 friend struct IPC::ParamTraits<CrossProcessSemaphore>;
98 CrossProcessSemaphore();
99 CrossProcessSemaphore(const CrossProcessSemaphore&);
100 CrossProcessSemaphore& operator=(const CrossProcessSemaphore&);
102 #if defined(XP_WIN)
103 explicit CrossProcessSemaphore(HANDLE aSemaphore);
105 HANDLE mSemaphore;
106 #elif defined(XP_DARWIN)
107 explicit CrossProcessSemaphore(CrossProcessSemaphoreHandle aSemaphore);
109 CrossProcessSemaphoreHandle mSemaphore;
110 #else
111 RefPtr<mozilla::ipc::SharedMemoryBasic> mSharedBuffer;
112 sem_t* mSemaphore;
113 mozilla::Atomic<int32_t>* mRefCount;
114 #endif
117 } // namespace mozilla
119 #endif