ipc/glue/CrossProcessMutex_windows.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <windows.h>
michael@0 7
michael@0 8 #include "base/process_util.h"
michael@0 9 #include "CrossProcessMutex.h"
michael@0 10 #include "nsDebug.h"
michael@0 11 #include "nsISupportsImpl.h"
michael@0 12
michael@0 13 using namespace base;
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 CrossProcessMutex::CrossProcessMutex(const char*)
michael@0 18 {
michael@0 19 // We explicitly share this using DuplicateHandle, we do -not- want this to
michael@0 20 // be inherited by child processes by default! So no security attributes are
michael@0 21 // given.
michael@0 22 mMutex = ::CreateMutexA(nullptr, FALSE, nullptr);
michael@0 23 if (!mMutex) {
michael@0 24 NS_RUNTIMEABORT("This shouldn't happen - failed to create mutex!");
michael@0 25 }
michael@0 26 MOZ_COUNT_CTOR(CrossProcessMutex);
michael@0 27 }
michael@0 28
michael@0 29 CrossProcessMutex::CrossProcessMutex(CrossProcessMutexHandle aHandle)
michael@0 30 {
michael@0 31 DWORD flags;
michael@0 32 if (!::GetHandleInformation(aHandle, &flags)) {
michael@0 33 NS_RUNTIMEABORT("Attempt to construct a mutex from an invalid handle!");
michael@0 34 }
michael@0 35 mMutex = aHandle;
michael@0 36 MOZ_COUNT_CTOR(CrossProcessMutex);
michael@0 37 }
michael@0 38
michael@0 39 CrossProcessMutex::~CrossProcessMutex()
michael@0 40 {
michael@0 41 NS_ASSERTION(mMutex, "Improper construction of mutex or double free.");
michael@0 42 ::CloseHandle(mMutex);
michael@0 43 MOZ_COUNT_DTOR(CrossProcessMutex);
michael@0 44 }
michael@0 45
michael@0 46 void
michael@0 47 CrossProcessMutex::Lock()
michael@0 48 {
michael@0 49 NS_ASSERTION(mMutex, "Improper construction of mutex.");
michael@0 50 ::WaitForSingleObject(mMutex, INFINITE);
michael@0 51 }
michael@0 52
michael@0 53 void
michael@0 54 CrossProcessMutex::Unlock()
michael@0 55 {
michael@0 56 NS_ASSERTION(mMutex, "Improper construction of mutex.");
michael@0 57 ::ReleaseMutex(mMutex);
michael@0 58 }
michael@0 59
michael@0 60 CrossProcessMutexHandle
michael@0 61 CrossProcessMutex::ShareToProcess(ProcessHandle aHandle)
michael@0 62 {
michael@0 63 HANDLE newHandle;
michael@0 64 bool succeeded = ::DuplicateHandle(GetCurrentProcessHandle(),
michael@0 65 mMutex, aHandle, &newHandle,
michael@0 66 0, FALSE, DUPLICATE_SAME_ACCESS);
michael@0 67
michael@0 68 if (!succeeded) {
michael@0 69 return nullptr;
michael@0 70 }
michael@0 71
michael@0 72 return newHandle;
michael@0 73 }
michael@0 74
michael@0 75 }

mercurial