-
Notifications
You must be signed in to change notification settings - Fork 13.9k
std: improve handling of timed condition variable waits on macOS #146503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -267,3 +267,35 @@ nonpoison_and_poison_unwrap_test!( | |
| } | ||
| } | ||
| ); | ||
|
|
||
| // Some platforms internally cast the timeout duration into nanoseconds. | ||
| // If they fail to consider overflow during the conversion (I'm looking | ||
| // at you, macOS), `wait_timeout` will return immediately and indicate a | ||
| // timeout for durations that are slightly longer than u64::MAX nanoseconds. | ||
| // `std` should guard against this by clamping the timeout. | ||
| // See #37440 for context. | ||
| nonpoison_and_poison_unwrap_test!( | ||
| name: timeout_nanoseconds, | ||
| test_body: { | ||
| use locks::Mutex; | ||
| use locks::Condvar; | ||
|
|
||
| let sent = Mutex::new(false); | ||
| let cond = Condvar::new(); | ||
|
|
||
| thread::scope(|s| { | ||
| s.spawn(|| { | ||
| thread::sleep(Duration::from_secs(2)); | ||
| maybe_unwrap(sent.set(true)); | ||
| cond.notify_all(); | ||
| }); | ||
|
|
||
| let guard = maybe_unwrap(sent.lock()); | ||
| // If there is internal overflow, this call will return almost | ||
| // immediately, before the other thread has reached the `notify_all` | ||
| let (guard, res) = maybe_unwrap(cond.wait_timeout(guard, Duration::from_secs(u64::MAX.div_ceil(1_000_000_000)))); | ||
| assert!(!res.timed_out()); | ||
| assert!(*guard); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems to fail in CI from time to time: #147885 |
||
| }) | ||
| } | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -834,8 +834,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
| &mut self, | ||
| cond_op: &OpTy<'tcx>, | ||
| mutex_op: &OpTy<'tcx>, | ||
| abstime_op: &OpTy<'tcx>, | ||
| timeout_op: &OpTy<'tcx>, | ||
| dest: &MPlaceTy<'tcx>, | ||
| macos_relative_np: bool, | ||
| ) -> InterpResult<'tcx> { | ||
| let this = self.eval_context_mut(); | ||
|
|
||
|
|
@@ -844,7 +845,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
|
|
||
| // Extract the timeout. | ||
| let duration = match this | ||
| .read_timespec(&this.deref_pointer_as(abstime_op, this.libc_ty_layout("timespec"))?)? | ||
| .read_timespec(&this.deref_pointer_as(timeout_op, this.libc_ty_layout("timespec"))?)? | ||
| { | ||
| Some(duration) => duration, | ||
| None => { | ||
|
|
@@ -853,14 +854,23 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { | |
| return interp_ok(()); | ||
| } | ||
| }; | ||
| if data.clock == TimeoutClock::RealTime { | ||
| this.check_no_isolation("`pthread_cond_timedwait` with `CLOCK_REALTIME`")?; | ||
| } | ||
|
|
||
| let (clock, anchor) = if macos_relative_np { | ||
| // `pthread_cond_timedwait_relative_np` always measures time against the | ||
| // monotonic clock, regardless of the condvar clock. | ||
| (TimeoutClock::Monotonic, TimeoutAnchor::Relative) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the argument named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thank you! |
||
| } else { | ||
| if data.clock == TimeoutClock::RealTime { | ||
| this.check_no_isolation("`pthread_cond_timedwait` with `CLOCK_REALTIME`")?; | ||
| } | ||
|
|
||
| (data.clock, TimeoutAnchor::Absolute) | ||
| }; | ||
|
|
||
| this.condvar_wait( | ||
| data.condvar_ref, | ||
| mutex_ref, | ||
| Some((data.clock, TimeoutAnchor::Absolute, duration)), | ||
| Some((clock, anchor, duration)), | ||
| Scalar::from_i32(0), | ||
| this.eval_libc("ETIMEDOUT"), // retval_timeout | ||
| dest.clone(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //@only-target: apple # `pthread_cond_timedwait_relative_np` is a non-standard extension | ||
|
|
||
| use std::time::Instant; | ||
|
|
||
| // FIXME: remove once this is in libc. | ||
| mod libc { | ||
| pub use ::libc::*; | ||
| unsafe extern "C" { | ||
| pub unsafe fn pthread_cond_timedwait_relative_np( | ||
| cond: *mut libc::pthread_cond_t, | ||
| lock: *mut libc::pthread_mutex_t, | ||
| timeout: *const libc::timespec, | ||
| ) -> libc::c_int; | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| let mut mutex: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; | ||
| let mut cond: libc::pthread_cond_t = libc::PTHREAD_COND_INITIALIZER; | ||
|
|
||
| // Wait for 100 ms. | ||
| let timeout = libc::timespec { tv_sec: 0, tv_nsec: 100_000_000 }; | ||
|
|
||
| assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); | ||
|
|
||
| let current_time = Instant::now(); | ||
| assert_eq!( | ||
| libc::pthread_cond_timedwait_relative_np(&mut cond, &mut mutex, &timeout), | ||
| libc::ETIMEDOUT | ||
| ); | ||
| let elapsed_time = current_time.elapsed().as_millis(); | ||
| // This is actually deterministic (since isolation remains enabled), | ||
| // but can change slightly with Rust updates. | ||
| assert!(90 <= elapsed_time && elapsed_time <= 110); | ||
|
|
||
| assert_eq!(libc::pthread_mutex_unlock(&mut mutex), 0); | ||
| assert_eq!(libc::pthread_mutex_destroy(&mut mutex), 0); | ||
| assert_eq!(libc::pthread_cond_destroy(&mut cond), 0); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually an interesting API question... is
wait_timeoutallowed to return "timeout" for a spurious wakeup?The other codepath, which clamps the timeout on cygwin, still assumes that to be allowed, but this codepath here goes out of its way to avoid that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation of
wait_timeoutsays that (emphasis mine)So I'd say that spurious wakeups may not return a
WaitTimeoutResultthat indicates timeout. But then again, who knows what the system might do internally – the newer futex-like macOS implementation ofpthread_cond_timedwaitfor instance properly clamps the nanoseconds, but will not perform this check. I guess it isn't too much of an issue, given that even the clamped timeout will only actually occur after 584 (macOS) and 1000 (Cygwin) years.