Blame ipc/glue/CrossProcessSemaphore_windows.cpp

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
#include <windows.h>
Packit f0b94e
Packit f0b94e
#include "base/process_util.h"
Packit f0b94e
#include "CrossProcessSemaphore.h"
Packit f0b94e
#include "nsDebug.h"
Packit f0b94e
#include "nsISupportsImpl.h"
Packit f0b94e
#include "ProtocolUtils.h"
Packit f0b94e
Packit f0b94e
using base::GetCurrentProcessHandle;
Packit f0b94e
using base::ProcessHandle;
Packit f0b94e
Packit f0b94e
namespace mozilla {
Packit f0b94e
Packit f0b94e
/* static */ CrossProcessSemaphore* CrossProcessSemaphore::Create(
Packit f0b94e
    const char*, uint32_t aInitialValue) {
Packit f0b94e
  // We explicitly share this using DuplicateHandle, we do -not- want this to
Packit f0b94e
  // be inherited by child processes by default! So no security attributes are
Packit f0b94e
  // given.
Packit f0b94e
  HANDLE semaphore =
Packit f0b94e
      ::CreateSemaphoreA(nullptr, aInitialValue, 0x7fffffff, nullptr);
Packit f0b94e
  if (!semaphore) {
Packit f0b94e
    return nullptr;
Packit f0b94e
  }
Packit f0b94e
  return new CrossProcessSemaphore(semaphore);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/* static */ CrossProcessSemaphore* CrossProcessSemaphore::Create(
Packit f0b94e
    CrossProcessSemaphoreHandle aHandle) {
Packit f0b94e
  DWORD flags;
Packit f0b94e
  if (!::GetHandleInformation(aHandle, &flags)) {
Packit f0b94e
    return nullptr;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return new CrossProcessSemaphore(aHandle);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
CrossProcessSemaphore::CrossProcessSemaphore(HANDLE aSemaphore)
Packit f0b94e
    : mSemaphore(aSemaphore) {
Packit f0b94e
  MOZ_COUNT_CTOR(CrossProcessSemaphore);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
CrossProcessSemaphore::~CrossProcessSemaphore() {
Packit f0b94e
  MOZ_ASSERT(mSemaphore, "Improper construction of semaphore or double free.");
Packit f0b94e
  ::CloseHandle(mSemaphore);
Packit f0b94e
  MOZ_COUNT_DTOR(CrossProcessSemaphore);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
bool CrossProcessSemaphore::Wait(const Maybe<TimeDuration>& aWaitTime) {
Packit f0b94e
  MOZ_ASSERT(mSemaphore, "Improper construction of semaphore.");
Packit f0b94e
  HRESULT hr = ::WaitForSingleObject(
Packit f0b94e
      mSemaphore, aWaitTime.isSome() ? aWaitTime->ToMilliseconds() : INFINITE);
Packit f0b94e
  return hr == WAIT_OBJECT_0;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void CrossProcessSemaphore::Signal() {
Packit f0b94e
  MOZ_ASSERT(mSemaphore, "Improper construction of semaphore.");
Packit f0b94e
  ::ReleaseSemaphore(mSemaphore, 1, nullptr);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
CrossProcessSemaphoreHandle CrossProcessSemaphore::ShareToProcess(
Packit f0b94e
    base::ProcessId aTargetPid) {
Packit f0b94e
  HANDLE newHandle;
Packit f0b94e
  bool succeeded = ipc::DuplicateHandle(mSemaphore, aTargetPid, &newHandle, 0,
Packit f0b94e
                                        DUPLICATE_SAME_ACCESS);
Packit f0b94e
Packit f0b94e
  if (!succeeded) {
Packit f0b94e
    return nullptr;
Packit f0b94e
  }
Packit f0b94e
Packit f0b94e
  return newHandle;
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
void CrossProcessSemaphore::CloseHandle() {}
Packit f0b94e
Packit f0b94e
}  // namespace mozilla