Skip to content

Commit fab849e

Browse files
committed
Move linux-specific futex code into sys module.
1 parent dc1f97a commit fab849e

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

library/std/src/sys/unix/futex.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![cfg(any(target_os = "linux", target_os = "android"))]
2+
3+
use crate::sync::atomic::AtomicI32;
4+
use crate::time::Duration;
5+
6+
pub fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
7+
let timespec;
8+
let timespec_ptr = match timeout {
9+
Some(timeout) => {
10+
timespec = libc::timespec {
11+
tv_sec: timeout.as_secs() as _,
12+
tv_nsec: timeout.subsec_nanos() as _,
13+
};
14+
&timespec as *const libc::timespec
15+
}
16+
None => crate::ptr::null(),
17+
};
18+
unsafe {
19+
libc::syscall(
20+
libc::SYS_futex,
21+
futex as *const AtomicI32,
22+
libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
23+
expected,
24+
timespec_ptr,
25+
);
26+
}
27+
}
28+
29+
pub fn futex_wake(futex: &AtomicI32) {
30+
unsafe {
31+
libc::syscall(
32+
libc::SYS_futex,
33+
futex as *const AtomicI32,
34+
libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
35+
1,
36+
);
37+
}
38+
}

library/std/src/sys/unix/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub mod env;
4949
pub mod ext;
5050
pub mod fd;
5151
pub mod fs;
52+
pub mod futex;
5253
pub mod io;
5354
#[cfg(target_os = "l4re")]
5455
mod l4re;

library/std/src/thread/parker/linux.rs renamed to library/std/src/thread/parker/futex.rs

+1-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::sync::atomic::AtomicI32;
22
use crate::sync::atomic::Ordering::{Acquire, Release};
3+
use crate::sys::futex::{futex_wait, futex_wake};
34
use crate::time::Duration;
45

56
const PARKED: i32 = -1;
@@ -71,37 +72,3 @@ impl Parker {
7172
}
7273
}
7374
}
74-
75-
fn futex_wait(futex: &AtomicI32, expected: i32, timeout: Option<Duration>) {
76-
let timespec;
77-
let timespec_ptr = match timeout {
78-
Some(timeout) => {
79-
timespec = libc::timespec {
80-
tv_sec: timeout.as_secs() as _,
81-
tv_nsec: timeout.subsec_nanos() as _,
82-
};
83-
&timespec as *const libc::timespec
84-
}
85-
None => crate::ptr::null(),
86-
};
87-
unsafe {
88-
libc::syscall(
89-
libc::SYS_futex,
90-
futex as *const AtomicI32,
91-
libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
92-
expected,
93-
timespec_ptr,
94-
);
95-
}
96-
}
97-
98-
fn futex_wake(futex: &AtomicI32) {
99-
unsafe {
100-
libc::syscall(
101-
libc::SYS_futex,
102-
futex as *const AtomicI32,
103-
libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
104-
1,
105-
);
106-
}
107-
}

library/std/src/thread/parker/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cfg_if::cfg_if! {
22
if #[cfg(any(target_os = "linux", target_os = "android"))] {
3-
mod linux;
4-
pub use linux::Parker;
3+
mod futex;
4+
pub use futex::Parker;
55
} else {
66
mod generic;
77
pub use generic::Parker;

0 commit comments

Comments
 (0)