tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CrossProcessMutex_windows.cpp (2030B)


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