1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/CrossProcessMutex_windows.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,75 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include <windows.h> 1.10 + 1.11 +#include "base/process_util.h" 1.12 +#include "CrossProcessMutex.h" 1.13 +#include "nsDebug.h" 1.14 +#include "nsISupportsImpl.h" 1.15 + 1.16 +using namespace base; 1.17 + 1.18 +namespace mozilla { 1.19 + 1.20 +CrossProcessMutex::CrossProcessMutex(const char*) 1.21 +{ 1.22 + // We explicitly share this using DuplicateHandle, we do -not- want this to 1.23 + // be inherited by child processes by default! So no security attributes are 1.24 + // given. 1.25 + mMutex = ::CreateMutexA(nullptr, FALSE, nullptr); 1.26 + if (!mMutex) { 1.27 + NS_RUNTIMEABORT("This shouldn't happen - failed to create mutex!"); 1.28 + } 1.29 + MOZ_COUNT_CTOR(CrossProcessMutex); 1.30 +} 1.31 + 1.32 +CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle) 1.33 +{ 1.34 + DWORD flags; 1.35 + if (!::GetHandleInformation(aHandle, &flags)) { 1.36 + NS_RUNTIMEABORT("Attempt to construct a mutex from an invalid handle!"); 1.37 + } 1.38 + mMutex = aHandle; 1.39 + MOZ_COUNT_CTOR(CrossProcessMutex); 1.40 +} 1.41 + 1.42 +CrossProcessMutex::~CrossProcessMutex() 1.43 +{ 1.44 + NS_ASSERTION(mMutex, "Improper construction of mutex or double free."); 1.45 + ::CloseHandle(mMutex); 1.46 + MOZ_COUNT_DTOR(CrossProcessMutex); 1.47 +} 1.48 + 1.49 +void 1.50 +CrossProcessMutex::Lock() 1.51 +{ 1.52 + NS_ASSERTION(mMutex, "Improper construction of mutex."); 1.53 + ::WaitForSingleObject(mMutex, INFINITE); 1.54 +} 1.55 + 1.56 +void 1.57 +CrossProcessMutex::Unlock() 1.58 +{ 1.59 + NS_ASSERTION(mMutex, "Improper construction of mutex."); 1.60 + ::ReleaseMutex(mMutex); 1.61 +} 1.62 + 1.63 +CrossProcessMutexHandle 1.64 +CrossProcessMutex::ShareToProcess(ProcessHandle aHandle) 1.65 +{ 1.66 + HANDLE newHandle; 1.67 + bool succeeded = ::DuplicateHandle(GetCurrentProcessHandle(), 1.68 + mMutex, aHandle, &newHandle, 1.69 + 0, FALSE, DUPLICATE_SAME_ACCESS); 1.70 + 1.71 + if (!succeeded) { 1.72 + return nullptr; 1.73 + } 1.74 + 1.75 + return newHandle; 1.76 +} 1.77 + 1.78 +}