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/. */
9 #include "base/process_util.h"
10 #include "CrossProcessSemaphore.h"
12 #include "nsISupportsImpl.h"
13 #include "ProtocolUtils.h"
15 using base::GetCurrentProcessHandle
;
16 using base::ProcessHandle
;
21 CrossProcessSemaphore
* CrossProcessSemaphore::Create(const char*,
22 uint32_t aInitialValue
) {
23 // We explicitly share this using DuplicateHandle, we do -not- want this to
24 // be inherited by child processes by default! So no security attributes are
27 ::CreateSemaphoreA(nullptr, aInitialValue
, 0x7fffffff, nullptr);
31 return new CrossProcessSemaphore(semaphore
);
35 CrossProcessSemaphore
* CrossProcessSemaphore::Create(
36 CrossProcessSemaphoreHandle aHandle
) {
38 if (!::GetHandleInformation(aHandle
.get(), &flags
)) {
42 return new CrossProcessSemaphore(aHandle
.release());
45 CrossProcessSemaphore::CrossProcessSemaphore(HANDLE aSemaphore
)
46 : mSemaphore(aSemaphore
) {
47 MOZ_COUNT_CTOR(CrossProcessSemaphore
);
50 CrossProcessSemaphore::~CrossProcessSemaphore() {
51 MOZ_ASSERT(mSemaphore
, "Improper construction of semaphore or double free.");
52 ::CloseHandle(mSemaphore
);
53 MOZ_COUNT_DTOR(CrossProcessSemaphore
);
56 bool CrossProcessSemaphore::Wait(const Maybe
<TimeDuration
>& aWaitTime
) {
57 MOZ_ASSERT(mSemaphore
, "Improper construction of semaphore.");
58 HRESULT hr
= ::WaitForSingleObject(
59 mSemaphore
, aWaitTime
.isSome() ? aWaitTime
->ToMilliseconds() : INFINITE
);
60 return hr
== WAIT_OBJECT_0
;
63 void CrossProcessSemaphore::Signal() {
64 MOZ_ASSERT(mSemaphore
, "Improper construction of semaphore.");
65 ::ReleaseSemaphore(mSemaphore
, 1, nullptr);
68 CrossProcessSemaphoreHandle
CrossProcessSemaphore::CloneHandle() {
71 ::DuplicateHandle(GetCurrentProcess(), mSemaphore
, GetCurrentProcess(),
72 &newHandle
, 0, false, DUPLICATE_SAME_ACCESS
);
78 return UniqueFileHandle(newHandle
);
81 void CrossProcessSemaphore::CloseHandle() {}
83 } // namespace mozilla