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"
18 # include <semaphore.h>
19 # include "mozilla/ipc/SharedMemoryBasic.h"
20 # include "mozilla/Atomics.h"
31 // - CrossProcessSemaphore, a semaphore that can be shared across processes
35 inline bool IsHandleValid(const T
& handle
) {
40 typedef mozilla::UniqueFileHandle CrossProcessSemaphoreHandle
;
41 #elif defined(XP_DARWIN)
42 typedef mozilla::UniqueMachSendRight CrossProcessSemaphoreHandle
;
44 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessSemaphoreHandle
;
47 inline bool IsHandleValid
<CrossProcessSemaphoreHandle
>(
48 const CrossProcessSemaphoreHandle
& handle
) {
49 return !(handle
== mozilla::ipc::SharedMemoryBasic::NULLHandle());
53 class CrossProcessSemaphore
{
56 * CrossProcessSemaphore
57 * @param name A name which can reference this lock (currently unused)
59 static CrossProcessSemaphore
* Create(const char* aName
,
60 uint32_t aInitialValue
);
63 * CrossProcessSemaphore
64 * @param handle A handle of an existing cross process semaphore that can be
67 static CrossProcessSemaphore
* Create(CrossProcessSemaphoreHandle aHandle
);
69 ~CrossProcessSemaphore();
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
75 * Returns true if the semaphore was succesfully decremented, false otherwise.
77 bool Wait(const Maybe
<TimeDuration
>& aWaitTime
= Nothing());
80 * Increments the current value of the semaphore.
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();
96 friend struct IPC::ParamTraits
<CrossProcessSemaphore
>;
98 CrossProcessSemaphore();
99 CrossProcessSemaphore(const CrossProcessSemaphore
&);
100 CrossProcessSemaphore
& operator=(const CrossProcessSemaphore
&);
103 explicit CrossProcessSemaphore(HANDLE aSemaphore
);
106 #elif defined(XP_DARWIN)
107 explicit CrossProcessSemaphore(CrossProcessSemaphoreHandle aSemaphore
);
109 CrossProcessSemaphoreHandle mSemaphore
;
111 RefPtr
<mozilla::ipc::SharedMemoryBasic
> mSharedBuffer
;
113 mozilla::Atomic
<int32_t>* mRefCount
;
117 } // namespace mozilla