Move linux-specific futex code into sys module.

This commit is contained in:
Mara Bos 2020-09-19 18:03:10 +02:00
parent 568d9696e9
commit 2cf0f64722
4 changed files with 42 additions and 36 deletions

View 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 _,
};
&timespec 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,
);
}
}

View File

@ -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;

View File

@ -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<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(),
};
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,
);
}
}

View File

@ -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;