blob: 49b49e0978cf4fb5d61fc1d3124adb8e3d140d62 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]b2e97292008-09-02 18:20:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Thomas Sepez852f1562024-08-19 18:40:415#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
[email protected]44f9c952011-01-02 06:05:3910#include "base/synchronization/waitable_event.h"
[email protected]b2e97292008-09-02 18:20:3411
[email protected]b2e97292008-09-02 18:20:3412#include <windows.h>
Bruce Dawsona1e1cfcb2022-11-22 20:04:3513
Takuto Ikutac8d6b16f2024-04-15 16:59:1914#include <stddef.h>
15
staniscacf68012016-11-30 19:56:0916#include <algorithm>
Arthur Sonzognie5fff99c2024-02-21 15:58:2417#include <optional>
dcheng70c49422016-03-02 23:20:3418#include <utility>
19
Etienne Bergerond2c6ab92024-01-25 21:16:3420#include "base/compiler_specific.h"
Chris Davis5ed388c2024-10-22 21:43:3721#include "base/debug/crash_logging.h"
Etienne Bergerond2c6ab92024-01-25 21:16:3422#include "base/debug/dump_without_crashing.h"
[email protected]b2e97292008-09-02 18:20:3423#include "base/logging.h"
Peter Boströme1098ef2024-10-28 09:04:2024#include "base/notreached.h"
rvargasb2e1b3c2015-03-31 03:17:0425#include "base/numerics/safe_conversions.h"
Francois Doray3abc0c02017-09-11 19:53:2926#include "base/threading/scoped_blocking_call.h"
[email protected]3a7b66d2012-04-26 16:34:1627#include "base/threading/thread_restrictions.h"
[email protected]8f9a3a52013-06-28 15:14:1828#include "base/time/time.h"
Gabriel Charetteef6cbc22019-08-02 06:24:5929#include "base/time/time_override.h"
[email protected]b2e97292008-09-02 18:20:3430
31namespace base {
32
Etienne Bergerond2c6ab92024-01-25 21:16:3433namespace {
Peter Boströme1098ef2024-10-28 09:04:2034
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
42NOINLINE void ReportInvalidWaitableEventResult(DWORD result, DWORD last_error) {
43 SCOPED_CRASH_KEY_NUMBER("WaitableEvent", "result", result);
44 debug::ScopedCrashKeyString last_error_key = SetLastErrorCrashKey(last_error);
Etienne Bergerond2c6ab92024-01-25 21:16:3445 base::debug::DumpWithoutCrashing(); // https://crbug.com/1478972.
46}
Peter Boströme1098ef2024-10-28 09:04:2047
Etienne Bergerond2c6ab92024-01-25 21:16:3448} // namespace
49
gabe5ff4d62016-06-01 00:16:5050WaitableEvent::WaitableEvent(ResetPolicy reset_policy,
51 InitialState initial_state)
gabd9aa1d12016-06-09 18:41:2252 : handle_(CreateEvent(nullptr,
53 reset_policy == ResetPolicy::MANUAL,
54 initial_state == InitialState::SIGNALED,
55 nullptr)) {
[email protected]b2e97292008-09-02 18:20:3456 // 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.
Lei Zhang04c0bcb2022-02-04 04:13:4558 CHECK(handle_.is_valid());
[email protected]1c4947f2009-01-15 22:25:1159}
60
rvargas94ed6c4e2014-11-25 01:23:3161WaitableEvent::WaitableEvent(win::ScopedHandle handle)
dcheng70c49422016-03-02 23:20:3462 : handle_(std::move(handle)) {
Lei Zhang04c0bcb2022-02-04 04:13:4563 CHECK(handle_.is_valid()) << "Tried to create WaitableEvent from NULL handle";
[email protected]b2e97292008-09-02 18:20:3464}
65
[email protected]b2e97292008-09-02 18:20:3466void WaitableEvent::Reset() {
Lei Zhang04c0bcb2022-02-04 04:13:4567 ResetEvent(handle_.get());
[email protected]b2e97292008-09-02 18:20:3468}
69
Gabriel Charetteecba93c2022-10-31 14:30:5570void WaitableEvent::SignalImpl() {
Lei Zhang04c0bcb2022-02-04 04:13:4571 SetEvent(handle_.get());
[email protected]b2e97292008-09-02 18:20:3472}
73
Bartek Nowierskifa70c222024-07-01 04:02:0874bool WaitableEvent::IsSignaled() const {
Lei Zhang04c0bcb2022-02-04 04:13:4575 DWORD result = WaitForSingleObject(handle_.get(), 0);
Etienne Bergerond2c6ab92024-01-25 21:16:3476 if (result != WAIT_OBJECT_0 && result != WAIT_TIMEOUT) {
Peter Boströme1098ef2024-10-28 09:04:2077 ReportInvalidWaitableEventResult(result, ::GetLastError());
Etienne Bergerond2c6ab92024-01-25 21:16:3478 }
staniscacf68012016-11-30 19:56:0979 return result == WAIT_OBJECT_0;
[email protected]b2e97292008-09-02 18:20:3480}
81
Gabriel Charetteecba93c2022-10-31 14:30:5582bool WaitableEvent::TimedWaitImpl(TimeDelta wait_delta) {
Gabriel Charettec2b3ac892019-07-30 12:13:4783 // TimeTicks takes care of overflow but we special case is_max() nonetheless
Gabriel Charetteef6cbc22019-08-02 06:24:5984 // to avoid invoking TimeTicksNowIgnoringOverride() unnecessarily.
Gabriel Charettec2b3ac892019-07-30 12:13:4785 // 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 =
Gabriel Charetteef6cbc22019-08-02 06:24:5988 wait_delta.is_max() ? TimeTicks::Max()
89 : subtle::TimeTicksNowIgnoringOverride() + wait_delta;
Xiaohan Wangabff0302021-10-27 00:42:5790 for (TimeDelta remaining = wait_delta; remaining.is_positive();
Gabriel Charetteef6cbc22019-08-02 06:24:5991 remaining = end_time - subtle::TimeTicksNowIgnoringOverride()) {
Gabriel Charettec2b3ac892019-07-30 12:13:4792 // 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());
Lei Zhang04c0bcb2022-02-04 04:13:4599 const DWORD result = WaitForSingleObject(handle_.get(), timeout_ms);
Etienne Bergerond2c6ab92024-01-25 21:16:34100 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
Chris Davis2bc79642024-10-24 16:01:29113 // 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) {
Peter Boströme1098ef2024-10-28 09:04:20119 debug::ScopedCrashKeyString last_error_key =
120 SetLastErrorCrashKey(::GetLastError());
121 NOTREACHED();
Chris Davis2bc79642024-10-24 16:01:29122 }
Chris Davis5ed388c2024-10-22 21:43:37123
Chris Davis2bc79642024-10-24 16:01:29124 if (wait_delta.is_max()) {
Chris Davis5ed388c2024-10-22 21:43:37125 // The only other documented result value is `WAIT_ABANDONED`. This nor
126 // any other result should ever be emitted.
Peter Boströme1098ef2024-10-28 09:04:20127 ReportInvalidWaitableEventResult(result, ::GetLastError());
staniscacf68012016-11-30 19:56:09128 }
Gabriel Charettec2b3ac892019-07-30 12:13:47129 }
staniscacf68012016-11-30 19:56:09130 return false;
131}
132
[email protected]1c4947f2009-01-15 22:25:11133// static
Thomas Sepez852f1562024-08-19 18:40:41134size_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))
[email protected]1c4947f2009-01-15 22:25:11137 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany";
138
Peter Kasting134ef9af2024-12-28 02:30:09139 for (size_t i = 0; i < count; ++i) {
Thomas Sepez852f1562024-08-19 18:40:41140 handles[i] = events[i]->handle();
Peter Kasting134ef9af2024-12-28 02:30:09141 }
[email protected]1c4947f2009-01-15 22:25:11142
Thomas Sepez852f1562024-08-19 18:40:41143 // The cast is safe because count is small - see the CHECK above.
144 DWORD result =
Peter Kasting134ef9af2024-12-28 02:30:09145 WaitForMultipleObjects(static_cast<DWORD>(count), handles,
Thomas Sepez852f1562024-08-19 18:40:41146 FALSE, // don't wait for all the objects
147 INFINITE); // no timeout
148 if (result >= WAIT_OBJECT_0 + count) {
[email protected]ad8cfa92014-05-21 20:06:23149 DPLOG(FATAL) << "WaitForMultipleObjects failed";
[email protected]1c4947f2009-01-15 22:25:11150 return 0;
151 }
152
153 return result - WAIT_OBJECT_0;
154}
155
[email protected]b2e97292008-09-02 18:20:34156} // namespace base