michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "base/process_util.h" michael@0: #include "CrossProcessMutex.h" michael@0: #include "nsDebug.h" michael@0: #include "nsISupportsImpl.h" michael@0: michael@0: using namespace base; michael@0: michael@0: namespace mozilla { michael@0: michael@0: CrossProcessMutex::CrossProcessMutex(const char*) michael@0: { michael@0: // We explicitly share this using DuplicateHandle, we do -not- want this to michael@0: // be inherited by child processes by default! So no security attributes are michael@0: // given. michael@0: mMutex = ::CreateMutexA(nullptr, FALSE, nullptr); michael@0: if (!mMutex) { michael@0: NS_RUNTIMEABORT("This shouldn't happen - failed to create mutex!"); michael@0: } michael@0: MOZ_COUNT_CTOR(CrossProcessMutex); michael@0: } michael@0: michael@0: CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle) michael@0: { michael@0: DWORD flags; michael@0: if (!::GetHandleInformation(aHandle, &flags)) { michael@0: NS_RUNTIMEABORT("Attempt to construct a mutex from an invalid handle!"); michael@0: } michael@0: mMutex = aHandle; michael@0: MOZ_COUNT_CTOR(CrossProcessMutex); michael@0: } michael@0: michael@0: CrossProcessMutex::~CrossProcessMutex() michael@0: { michael@0: NS_ASSERTION(mMutex, "Improper construction of mutex or double free."); michael@0: ::CloseHandle(mMutex); michael@0: MOZ_COUNT_DTOR(CrossProcessMutex); michael@0: } michael@0: michael@0: void michael@0: CrossProcessMutex::Lock() michael@0: { michael@0: NS_ASSERTION(mMutex, "Improper construction of mutex."); michael@0: ::WaitForSingleObject(mMutex, INFINITE); michael@0: } michael@0: michael@0: void michael@0: CrossProcessMutex::Unlock() michael@0: { michael@0: NS_ASSERTION(mMutex, "Improper construction of mutex."); michael@0: ::ReleaseMutex(mMutex); michael@0: } michael@0: michael@0: CrossProcessMutexHandle michael@0: CrossProcessMutex::ShareToProcess(ProcessHandle aHandle) michael@0: { michael@0: HANDLE newHandle; michael@0: bool succeeded = ::DuplicateHandle(GetCurrentProcessHandle(), michael@0: mMutex, aHandle, &newHandle, michael@0: 0, FALSE, DUPLICATE_SAME_ACCESS); michael@0: michael@0: if (!succeeded) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return newHandle; michael@0: } michael@0: michael@0: }