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 "CrossProcessMutex.h"
12 #include "nsISupportsImpl.h"
13 #include "ProtocolUtils.h"
15 using base::GetCurrentProcessHandle
;
16 using base::ProcessHandle
;
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
24 mMutex
= ::CreateMutexA(nullptr, FALSE
, nullptr);
26 MOZ_CRASH("This shouldn't happen - failed to create mutex!");
28 MOZ_COUNT_CTOR(CrossProcessMutex
);
31 CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle
) {
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() {
58 if (!::DuplicateHandle(GetCurrentProcess(), mMutex
, GetCurrentProcess(),
59 &newHandle
, 0, false, DUPLICATE_SAME_ACCESS
)) {
62 return mozilla::UniqueFileHandle(newHandle
);
65 } // namespace mozilla