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"
15 # include "mozilla/UniquePtrExtensions.h"
17 #if !defined(OS_WIN) && !defined(OS_MACOSX)
19 # include <semaphore.h>
20 # include "mozilla/ipc/SharedMemoryBasic.h"
21 # include "mozilla/Atomics.h"
32 // - CrossProcessSemaphore, a semaphore that can be shared across processes
36 inline bool IsHandleValid(const T
& handle
) {
41 typedef mozilla::UniqueFileHandle CrossProcessSemaphoreHandle
;
42 #elif !defined(OS_MACOSX)
43 typedef mozilla::ipc::SharedMemoryBasic::Handle CrossProcessSemaphoreHandle
;
46 inline bool IsHandleValid
<CrossProcessSemaphoreHandle
>(
47 const CrossProcessSemaphoreHandle
& handle
) {
48 return !(handle
== mozilla::ipc::SharedMemoryBasic::NULLHandle());
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
;
56 class CrossProcessSemaphore
{
59 * CrossProcessSemaphore
60 * @param name A name which can reference this lock (currently unused)
62 static CrossProcessSemaphore
* Create(const char* aName
,
63 uint32_t aInitialValue
);
66 * CrossProcessSemaphore
67 * @param handle A handle of an existing cross process semaphore that can be
70 static CrossProcessSemaphore
* Create(CrossProcessSemaphoreHandle aHandle
);
72 ~CrossProcessSemaphore();
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
78 * Returns true if the semaphore was succesfully decremented, false otherwise.
80 bool Wait(const Maybe
<TimeDuration
>& aWaitTime
= Nothing());
83 * Increments the current value of the semaphore.
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();
99 friend struct IPC::ParamTraits
<CrossProcessSemaphore
>;
101 CrossProcessSemaphore();
102 CrossProcessSemaphore(const CrossProcessSemaphore
&);
103 CrossProcessSemaphore
& operator=(const CrossProcessSemaphore
&);
106 explicit CrossProcessSemaphore(HANDLE aSemaphore
);
109 #elif !defined(OS_MACOSX)
110 RefPtr
<mozilla::ipc::SharedMemoryBasic
> mSharedBuffer
;
112 mozilla::Atomic
<int32_t>* mRefCount
;
116 } // namespace mozilla