Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd09c2f

Browse files
committed
[libc++] Fix semaphore timed wait hanging on Windows (llvm#180398)
Fixes llvm#180334 The semaphore timed wait test is flaky on Windows. It hangs from time to time. Some examples: [windows (clang-cl-no-vcruntime, false, clang-cl, clang-cl)](https://github.com/llvm/llvm-project/actions/runs/21737380876/job/62707542836#logs) [windows (mingw-static, true, cc, c++)](https://github.com/llvm/llvm-project/actions/runs/21636063482/job/62367831823?pr=179483#logs) [windows (clang-cl-static, false, clang-cl, clang-cl)](https://github.com/llvm/llvm-project/actions/runs/21453876753/job/61794464147#logs) [windows (clang-cl-dll, false, clang-cl, clang-cl)](https://github.com/llvm/llvm-project/actions/runs/21382902941/job/61556154029#logs) [windows (mingw-static, true, cc, c++)](https://github.com/llvm/llvm-project/actions/runs/21365713577/job/61502377123#logs) The internal dylib function takes a timeout ```cpp static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) ``` We followed the same convention as `__libcpp_thread_poll_with_backoff`, where we used `0ns` to indicate wait indefinitely until being notified. ```cpp _LIBCPP_HIDE_FROM_ABI __poll_with_backoff_results __libcpp_thread_poll_with_backoff( _Poll&& __poll, _Backoff&& __backoff, chrono::nanoseconds __max_elapsed = chrono::nanoseconds::zero()) ``` This is problematic, if the caller indeed wants to wait `0ns` and passes `0`, the internal dylib function `__platform_wait_on_address` would wait indefinitely ```cpp __timeout_ns == 0 ? INFINITE : static_cast<DWORD>(__timeout_ns / 1'000'000) ``` This is what actually happened here. So the fix is to update internal dylib function to use `optional` and use `nullopt` to indicate wait indefinitely ```cpp static void __platform_wait_on_address(void const* __ptr, void const* __val, optional<uint64_t> __timeout_ns) ``` Edit: after code review, the code is updated to use tag type `NoTimeout` to indicate "wait indefinitely". this is superior because it is coded into the type system instead of runtime check problem? `__libcpp_thread_poll_with_backoff` has this " `0ns` means wait indefinitely " semantic for years (it has always been like that), but it never causes issues. This is because, it has ```cpp chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start; if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed) return __poll_with_backoff_results::__timeout; ``` `__max_elapsed` is what user passed in, let's assume the user passed in `0ns`, and `__elapsed` is certainly a positive number so it never goes to the backoff function and directly returned. No hanging possible So in the test, we passed wait some time, say `1ms`, from the `semaphore` public API, which calls `__libcpp_thread_poll_with_backoff` with `1ms` timeout. `__libcpp_thread_poll_with_backoff` will do some polling loops and then calling into the backoff function, platform timed wait in this case, with timeout of `1ms - elapsed` , say `950us`. However, Windows platform wait has millisecond precision, so `950us` is rounded down to `0ms` (`static_cast<DWORD>(__timeout_ns / 1'000'000)`), so the function call almost immediately returns, and `__libcpp_thread_poll_with_backoff` will keep its polling loop like this. As time goes by in the polling loop, the timeout for platform wait will decrease from `950us` to smaller and smaller number. In the `__libcpp_thread_poll_with_backoff` ```cpp if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed) return __poll_with_backoff_results::__timeout; if (auto __backoff_res = __backoff(__elapsed); __backoff_res == __backoff_results::__continue_poll) ``` `__max_elapsed` is user requested timeout, which is `1ms` in this case, and `__elapsed` gradually increases and eventually, if it becomes greater than `1ms`, we have `__max_elapsed < __elapsed`, it will return and test passes. all Good. But there is a slim chance that on one loop, `__elapsed` is exactly the same number of the user requested `__max_elapsed` `1ms`, so `__max_elapsed == __elapsed`, this will make the code path go to backoff platform wait, with the timeout `__max_elapsed - __elapsed == 0ns`. So now we are requesting `__platform_wait_on_address` to wait exactly `0ns`, and due to our ambiguous API, `0ns` means wait indefinitely ```cpp __timeout_ns == 0 ? INFINITE : static_cast<DWORD>(__timeout_ns / 1'000'000) ``` The test will just hang forever. in `__libcpp_thread_poll_with_backoff`, If we check `__max_elapsed <= __elapsed` instead of `__max_elapsed < __elapsed`, we would avoid the call to platform wait with `0ns`. But according to the standard, I think the current `__max_elapsed < __elapsed` is more correct > The timeout expires ([[thread.req.timing]](https://eel.is/c++draft/thread.req.timing)) when the current time is after abs_time (for try_acquire_until) or when at least rel_time has passed from the start of the function (for try_acquire_for)[.](https://eel.is/c++draft/thread.sema#cnt-18.sentence-2) https://eel.is/c++draft/thread.sema#cnt-18.2 So this **after** means that it needs strictly greater than i think. The fix is to update the internal dylib API to use `nullopt` to indicate wait indefinitely. So a call to platform wait with `0ns` will not cause a hang. Edit: after code review, the code is updated to use tag type `NoTimeout` to indicate "wait indefinitely". this is superior because it is coded into the type system instead of runtime check I made a small change as well. it is very easy to get into a situation where the requested platform wait timeout is `<1ms`, we will be keep calling Windows platform wait with `0ns` because of the rounding, and this is effectively a spin lock . I made a change such that if the requested timeout is between `100us to 1ms`, just rounded up to `1ms` to wait a bit longer (which is conforming IIUC) . let me know if this change is necessary, happy to take this part out if it is not considered good.
1 parent 2bb9885 commit cd09c2f

2 files changed

Lines changed: 105 additions & 30 deletions

File tree

libcxx/src/atomic.cpp

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,27 @@
5959

6060
#endif
6161

62+
_LIBCPP_PUSH_MACROS
63+
#include <__undef_macros>
64+
6265
_LIBCPP_BEGIN_NAMESPACE_STD
6366

67+
struct NoTimeout {};
68+
6469
#ifdef __linux__
6570

66-
template <std::size_t _Size>
67-
static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
71+
template <std::size_t _Size, class MaybeTimeout>
72+
static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_timeout_ns) {
6873
static_assert(_Size == 4, "Can only wait on 4 bytes value");
6974
alignas(__cxx_contention_t) char buffer[_Size];
7075
std::memcpy(&buffer, const_cast<const void*>(__val), _Size);
7176
static constexpr timespec __default_timeout = {2, 0};
7277
timespec __timeout;
73-
if (__timeout_ns == 0) {
78+
if constexpr (is_same_v<MaybeTimeout, NoTimeout>) {
7479
__timeout = __default_timeout;
7580
} else {
76-
__timeout.tv_sec = __timeout_ns / 1'000'000'000;
77-
__timeout.tv_nsec = __timeout_ns % 1'000'000'000;
81+
__timeout.tv_sec = maybe_timeout_ns / 1'000'000'000;
82+
__timeout.tv_nsec = maybe_timeout_ns % 1'000'000'000;
7883
}
7984
_LIBCPP_FUTEX(__ptr, FUTEX_WAIT_PRIVATE, *reinterpret_cast<__cxx_contention_t const*>(&buffer), &__timeout, 0, 0);
8085
}
@@ -96,10 +101,16 @@ extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value)
96101
# define UL_COMPARE_AND_WAIT64 5
97102
# define ULF_WAKE_ALL 0x00000100
98103

99-
template <std::size_t _Size>
100-
static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
104+
template <std::size_t _Size, class MaybeTimeout>
105+
static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_timeout_ns) {
101106
static_assert(_Size == 8 || _Size == 4, "Can only wait on 8 bytes or 4 bytes value");
102-
auto __timeout_us = __timeout_ns == 0 ? 0 : static_cast<uint32_t>(__timeout_ns / 1000);
107+
auto __timeout_us = [&] {
108+
if constexpr (is_same_v<MaybeTimeout, NoTimeout>) {
109+
return uint32_t(0);
110+
} else {
111+
return std::max(static_cast<uint32_t>(maybe_timeout_ns / 1000), uint32_t(1));
112+
}
113+
}();
103114
if constexpr (_Size == 4) {
104115
alignas(uint32_t) char buffer[_Size];
105116
std::memcpy(&buffer, const_cast<const void*>(__val), _Size);
@@ -130,17 +141,17 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
130141
* limit its use to architectures where long and int64_t are synonyms.
131142
*/
132143

133-
template <std::size_t _Size>
134-
static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
144+
template <std::size_t _Size, class MaybeTimeout>
145+
static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_timeout_ns) {
135146
static_assert(_Size == 8, "Can only wait on 8 bytes value");
136147
alignas(__cxx_contention_t) char buffer[_Size];
137148
std::memcpy(&buffer, const_cast<const void*>(__val), _Size);
138-
if (__timeout_ns == 0) {
149+
if constexpr (is_same_v<MaybeTimeout, NoTimeout>) {
139150
_umtx_op(const_cast<void*>(__ptr), UMTX_OP_WAIT, *reinterpret_cast<__cxx_contention_t*>(&buffer), nullptr, nullptr);
140151
} else {
141152
_umtx_time ut;
142-
ut._timeout.tv_sec = __timeout_ns / 1'000'000'000;
143-
ut._timeout.tv_nsec = __timeout_ns % 1'000'000'000;
153+
ut._timeout.tv_sec = maybe_timeout_ns / 1'000'000'000;
154+
ut._timeout.tv_nsec = maybe_timeout_ns % 1'000'000'000;
144155
ut._flags = 0; // Relative time (not absolute)
145156
ut._clockid = CLOCK_MONOTONIC; // Use monotonic clock
146157

@@ -184,22 +195,35 @@ static void* win32_get_synch_api_function(const char* function_name) {
184195
return reinterpret_cast<void*>(GetProcAddress(module_handle, function_name));
185196
}
186197

187-
template <std::size_t _Size>
188-
static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
198+
template <std::size_t _Size, class MaybeTimeout>
199+
static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_timeout_ns) {
189200
static_assert(_Size == 8, "Can only wait on 8 bytes value");
190201
// WaitOnAddress was added in Windows 8 (build 9200)
191202
static auto wait_on_address =
192203
reinterpret_cast<BOOL(WINAPI*)(void*, PVOID, SIZE_T, DWORD)>(win32_get_synch_api_function("WaitOnAddress"));
193204
if (wait_on_address != nullptr) {
194-
wait_on_address(const_cast<void*>(__ptr),
195-
const_cast<void*>(__val),
196-
_Size,
197-
__timeout_ns == 0 ? INFINITE : static_cast<DWORD>(__timeout_ns / 1'000'000));
205+
auto timeout_ms = [&]() -> DWORD {
206+
if constexpr (is_same_v<MaybeTimeout, NoTimeout>) {
207+
return INFINITE;
208+
} else {
209+
uint64_t ms = maybe_timeout_ns / 1'000'000;
210+
if (ms == 0 && maybe_timeout_ns > 100'000)
211+
// Round up to 1ms if requested between 100us - 1ms
212+
return 1;
213+
214+
return static_cast<DWORD>(std::min(static_cast<uint64_t>(INFINITE), ms));
215+
}
216+
}();
217+
wait_on_address(const_cast<void*>(__ptr), const_cast<void*>(__val), _Size, timeout_ms);
198218
} else {
219+
std::chrono::nanoseconds timeout = std::chrono::nanoseconds(0);
220+
if constexpr (!is_same_v<MaybeTimeout, NoTimeout>) {
221+
timeout = std::chrono::nanoseconds(maybe_timeout_ns);
222+
}
199223
__libcpp_thread_poll_with_backoff(
200224
[=]() -> bool { return std::memcmp(const_cast<const void*>(__ptr), __val, _Size) != 0; },
201225
__libcpp_timed_backoff_policy(),
202-
std::chrono::nanoseconds(__timeout_ns));
226+
timeout);
203227
}
204228
}
205229

@@ -233,12 +257,16 @@ static void __platform_wake_by_address(void const* __ptr, bool __notify_one) {
233257

234258
// Baseline is just a timed backoff
235259

236-
template <std::size_t _Size>
237-
static void __platform_wait_on_address(void const* __ptr, void const* __val, uint64_t __timeout_ns) {
260+
template <std::size_t _Size, class MaybeTimeout>
261+
static void __platform_wait_on_address(void const* __ptr, void const* __val, MaybeTimeout maybe_timeout_ns) {
262+
std::chrono::nanoseconds timeout = std::chrono::nanoseconds(0);
263+
if constexpr (!is_same_v<MaybeTimeout, NoTimeout>) {
264+
timeout = std::chrono::nanoseconds(maybe_timeout_ns);
265+
}
238266
__libcpp_thread_poll_with_backoff(
239267
[=]() -> bool { return std::memcmp(const_cast<const void*>(__ptr), __val, _Size) != 0; },
240268
__libcpp_timed_backoff_policy(),
241-
std::chrono::nanoseconds(__timeout_ns));
269+
timeout);
242270
}
243271

244272
template <std::size_t _Size>
@@ -261,17 +289,17 @@ __contention_notify(__cxx_atomic_contention_t* __waiter_count, void const* __add
261289
__platform_wake_by_address<_Size>(__address_to_notify, __notify_one);
262290
}
263291

264-
template <std::size_t _Size>
292+
template <std::size_t _Size, class MaybeTimeout>
265293
static void __contention_wait(__cxx_atomic_contention_t* __waiter_count,
266294
void const* __address_to_wait,
267295
void const* __old_value,
268-
uint64_t __timeout_ns) {
296+
MaybeTimeout maybe_timeout_ns) {
269297
__cxx_atomic_fetch_add(__waiter_count, __cxx_contention_t(1), memory_order_relaxed);
270298
// https://llvm.org/PR109290
271299
// There are no platform guarantees of a memory barrier in the platform wait implementation
272300
__cxx_atomic_thread_fence(memory_order_seq_cst);
273301
// We sleep as long as the monitored value hasn't changed.
274-
__platform_wait_on_address<_Size>(__address_to_wait, __old_value, __timeout_ns);
302+
__platform_wait_on_address<_Size>(__address_to_wait, __old_value, maybe_timeout_ns);
275303
__cxx_atomic_fetch_sub(__waiter_count, __cxx_contention_t(1), memory_order_release);
276304
}
277305

@@ -334,7 +362,7 @@ _LIBCPP_EXPORTED_FROM_ABI void
334362
__atomic_wait_global_table(void const* __location, __cxx_contention_t __old_value) noexcept {
335363
auto const __entry = __get_global_contention_state(__location);
336364
__contention_wait<sizeof(__cxx_atomic_contention_t)>(
337-
&__entry->__waiter_count, &__entry->__platform_state, &__old_value, 0);
365+
&__entry->__waiter_count, &__entry->__platform_state, &__old_value, NoTimeout{});
338366
}
339367

340368
_LIBCPP_EXPORTED_FROM_ABI void __atomic_wait_global_table_with_timeout(
@@ -356,7 +384,7 @@ _LIBCPP_EXPORTED_FROM_ABI void __atomic_notify_all_global_table(void const* __lo
356384

357385
template <std::size_t _Size>
358386
_LIBCPP_EXPORTED_FROM_ABI void __atomic_wait_native(void const* __address, void const* __old_value) noexcept {
359-
__contention_wait<_Size>(__get_native_waiter_count(__address), __address, __old_value, 0);
387+
__contention_wait<_Size>(__get_native_waiter_count(__address), __address, __old_value, NoTimeout{});
360388
}
361389

362390
template <std::size_t _Size>
@@ -431,7 +459,7 @@ _LIBCPP_EXPORTED_FROM_ABI void
431459
__libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value) noexcept {
432460
auto const __entry = __get_global_contention_state(const_cast<void const*>(__location));
433461
__contention_wait<sizeof(__cxx_atomic_contention_t)>(
434-
&__entry->__waiter_count, &__entry->__platform_state, &__old_value, 0);
462+
&__entry->__waiter_count, &__entry->__platform_state, &__old_value, NoTimeout{});
435463
}
436464

437465
_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location) noexcept {
@@ -450,7 +478,7 @@ _LIBCPP_EXPORTED_FROM_ABI void
450478
__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value) noexcept {
451479
auto __location_cast = const_cast<const void*>(static_cast<const volatile void*>(__location));
452480
__contention_wait<sizeof(__cxx_atomic_contention_t)>(
453-
__get_native_waiter_count(__location_cast), __location_cast, &__old_value, 0);
481+
__get_native_waiter_count(__location_cast), __location_cast, &__old_value, NoTimeout{});
454482
}
455483

456484
// this function is even unused in the old ABI
@@ -462,3 +490,5 @@ __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location) no
462490
_LIBCPP_DIAGNOSTIC_POP
463491

464492
_LIBCPP_END_NAMESPACE_STD
493+
494+
_LIBCPP_POP_MACROS
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// UNSUPPORTED: no-threads
10+
// UNSUPPORTED: c++03, c++11, c++14, c++17
11+
12+
// <semaphore>
13+
14+
// This is a regression test for a bug in semaphore::try_acquire_for
15+
// where it can wait indefinitely
16+
// https://github.com/llvm/llvm-project/issues/180334
17+
18+
#include <semaphore>
19+
#include <thread>
20+
#include <chrono>
21+
#include <cassert>
22+
23+
#include "make_test_thread.h"
24+
#include "test_macros.h"
25+
26+
void test() {
27+
auto const start = std::chrono::steady_clock::now();
28+
std::counting_semaphore<> s(0);
29+
30+
assert(!s.try_acquire_for(std::chrono::nanoseconds(1)));
31+
assert(!s.try_acquire_for(std::chrono::microseconds(1)));
32+
assert(!s.try_acquire_for(std::chrono::milliseconds(1)));
33+
assert(!s.try_acquire_for(std::chrono::milliseconds(100)));
34+
35+
auto const end = std::chrono::steady_clock::now();
36+
assert(end - start < std::chrono::seconds(10));
37+
}
38+
39+
int main(int, char**) {
40+
for (auto i = 0; i < 10; ++i) {
41+
test();
42+
}
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)