Skip to content

Commit da1ddf3

Browse files
authored
Rollup merge of rust-lang#96205 - m-ou-se:emscripten-futex-locks, r=thomcc
Use futex locks on emscripten. This switches Emscripten to the futex-based lock implementations, away from pthread. Tracking issue: rust-lang#93740
2 parents 9d9d591 + 06a8f05 commit da1ddf3

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

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

+25-22
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,6 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
5252
}
5353
}
5454

55-
#[cfg(target_os = "emscripten")]
56-
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) {
57-
extern "C" {
58-
fn emscripten_futex_wait(
59-
addr: *const AtomicU32,
60-
val: libc::c_uint,
61-
max_wait_ms: libc::c_double,
62-
) -> libc::c_int;
63-
}
64-
65-
unsafe {
66-
emscripten_futex_wait(
67-
futex,
68-
expected,
69-
timeout.map_or(crate::f64::INFINITY, |d| d.as_secs_f64() * 1000.0),
70-
);
71-
}
72-
}
73-
7455
/// Wake up one thread that's blocked on futex_wait on this futex.
7556
///
7657
/// Returns true if this actually woke up such a thread,
@@ -101,10 +82,32 @@ pub fn futex_wake_all(futex: &AtomicU32) {
10182
}
10283

10384
#[cfg(target_os = "emscripten")]
104-
pub fn futex_wake(futex: &AtomicU32) -> bool {
105-
extern "C" {
106-
fn emscripten_futex_wake(addr: *const AtomicU32, count: libc::c_int) -> libc::c_int;
85+
extern "C" {
86+
fn emscripten_futex_wake(addr: *const AtomicU32, count: libc::c_int) -> libc::c_int;
87+
fn emscripten_futex_wait(
88+
addr: *const AtomicU32,
89+
val: libc::c_uint,
90+
max_wait_ms: libc::c_double,
91+
) -> libc::c_int;
92+
}
93+
94+
#[cfg(target_os = "emscripten")]
95+
pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -> bool {
96+
unsafe {
97+
emscripten_futex_wait(
98+
futex,
99+
expected,
100+
timeout.map_or(f64::INFINITY, |d| d.as_secs_f64() * 1000.0),
101+
) != -libc::ETIMEDOUT
107102
}
103+
}
108104

105+
#[cfg(target_os = "emscripten")]
106+
pub fn futex_wake(futex: &AtomicU32) -> bool {
109107
unsafe { emscripten_futex_wake(futex, 1) > 0 }
110108
}
109+
110+
#[cfg(target_os = "emscripten")]
111+
pub fn futex_wake_all(futex: &AtomicU32) {
112+
unsafe { emscripten_futex_wake(futex, i32::MAX) };
113+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cfg_if::cfg_if! {
22
if #[cfg(any(
33
target_os = "linux",
44
target_os = "android",
5+
all(target_os = "emscripten", target_feature = "atomics"),
56
))] {
67
mod futex;
78
mod futex_rwlock;

0 commit comments

Comments
 (0)