Bug 1845811 remove unused rv variable r=padenot
[gecko.git] / ipc / glue / CrossProcessMutex_windows.cpp
blobd4bbe2f2ba862a27e5fa9b5d2c4464f6f93e8ef7
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 "CrossProcessMutex.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 CrossProcessMutex::CrossProcessMutex(const char*) {
21 // We explicitly share this using DuplicateHandle, we do -not- want this to
22 // be inherited by child processes by default! So no security attributes are
23 // given.
24 mMutex = ::CreateMutexA(nullptr, FALSE, nullptr);
25 if (!mMutex) {
26 MOZ_CRASH("This shouldn't happen - failed to create mutex!");
28 MOZ_COUNT_CTOR(CrossProcessMutex);
31 CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle) {
32 DWORD flags;
33 if (!::GetHandleInformation(aHandle.get(), &flags)) {
34 MOZ_CRASH("Attempt to construct a mutex from an invalid handle!");
36 mMutex = aHandle.release();
37 MOZ_COUNT_CTOR(CrossProcessMutex);
40 CrossProcessMutex::~CrossProcessMutex() {
41 NS_ASSERTION(mMutex, "Improper construction of mutex or double free.");
42 ::CloseHandle(mMutex);
43 MOZ_COUNT_DTOR(CrossProcessMutex);
46 void CrossProcessMutex::Lock() {
47 NS_ASSERTION(mMutex, "Improper construction of mutex.");
48 ::WaitForSingleObject(mMutex, INFINITE);
51 void CrossProcessMutex::Unlock() {
52 NS_ASSERTION(mMutex, "Improper construction of mutex.");
53 ::ReleaseMutex(mMutex);
56 CrossProcessMutexHandle CrossProcessMutex::CloneHandle() {
57 HANDLE newHandle;
58 if (!::DuplicateHandle(GetCurrentProcess(), mMutex, GetCurrentProcess(),
59 &newHandle, 0, false, DUPLICATE_SAME_ACCESS)) {
60 return nullptr;
62 return mozilla::UniqueFileHandle(newHandle);
65 } // namespace mozilla