1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/synchronization/waitable_event.h"
11
12 #include <windows.h>
13
14 #include <stddef.h>
15
16 #include <algorithm>
17 #include <optional>
18 #include <utility>
19
20 #include "base/compiler_specific.h"
21 #include "base/debug/crash_logging.h"
22 #include "base/debug/dump_without_crashing.h"
23 #include "base/logging.h"
24 #include "base/notreached.h"
25 #include "base/numerics/safe_conversions.h"
26 #include "base/threading/scoped_blocking_call.h"
27 #include "base/threading/thread_restrictions.h"
28 #include "base/time/time.h"
29 #include "base/time/time_override.h"
30
31 namespace base {
32
33 namespace {
34
SetLastErrorCrashKey(DWORD last_error)35 [[nodiscard]] debug::ScopedCrashKeyString SetLastErrorCrashKey(
36 DWORD last_error) {
37 static auto* const key = debug::AllocateCrashKeyString(
38 "WaitableEvent-last_error", debug::CrashKeySize::Size32);
39 return debug::ScopedCrashKeyString(key, NumberToString(last_error));
40 }
41
ReportInvalidWaitableEventResult(DWORD result,DWORD last_error)42 NOINLINE void ReportInvalidWaitableEventResult(DWORD result, DWORD last_error) {
43 SCOPED_CRASH_KEY_NUMBER("WaitableEvent", "result", result);
44 debug::ScopedCrashKeyString last_error_key = SetLastErrorCrashKey(last_error);
45 base::debug::DumpWithoutCrashing(); // https://crbug.com/1478972.
46 }
47
48 } // namespace
49
WaitableEvent(ResetPolicy reset_policy,InitialState initial_state)50 WaitableEvent::WaitableEvent(ResetPolicy reset_policy,
51 InitialState initial_state)
52 : handle_(CreateEvent(nullptr,
53 reset_policy == ResetPolicy::MANUAL,
54 initial_state == InitialState::SIGNALED,
55 nullptr)) {
56 // We're probably going to crash anyways if this is ever NULL, so we might as
57 // well make our stack reports more informative by crashing here.
58 CHECK(handle_.is_valid());
59 }
60
WaitableEvent(win::ScopedHandle handle)61 WaitableEvent::WaitableEvent(win::ScopedHandle handle)
62 : handle_(std::move(handle)) {
63 CHECK(handle_.is_valid()) << "Tried to create WaitableEvent from NULL handle";
64 }
65
Reset()66 void WaitableEvent::Reset() {
67 ResetEvent(handle_.get());
68 }
69
SignalImpl()70 void WaitableEvent::SignalImpl() {
71 SetEvent(handle_.get());
72 }
73
IsSignaled() const74 bool WaitableEvent::IsSignaled() const {
75 DWORD result = WaitForSingleObject(handle_.get(), 0);
76 if (result != WAIT_OBJECT_0 && result != WAIT_TIMEOUT) {
77 ReportInvalidWaitableEventResult(result, ::GetLastError());
78 }
79 return result == WAIT_OBJECT_0;
80 }
81
TimedWaitImpl(TimeDelta wait_delta)82 bool WaitableEvent::TimedWaitImpl(TimeDelta wait_delta) {
83 // TimeTicks takes care of overflow but we special case is_max() nonetheless
84 // to avoid invoking TimeTicksNowIgnoringOverride() unnecessarily.
85 // WaitForSingleObject(handle_.Get(), INFINITE) doesn't spuriously wakeup so
86 // we don't need to worry about is_max() for the increment phase of the loop.
87 const TimeTicks end_time =
88 wait_delta.is_max() ? TimeTicks::Max()
89 : subtle::TimeTicksNowIgnoringOverride() + wait_delta;
90 for (TimeDelta remaining = wait_delta; remaining.is_positive();
91 remaining = end_time - subtle::TimeTicksNowIgnoringOverride()) {
92 // Truncate the timeout to milliseconds, rounded up to avoid spinning
93 // (either by returning too early or because a < 1ms timeout on Windows
94 // tends to return immediately).
95 const DWORD timeout_ms =
96 remaining.is_max()
97 ? INFINITE
98 : saturated_cast<DWORD>(remaining.InMillisecondsRoundedUp());
99 const DWORD result = WaitForSingleObject(handle_.get(), timeout_ms);
100 if (result == WAIT_OBJECT_0) {
101 // The object is signaled.
102 return true;
103 }
104
105 if (result == WAIT_TIMEOUT) {
106 // TimedWait can time out earlier than the specified |timeout| on
107 // Windows. To make this consistent with the posix implementation we
108 // should guarantee that TimedWait doesn't return earlier than the
109 // specified |max_time| and wait again for the remaining time.
110 continue;
111 }
112
113 // Failures are likely due to ERROR_INVALID_HANDLE. This unrecoverable
114 // error likely means that the waited-on object has been closed elsewhere,
115 // possibly due to a double-close on an unrelated HANDLE. Crash
116 // immediately since it is not possible to reason about the state of the
117 // process in this case.
118 if (result == WAIT_FAILED) {
119 debug::ScopedCrashKeyString last_error_key =
120 SetLastErrorCrashKey(::GetLastError());
121 NOTREACHED();
122 }
123
124 if (wait_delta.is_max()) {
125 // The only other documented result value is `WAIT_ABANDONED`. This nor
126 // any other result should ever be emitted.
127 ReportInvalidWaitableEventResult(result, ::GetLastError());
128 }
129 }
130 return false;
131 }
132
133 // static
WaitManyImpl(WaitableEvent ** events,size_t count)134 size_t WaitableEvent::WaitManyImpl(WaitableEvent** events, size_t count) {
135 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
136 CHECK_LE(count, static_cast<size_t>(MAXIMUM_WAIT_OBJECTS))
137 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany";
138
139 for (size_t i = 0; i < count; ++i)
140 handles[i] = events[i]->handle();
141
142 // The cast is safe because count is small - see the CHECK above.
143 DWORD result =
144 WaitForMultipleObjects(static_cast<DWORD>(count),
145 handles,
146 FALSE, // don't wait for all the objects
147 INFINITE); // no timeout
148 if (result >= WAIT_OBJECT_0 + count) {
149 DPLOG(FATAL) << "WaitForMultipleObjects failed";
150 return 0;
151 }
152
153 return result - WAIT_OBJECT_0;
154 }
155
156 } // namespace base
157