2020-09-19 16:03:10 +00:00
|
|
|
#![cfg(any(target_os = "linux", target_os = "android"))]
|
|
|
|
|
2020-09-19 22:17:05 +00:00
|
|
|
use crate::convert::TryInto;
|
|
|
|
use crate::ptr::null;
|
2020-09-19 16:03:10 +00:00
|
|
|
use crate::sync::atomic::AtomicI32;
|
|
|
|
use crate::time::Duration;
|
|
|
|
|
|
|
|
pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
|
2020-09-19 22:17:05 +00:00
|
|
|
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 _,
|
|
|
|
})
|
|
|
|
});
|
2020-09-19 16:03:10 +00:00
|
|
|
unsafe {
|
|
|
|
libc::syscall(
|
|
|
|
libc::SYS_futex,
|
|
|
|
futex as *const AtomicI32,
|
|
|
|
libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
|
|
|
|
expected,
|
2020-09-19 22:17:05 +00:00
|
|
|
timespec.as_ref().map_or(null(), |d| d as *const libc::timespec),
|
2020-09-19 16:03:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn futex_wake(futex: &AtomicI32) {
|
|
|
|
unsafe {
|
|
|
|
libc::syscall(
|
|
|
|
libc::SYS_futex,
|
|
|
|
futex as *const AtomicI32,
|
|
|
|
libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|