Skip to content

Commit 6abdd0b

Browse files
committed
Make std::sys::unix::futex consistent on emscripten.
1 parent 8305398 commit 6abdd0b

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-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+
}

0 commit comments

Comments
 (0)