Skip to content

Commit 262193e

Browse files
committed
std: use futex-based locks and thread parker on Hermit
1 parent 4a09adf commit 262193e

File tree

8 files changed

+53
-456
lines changed

8 files changed

+53
-456
lines changed

Cargo.lock

+4-3
Original file line numberDiff line numberDiff line change
@@ -1656,12 +1656,13 @@ dependencies = [
16561656

16571657
[[package]]
16581658
name = "hermit-abi"
1659-
version = "0.2.0"
1659+
version = "0.2.6"
16601660
source = "registry+https://github.com/rust-lang/crates.io-index"
1661-
checksum = "1ab7905ea95c6d9af62940f9d7dd9596d54c334ae2c15300c482051292d5637f"
1661+
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
16621662
dependencies = [
16631663
"compiler_builtins",
16641664
"libc",
1665+
"rustc-std-workspace-alloc",
16651666
"rustc-std-workspace-core",
16661667
]
16671668

@@ -4608,7 +4609,7 @@ dependencies = [
46084609
"dlmalloc",
46094610
"fortanix-sgx-abi",
46104611
"hashbrown",
4611-
"hermit-abi 0.2.0",
4612+
"hermit-abi 0.2.6",
46124613
"libc",
46134614
"miniz_oxide 0.4.0",
46144615
"object 0.26.2",

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }
4242
fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'] }
4343

4444
[target.'cfg(target_os = "hermit")'.dependencies]
45-
hermit-abi = { version = "0.2.0", features = ['rustc-dep-of-std'] }
45+
hermit-abi = { version = "0.2.6", features = ['rustc-dep-of-std'] }
4646

4747
[target.wasm32-wasi.dependencies]
4848
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }

library/std/src/sys/hermit/condvar.rs

-90
This file was deleted.

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

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use super::abi;
2+
use crate::ptr::null;
3+
use crate::sync::atomic::AtomicU32;
4+
use crate::time::Duration;
5+
6+
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
7+
// Calculate the timeout as a relative timespec.
8+
//
9+
// Overflows are rounded up to an infinite timeout (None).
10+
let timespec = timeout.and_then(|dur| {
11+
Some(abi::timespec {
12+
tv_sec: dur.as_secs().try_into().ok()?,
13+
tv_nsec: dur.subsec_nanos().into(),
14+
})
15+
});
16+
17+
let r = unsafe {
18+
abi::futex_wait(
19+
futex.as_mut_ptr(),
20+
expected,
21+
timespec.as_ref().map_or(null(), |t| t as *const abi::timespec),
22+
abi::FUTEX_RELATIVE_TIMEOUT,
23+
)
24+
};
25+
26+
r != -abi::errno::ETIMEDOUT
27+
}
28+
29+
#[inline]
30+
pub fn futex_wake(futex: &AtomicU32) -> bool {
31+
unsafe { abi::futex_wake(futex.as_mut_ptr(), 1) > 0 }
32+
}
33+
34+
#[inline]
35+
pub fn futex_wake_all(futex: &AtomicU32) {
36+
unsafe {
37+
abi::futex_wake(futex.as_mut_ptr(), i32::MAX);
38+
}
39+
}

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub mod cmath;
2525
pub mod env;
2626
pub mod fd;
2727
pub mod fs;
28+
pub mod futex;
2829
#[path = "../unsupported/io.rs"]
2930
pub mod io;
3031
pub mod memchr;
@@ -45,14 +46,14 @@ pub mod thread_local_dtor;
4546
pub mod thread_local_key;
4647
pub mod time;
4748

48-
mod condvar;
49-
mod mutex;
50-
mod rwlock;
51-
49+
#[path = "../unix/locks"]
5250
pub mod locks {
53-
pub use super::condvar::*;
54-
pub use super::mutex::*;
55-
pub use super::rwlock::*;
51+
mod futex_condvar;
52+
mod futex_mutex;
53+
mod futex_rwlock;
54+
pub(crate) use futex_condvar::MovableCondvar;
55+
pub(crate) use futex_mutex::{MovableMutex, Mutex};
56+
pub(crate) use futex_rwlock::{MovableRwLock, RwLock};
5657
}
5758

5859
use crate::io::ErrorKind;

0 commit comments

Comments
 (0)