#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/synchronization/waitable_event.h"
#include <windows.h>
#include <stddef.h>
#include <algorithm>
#include <optional>
#include <utility>
#include "base/compiler_specific.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
namespace base {
namespace {
[[nodiscard]] debug::ScopedCrashKeyString SetLastErrorCrashKey(
DWORD last_error) {
static auto* const key = debug::AllocateCrashKeyString(
"WaitableEvent-last_error", debug::CrashKeySize::Size32);
return debug::ScopedCrashKeyString(key, NumberToString(last_error));
}
NOINLINE void ReportInvalidWaitableEventResult(DWORD result, DWORD last_error) {
SCOPED_CRASH_KEY_NUMBER("WaitableEvent", "result", result);
debug::ScopedCrashKeyString last_error_key = SetLastErrorCrashKey(last_error);
base::debug::DumpWithoutCrashing();
}
}
WaitableEvent::WaitableEvent(ResetPolicy reset_policy,
InitialState initial_state)
: handle_(CreateEvent(nullptr,
reset_policy == ResetPolicy::MANUAL,
initial_state == InitialState::SIGNALED,
nullptr)) {
CHECK(handle_.is_valid());
}
WaitableEvent::WaitableEvent(win::ScopedHandle handle)
: handle_(std::move(handle)) {
CHECK(handle_.is_valid()) << "Tried to create WaitableEvent from NULL handle";
}
void WaitableEvent::Reset() {
ResetEvent(handle_.get());
}
void WaitableEvent::SignalImpl() {
SetEvent(handle_.get());
}
bool WaitableEvent::IsSignaled() const {
DWORD result = WaitForSingleObject(handle_.get(), 0);
if (result != WAIT_OBJECT_0 && result != WAIT_TIMEOUT) {
ReportInvalidWaitableEventResult(result, ::GetLastError());
}
return result == WAIT_OBJECT_0;
}
bool WaitableEvent::TimedWaitImpl(TimeDelta wait_delta) {
const TimeTicks end_time =
wait_delta.is_max() ? TimeTicks::Max()
: subtle::TimeTicksNowIgnoringOverride() + wait_delta;
for (TimeDelta remaining = wait_delta; remaining.is_positive();
remaining = end_time - subtle::TimeTicksNowIgnoringOverride()) {
const DWORD timeout_ms =
remaining.is_max()
? INFINITE
: saturated_cast<DWORD>(remaining.InMillisecondsRoundedUp());
const DWORD result = WaitForSingleObject(handle_.get(), timeout_ms);
if (result == WAIT_OBJECT_0) {
return true;
}
if (result == WAIT_TIMEOUT) {
continue;
}
if (result == WAIT_FAILED) {
debug::ScopedCrashKeyString last_error_key =
SetLastErrorCrashKey(::GetLastError());
NOTREACHED();
}
if (wait_delta.is_max()) {
ReportInvalidWaitableEventResult(result, ::GetLastError());
}
}
return false;
}
size_t WaitableEvent::WaitManyImpl(base::span<WaitableEvent*> events) {
HANDLE handles[MAXIMUM_WAIT_OBJECTS];
CHECK_LE(events.size(), static_cast<size_t>(MAXIMUM_WAIT_OBJECTS))
<< "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany";
for (size_t i = 0; i < events.size(); ++i) {
handles[i] = events[i]->handle();
}
DWORD result =
WaitForMultipleObjects(static_cast<DWORD>(events.size()), handles,
FALSE,
INFINITE);
if (result >= WAIT_OBJECT_0 + events.size()) {
DPLOG(FATAL) << "WaitForMultipleObjects failed";
return 0;
}
return result - WAIT_OBJECT_0;
}
}