mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-03 21:47:36 +00:00
Move linux-specific futex code into sys
module.
This commit is contained in:
parent
568d9696e9
commit
2cf0f64722
38
library/std/src/sys/unix/futex.rs
Normal file
38
library/std/src/sys/unix/futex.rs
Normal file
@ -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<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 _,
|
||||||
|
};
|
||||||
|
×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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -49,6 +49,7 @@ pub mod env;
|
|||||||
pub mod ext;
|
pub mod ext;
|
||||||
pub mod fd;
|
pub mod fd;
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
|
pub mod futex;
|
||||||
pub mod io;
|
pub mod io;
|
||||||
#[cfg(target_os = "l4re")]
|
#[cfg(target_os = "l4re")]
|
||||||
mod l4re;
|
mod l4re;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::sync::atomic::AtomicI32;
|
use crate::sync::atomic::AtomicI32;
|
||||||
use crate::sync::atomic::Ordering::{Acquire, Release};
|
use crate::sync::atomic::Ordering::{Acquire, Release};
|
||||||
|
use crate::sys::futex::{futex_wait, futex_wake};
|
||||||
use crate::time::Duration;
|
use crate::time::Duration;
|
||||||
|
|
||||||
const PARKED: i32 = -1;
|
const PARKED: i32 = -1;
|
||||||
@ -70,37 +71,3 @@ impl Parker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 _,
|
|
||||||
};
|
|
||||||
×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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(any(target_os = "linux", target_os = "android"))] {
|
if #[cfg(any(target_os = "linux", target_os = "android"))] {
|
||||||
mod linux;
|
mod futex;
|
||||||
pub use linux::Parker;
|
pub use futex::Parker;
|
||||||
} else {
|
} else {
|
||||||
mod generic;
|
mod generic;
|
||||||
pub use generic::Parker;
|
pub use generic::Parker;
|
||||||
|
Loading…
Reference in New Issue
Block a user