tor-browser

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

CrossProcessSemaphore_windows.cpp (2551B)


      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 "CrossProcessSemaphore.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 /* static */
     21 CrossProcessSemaphore* CrossProcessSemaphore::Create(const char*,
     22                                                     uint32_t aInitialValue) {
     23  // We explicitly share this using DuplicateHandle, we do -not- want this to
     24  // be inherited by child processes by default! So no security attributes are
     25  // given.
     26  HANDLE semaphore =
     27      ::CreateSemaphoreA(nullptr, aInitialValue, 0x7fffffff, nullptr);
     28  if (!semaphore) {
     29    return nullptr;
     30  }
     31  return new CrossProcessSemaphore(semaphore);
     32 }
     33 
     34 /* static */
     35 CrossProcessSemaphore* CrossProcessSemaphore::Create(
     36    CrossProcessSemaphoreHandle aHandle) {
     37  DWORD flags;
     38  if (!::GetHandleInformation(aHandle.get(), &flags)) {
     39    return nullptr;
     40  }
     41 
     42  return new CrossProcessSemaphore(aHandle.release());
     43 }
     44 
     45 CrossProcessSemaphore::CrossProcessSemaphore(HANDLE aSemaphore)
     46    : mSemaphore(aSemaphore) {
     47  MOZ_COUNT_CTOR(CrossProcessSemaphore);
     48 }
     49 
     50 CrossProcessSemaphore::~CrossProcessSemaphore() {
     51  MOZ_ASSERT(mSemaphore, "Improper construction of semaphore or double free.");
     52  ::CloseHandle(mSemaphore);
     53  MOZ_COUNT_DTOR(CrossProcessSemaphore);
     54 }
     55 
     56 bool CrossProcessSemaphore::Wait(const Maybe<TimeDuration>& aWaitTime) {
     57  MOZ_ASSERT(mSemaphore, "Improper construction of semaphore.");
     58  HRESULT hr = ::WaitForSingleObject(
     59      mSemaphore, aWaitTime.isSome() ? aWaitTime->ToMilliseconds() : INFINITE);
     60  return hr == WAIT_OBJECT_0;
     61 }
     62 
     63 void CrossProcessSemaphore::Signal() {
     64  MOZ_ASSERT(mSemaphore, "Improper construction of semaphore.");
     65  ::ReleaseSemaphore(mSemaphore, 1, nullptr);
     66 }
     67 
     68 CrossProcessSemaphoreHandle CrossProcessSemaphore::CloneHandle() {
     69  HANDLE newHandle;
     70  bool succeeded =
     71      ::DuplicateHandle(GetCurrentProcess(), mSemaphore, GetCurrentProcess(),
     72                        &newHandle, 0, false, DUPLICATE_SAME_ACCESS);
     73 
     74  if (!succeeded) {
     75    return nullptr;
     76  }
     77 
     78  return UniqueFileHandle(newHandle);
     79 }
     80 
     81 void CrossProcessSemaphore::CloseHandle() {}
     82 
     83 }  // namespace mozilla