Skip to content

Commit

Permalink
Move linux-specific futex code into sys module.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Sep 19, 2020
1 parent dc1f97a commit fab849e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
38 changes: 38 additions & 0 deletions library/std/src/sys/unix/futex.rs
Original file line number Diff line number Diff line change
@@ -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,
);
}
}
1 change: 1 addition & 0 deletions library/std/src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,37 +72,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,
);
}
}
4 changes: 2 additions & 2 deletions library/std/src/thread/parker/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit fab849e

Please sign in to comment.