Skip to content

Commit

Permalink
Check conversion from Duration to timespec in futex_wait.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Sep 19, 2020
1 parent fab849e commit 195c4fe
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions library/std/src/sys/unix/futex.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#![cfg(any(target_os = "linux", target_os = "android"))]

use crate::convert::TryInto;
use crate::ptr::null;
use crate::sync::atomic::AtomicI32;
use crate::time::Duration;

pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
let timespec;
let timespec_ptr = match timeout {
Some(timeout) => {
timespec = libc::timespec {
tv_sec: timeout.as_secs() as _,
tv_nsec: timeout.subsec_nanos() as _,
};
&timespec as *const libc::timespec
}
None => crate::ptr::null(),
};
let timespec = timeout.and_then(|d| {
Some(libc::timespec {
// Sleep forever if the timeout is longer than fits in a timespec.
tv_sec: d.as_secs().try_into().ok()?,
// This conversion never truncates, as subsec_nanos is always <1e9.
tv_nsec: d.subsec_nanos() as _,
})
});
unsafe {
libc::syscall(
libc::SYS_futex,
futex as *const AtomicI32,
libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
expected,
timespec_ptr,
timespec.as_ref().map_or(null(), |d| d as *const libc::timespec),
);
}
}
Expand Down

0 comments on commit 195c4fe

Please sign in to comment.