|
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/. */ |
|
5 |
|
6 #include <windows.h> |
|
7 |
|
8 #include "base/process_util.h" |
|
9 #include "CrossProcessMutex.h" |
|
10 #include "nsDebug.h" |
|
11 #include "nsISupportsImpl.h" |
|
12 |
|
13 using namespace base; |
|
14 |
|
15 namespace mozilla { |
|
16 |
|
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 } |
|
28 |
|
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 } |
|
38 |
|
39 CrossProcessMutex::~CrossProcessMutex() |
|
40 { |
|
41 NS_ASSERTION(mMutex, "Improper construction of mutex or double free."); |
|
42 ::CloseHandle(mMutex); |
|
43 MOZ_COUNT_DTOR(CrossProcessMutex); |
|
44 } |
|
45 |
|
46 void |
|
47 CrossProcessMutex::Lock() |
|
48 { |
|
49 NS_ASSERTION(mMutex, "Improper construction of mutex."); |
|
50 ::WaitForSingleObject(mMutex, INFINITE); |
|
51 } |
|
52 |
|
53 void |
|
54 CrossProcessMutex::Unlock() |
|
55 { |
|
56 NS_ASSERTION(mMutex, "Improper construction of mutex."); |
|
57 ::ReleaseMutex(mMutex); |
|
58 } |
|
59 |
|
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); |
|
67 |
|
68 if (!succeeded) { |
|
69 return nullptr; |
|
70 } |
|
71 |
|
72 return newHandle; |
|
73 } |
|
74 |
|
75 } |