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 #include "base/synchronization/waitable_event.h"
6
7 #include <windows.h>
8
9 #include <stddef.h>
10
11 #include <algorithm>
12 #include <optional>
13 #include <utility>
14
15 #include "base/compiler_specific.h"
16 #include "base/debug/alias.h"
17 #include "base/debug/dump_without_crashing.h"
18 #include "base/logging.h"
19 #include "base/numerics/safe_conversions.h"
20 #include "base/threading/scoped_blocking_call.h"
21 #include "base/threading/thread_restrictions.h"
22 #include "base/time/time.h"
23 #include "base/time/time_override.h"
24
25 namespace base {
26
27 namespace {
ReportInvalidWaitableEventResult(DWORD result)28 NOINLINE void ReportInvalidWaitableEventResult(DWORD result) {
29 const auto last_error = ::GetLastError();
30 base::debug::Alias(&last_error);
31 base::debug::Alias(&result);
32 base::debug::DumpWithoutCrashing(); // https://crbug.com/1478972.
33 }
34 } // namespace
35
WaitableEvent(ResetPolicy reset_policy,InitialState initial_state)36 WaitableEvent::WaitableEvent(ResetPolicy reset_policy,
37 InitialState initial_state)
38 : handle_(CreateEvent(nullptr,
39 reset_policy == ResetPolicy::MANUAL,
40 initial_state == InitialState::SIGNALED,
41 nullptr)) {
42 // We're probably going to crash anyways if this is ever NULL, so we might as
43 // well make our stack reports more informative by crashing here.
44 CHECK(handle_.is_valid());
45 }
46
WaitableEvent(win::ScopedHandle handle)47 WaitableEvent::WaitableEvent(win::ScopedHandle handle)
48 : handle_(std::move(handle)) {
49 CHECK(handle_.is_valid()) << "Tried to create WaitableEvent from NULL handle";
50 }
51
Reset()52 void WaitableEvent::Reset() {
53 ResetEvent(handle_.get());
54 }
55
SignalImpl()56 void WaitableEvent::SignalImpl() {
57 SetEvent(handle_.get());
58 }
59
IsSignaled()60 bool WaitableEvent::IsSignaled() {
61 DWORD result = WaitForSingleObject(handle_.get(), 0);
62 if (result != WAIT_OBJECT_0 && result != WAIT_TIMEOUT) {
63 ReportInvalidWaitableEventResult(result);
64 }
65 return result == WAIT_OBJECT_0;
66 }
67
TimedWaitImpl(TimeDelta wait_delta)68 bool WaitableEvent::TimedWaitImpl(TimeDelta wait_delta) {
69 // TimeTicks takes care of overflow but we special case is_max() nonetheless
70 // to avoid invoking TimeTicksNowIgnoringOverride() unnecessarily.
71 // WaitForSingleObject(handle_.Get(), INFINITE) doesn't spuriously wakeup so
72 // we don't need to worry about is_max() for the increment phase of the loop.
73 const TimeTicks end_time =
74 wait_delta.is_max() ? TimeTicks::Max()
75 : subtle::TimeTicksNowIgnoringOverride() + wait_delta;
76 for (TimeDelta remaining = wait_delta; remaining.is_positive();
77 remaining = end_time - subtle::TimeTicksNowIgnoringOverride()) {
78 // Truncate the timeout to milliseconds, rounded up to avoid spinning
79 // (either by returning too early or because a < 1ms timeout on Windows
80 // tends to return immediately).
81 const DWORD timeout_ms =
82 remaining.is_max()
83 ? INFINITE
84 : saturated_cast<DWORD>(remaining.InMillisecondsRoundedUp());
85 const DWORD result = WaitForSingleObject(handle_.get(), timeout_ms);
86 if (result == WAIT_OBJECT_0) {
87 // The object is signaled.
88 return true;
89 }
90
91 if (result == WAIT_TIMEOUT) {
92 // TimedWait can time out earlier than the specified |timeout| on
93 // Windows. To make this consistent with the posix implementation we
94 // should guarantee that TimedWait doesn't return earlier than the
95 // specified |max_time| and wait again for the remaining time.
96 continue;
97 }
98
99 // The only other documented result values are `WAIT_ABANDONED` and
100 // `WAIT_FAILED`. Neither of these nor any other result should ever be
101 // emitted unless there is a double free or another entity is tampering
102 // with this instance's event handle. Only fails if the timeout was
103 // INFINITE.
104 if (wait_delta.is_max()) {
105 ReportInvalidWaitableEventResult(result);
106 // The code may infinite loop and then hang if the returned value
107 // continues being `WAIT_FAILED`.
108 }
109 }
110 return false;
111 }
112
113 // static
WaitManyImpl(WaitableEvent ** events,size_t count)114 size_t WaitableEvent::WaitManyImpl(WaitableEvent** events, size_t count) {
115 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
116 CHECK_LE(count, static_cast<size_t>(MAXIMUM_WAIT_OBJECTS))
117 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany";
118
119 for (size_t i = 0; i < count; ++i)
120 handles[i] = events[i]->handle();
121
122 // The cast is safe because count is small - see the CHECK above.
123 DWORD result =
124 WaitForMultipleObjects(static_cast<DWORD>(count),
125 handles,
126 FALSE, // don't wait for all the objects
127 INFINITE); // no timeout
128 if (result >= WAIT_OBJECT_0 + count) {
129 DPLOG(FATAL) << "WaitForMultipleObjects failed";
130 return 0;
131 }
132
133 return result - WAIT_OBJECT_0;
134 }
135
136 } // namespace base
137