-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -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_timeout
allowed 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_timeout
says that (emphasis mine)So I'd say that spurious wakeups may not return a
WaitTimeoutResult
that indicates timeout. But then again, who knows what the system might do internally – the newer futex-like macOS implementation ofpthread_cond_timedwait
for 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.