Bug 1891340 - Part 1: Add parameters to customize the before and after icon tints...
[gecko.git] / ipc / glue / CrossProcessSemaphore_windows.cpp
blob70644b7d5155c2bad98f9dbaf287f733152365d8
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 #include <windows.h>
9 #include "base/process_util.h"
10 #include "CrossProcessSemaphore.h"
11 #include "nsDebug.h"
12 #include "nsISupportsImpl.h"
13 #include "ProtocolUtils.h"
15 using base::GetCurrentProcessHandle;
16 using base::ProcessHandle;
18 namespace mozilla {
20 /* static */
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
25 // given.
26 HANDLE semaphore =
27 ::CreateSemaphoreA(nullptr, aInitialValue, 0x7fffffff, nullptr);
28 if (!semaphore) {
29 return nullptr;
31 return new CrossProcessSemaphore(semaphore);
34 /* static */
35 CrossProcessSemaphore* CrossProcessSemaphore::Create(
36 CrossProcessSemaphoreHandle aHandle) {
37 DWORD flags;
38 if (!::GetHandleInformation(aHandle.get(), &flags)) {
39 return nullptr;
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() {
69 HANDLE newHandle;
70 bool succeeded =
71 ::DuplicateHandle(GetCurrentProcess(), mSemaphore, GetCurrentProcess(),
72 &newHandle, 0, false, DUPLICATE_SAME_ACCESS);
74 if (!succeeded) {
75 return nullptr;
78 return UniqueFileHandle(newHandle);
81 void CrossProcessSemaphore::CloseHandle() {}
83 } // namespace mozilla