Skip to content

Commit b35ab1b

Browse files
committed
feat(library/std safaos): impl Futex, Mutex, RwLock, Once, and CondVar support
1 parent 83a5747 commit b35ab1b

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::sync::atomic::Atomic;
2+
use crate::time::Duration;
3+
4+
/// An atomic for use as a futex that is at least 32-bits but may be larger
5+
pub type Futex = Atomic<Primitive>;
6+
/// Must be the underlying type of Futex
7+
pub type Primitive = u32;
8+
9+
/// An atomic for use as a futex that is at least 8-bits but may be larger.
10+
pub type SmallFutex = Atomic<SmallPrimitive>;
11+
/// Must be the underlying type of SmallFutex
12+
pub type SmallPrimitive = u32;
13+
14+
pub fn futex_wait(futex: &Atomic<u32>, expected: u32, timeout: Option<Duration>) -> bool {
15+
// FIXME: Infinite timeout is just the max for now
16+
let timeout_duration = timeout.unwrap_or(Duration::MAX);
17+
let results = safa_api::syscalls::futex::futex_wait(futex, expected, timeout_duration)
18+
.expect("FATAL System error while waiting for Futex");
19+
results
20+
}
21+
22+
#[inline]
23+
pub fn futex_wake(futex: &Atomic<u32>) -> bool {
24+
let results = safa_api::syscalls::futex::futex_wake(futex, 1)
25+
.expect("FATAL System error while waking 1 Futex")
26+
> 0;
27+
results
28+
}
29+
30+
#[inline]
31+
pub fn futex_wake_all(futex: &Atomic<u32>) {
32+
safa_api::syscalls::futex::futex_wake(futex, usize::MAX)
33+
.expect("FATAL System error while waking all Futexes");
34+
}

library/std/src/sys/pal/safaos/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
22

3+
pub mod futex;
34
pub mod os;
45
pub mod pipe;
56
pub mod resources;

library/std/src/sys/sync/condvar/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cfg_if::cfg_if! {
99
target_os = "fuchsia",
1010
all(target_family = "wasm", target_feature = "atomics"),
1111
target_os = "hermit",
12+
target_os = "safaos",
1213
))] {
1314
mod futex;
1415
pub use futex::Condvar;

library/std/src/sys/sync/mutex/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cfg_if::cfg_if! {
88
target_os = "dragonfly",
99
all(target_family = "wasm", target_feature = "atomics"),
1010
target_os = "hermit",
11+
target_os = "safaos",
1112
))] {
1213
mod futex;
1314
pub use futex::Mutex;

library/std/src/sys/sync/once/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cfg_if::cfg_if! {
1818
target_os = "dragonfly",
1919
target_os = "fuchsia",
2020
target_os = "hermit",
21+
target_os = "safaos",
2122
))] {
2223
mod futex;
2324
pub use futex::{Once, OnceState};

library/std/src/sys/sync/rwlock/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cfg_if::cfg_if! {
99
target_os = "fuchsia",
1010
all(target_family = "wasm", target_feature = "atomics"),
1111
target_os = "hermit",
12+
target_os = "safaos",
1213
))] {
1314
mod futex;
1415
pub use futex::RwLock;

library/std/src/sys/sync/thread_parking/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cfg_if::cfg_if! {
99
target_os = "dragonfly",
1010
target_os = "fuchsia",
1111
target_os = "hermit",
12+
target_os = "safaos",
1213
))] {
1314
mod futex;
1415
pub use futex::Parker;

0 commit comments

Comments
 (0)