diff --git a/library/std/src/sys/unix/futex.rs b/library/std/src/sys/unix/futex.rs new file mode 100644 index 00000000000..6af06aa5f7e --- /dev/null +++ b/library/std/src/sys/unix/futex.rs @@ -0,0 +1,38 @@ +#![cfg(any(target_os = "linux", target_os = "android"))] + +use crate::sync::atomic::AtomicI32; +use crate::time::Duration; + +pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option) { + let timespec; + let timespec_ptr = match timeout { + Some(timeout) => { + timespec = libc::timespec { + tv_sec: timeout.as_secs() as _, + tv_nsec: timeout.subsec_nanos() as _, + }; + ×pec as *const libc::timespec + } + None => crate::ptr::null(), + }; + unsafe { + libc::syscall( + libc::SYS_futex, + futex as *const AtomicI32, + libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG, + expected, + timespec_ptr, + ); + } +} + +pub fn futex_wake(futex: &AtomicI32) { + unsafe { + libc::syscall( + libc::SYS_futex, + futex as *const AtomicI32, + libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG, + 1, + ); + } +} diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index eddf00d3979..6c623e21099 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -49,6 +49,7 @@ pub mod env; pub mod ext; pub mod fd; pub mod fs; +pub mod futex; pub mod io; #[cfg(target_os = "l4re")] mod l4re; diff --git a/library/std/src/thread/parker/linux.rs b/library/std/src/thread/parker/futex.rs similarity index 74% rename from library/std/src/thread/parker/linux.rs rename to library/std/src/thread/parker/futex.rs index 090c83fbb40..2d7a7770508 100644 --- a/library/std/src/thread/parker/linux.rs +++ b/library/std/src/thread/parker/futex.rs @@ -1,5 +1,6 @@ use crate::sync::atomic::AtomicI32; use crate::sync::atomic::Ordering::{Acquire, Release}; +use crate::sys::futex::{futex_wait, futex_wake}; use crate::time::Duration; const PARKED: i32 = -1; @@ -70,37 +71,3 @@ impl Parker { } } } - -fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option) { - let timespec; - let timespec_ptr = match timeout { - Some(timeout) => { - timespec = libc::timespec { - tv_sec: timeout.as_secs() as _, - tv_nsec: timeout.subsec_nanos() as _, - }; - ×pec as *const libc::timespec - } - None => crate::ptr::null(), - }; - unsafe { - libc::syscall( - libc::SYS_futex, - futex as *const AtomicI32, - libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG, - expected, - timespec_ptr, - ); - } -} - -fn futex_wake(futex: &AtomicI32) { - unsafe { - libc::syscall( - libc::SYS_futex, - futex as *const AtomicI32, - libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG, - 1, - ); - } -} diff --git a/library/std/src/thread/parker/mod.rs b/library/std/src/thread/parker/mod.rs index 4dc5e1aa271..23c17c8e2cf 100644 --- a/library/std/src/thread/parker/mod.rs +++ b/library/std/src/thread/parker/mod.rs @@ -1,7 +1,7 @@ cfg_if::cfg_if! { if #[cfg(any(target_os = "linux", target_os = "android"))] { - mod linux; - pub use linux::Parker; + mod futex; + pub use futex::Parker; } else { mod generic; pub use generic::Parker;