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