rust/library/std/src/sys
Matthias Krüger dcd2dd9bba
Rollup merge of #146503 - joboet:macos-condvar-timeout, r=ibraheemdev
std: improve handling of timed condition variable waits on macOS

Fixes rust-lang/rust#37440 (for good).

This fixes two issues with `Condvar::wait_timeout` on macOS:

Apple's implementation of `pthread_cond_timedwait` internally converts the absolute timeout to a relative one, measured in nanoseconds, but fails to consider overflow when doing so. This results in `wait_timeout` returning much earlier than anticipated when passed a duration that is slightly longer than `u64::MAX` nanoseconds (around 584 years). The existing clamping introduced by rust-lang/rust#42604 to address rust-lang/rust#37440 unfortunately used a maximum duration of 1000 years and thus still runs into the bug when run on older macOS versions (or with `PTHREAD_MUTEX_USE_ULOCK` set to a value other than "1"). See https://github.com/rust-lang/rust/issues/37440#issuecomment-3285958326 for context.

Reducing the maximum duration alone however would not be enough to make the implementation completely correct. As macOS does not support `pthread_condattr_setclock`, the deadline passed to `pthread_cond_timedwait` is measured against the wall-time clock. `std` currently calculates the deadline by retrieving the current time and adding the duration to that, only for macOS to convert the deadline back to a relative duration by [retrieving the current time itself](1ebf56b3a7/src/pthread_cond.c (L802-L819)) (this conversion is performed before the aforementioned problematic one). Thus, if the wall-time clock is adjusted between the `std` lookup and the system lookup, the relative duration could have changed, possibly even to a value larger than $2^{64}\ \textrm{ns}$. Luckily however, macOS supports the non-standard, tongue-twisting `pthread_cond_timedwait_relative_np` function which avoids the wall-clock-time roundtrip by taking a relative timeout. Even apart from that, this function is perfectly suited for `std`'s purposes: it is public (albeit badly-documented) API, [available since macOS 10.4](1ebf56b3a7/include/pthread/pthread.h (L555-L559)) (that's way below our minimum of 10.12) and completely resilient against wall-time changes as all timeouts are [measured against the monotonic clock](e3723e1f17/bsd/kern/sys_ulock.c (L741)) inside the kernel.

Thus, this PR switches `Condvar::wait_timeout` to `pthread_cond_timedwait_relative_np`, making sure to clamp the duration to a maximum of $2^{64} - 1 \ \textrm{ns}$. I've added a miri shim as well, so the only thing missing is a definition of `pthread_cond_timedwait_relative_np` inside `libc`.
2025-10-14 19:47:28 +02:00
..
alloc std: add support for armv7a-vex-v5 target 2025-09-24 12:10:15 -05:00
anonymous_pipe library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
args Add a new wasm32-wasip3 target to Rust 2025-10-02 15:09:09 -07:00
env library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
fd Rollup merge of #140459 - niklasf:feature/read-buf-at, r=tgross35 2025-09-04 10:01:51 +10:00
fs std: add support for armv7a-vex-v5 target 2025-09-24 12:10:15 -05:00
io library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
net Rollup merge of #147205 - alexcrichton:wasip3, r=davidtwco 2025-10-07 19:39:07 +02:00
os_str Move WTF-8 code from std to core/alloc 2025-08-20 20:31:33 -04:00
pal Rollup merge of #146503 - joboet:macos-condvar-timeout, r=ibraheemdev 2025-10-14 19:47:28 +02:00
path library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
personality library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
platform_version Fix compare_against_sw_vers test when a version part is 0 2025-09-09 19:43:50 +02:00
process std: merge definitions of StdioPipes 2025-09-21 19:45:46 +02:00
random Add a new wasm32-wasip3 target to Rust 2025-10-02 15:09:09 -07:00
stdio Add a new wasm32-wasip3 target to Rust 2025-10-02 15:09:09 -07:00
sync thread parking: fix docs and examples 2025-09-03 09:14:25 +02:00
thread Add a new wasm32-wasip3 target to Rust 2025-10-02 15:09:09 -07:00
thread_local Mitigate thread_local! shadowing issues 2025-10-03 00:00:39 -04:00
backtrace.rs fix(std): Add __my_thread_exit stub for QNX 8 2025-08-25 10:34:40 +02:00
cmath.rs library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
configure_builtins.rs Dynamically enable LSE for aarch64 rust provided intrinsics 2025-08-05 10:35:13 -05:00
env_consts.rs std: add support for armv7a-vex-v5 target 2025-09-24 12:10:15 -05:00
exit_guard.rs library: Migrate from cfg_if to cfg_select 2025-08-16 05:28:31 -07:00
mod.rs std: move thread into sys 2025-09-10 15:26:17 +02:00