Skip to content

Commit 0d91b08

Browse files
committed
std: fix issue with perma-locked mutexes on Fuchsia
1 parent f7ae92c commit 0d91b08

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ pub mod zircon {
251251
pub const ZX_TIME_INFINITE: zx_time_t = zx_time_t::MAX;
252252

253253
pub const ZX_OK: zx_status_t = 0;
254+
pub const ZX_ERR_INVALID_ARGS: zx_status_t = -10;
255+
pub const ZX_ERR_BAD_HANDLE: zx_status_t = -11;
256+
pub const ZX_ERR_WRONG_TYPE: zx_status_t = -12;
254257
pub const ZX_ERR_BAD_STATE: zx_status_t = -20;
255258
pub const ZX_ERR_TIMED_OUT: zx_status_t = -21;
256259

@@ -264,6 +267,7 @@ pub mod zircon {
264267
) -> zx_status_t;
265268
pub fn zx_futex_wake(value_ptr: *const zx_futex_t, wake_count: u32) -> zx_status_t;
266269
pub fn zx_futex_wake_single_owner(value_ptr: *const zx_futex_t) -> zx_status_t;
270+
pub fn zx_nanosleep(deadline: zx_time_t) -> zx_status_t;
267271
pub fn zx_thread_self() -> zx_handle_t;
268272
}
269273
}

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

+16-9
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ use crate::sync::atomic::{
4242
Ordering::{Acquire, Relaxed, Release},
4343
};
4444
use crate::sys::futex::zircon::{
45-
zx_futex_wait, zx_futex_wake_single_owner, zx_handle_t, zx_thread_self, ZX_ERR_BAD_STATE,
46-
ZX_OK, ZX_TIME_INFINITE,
45+
zx_futex_wait, zx_futex_wake_single_owner, zx_handle_t, zx_nanosleep, zx_thread_self,
46+
ZX_ERR_BAD_HANDLE, ZX_ERR_BAD_STATE, ZX_ERR_INVALID_ARGS, ZX_ERR_TIMED_OUT, ZX_ERR_WRONG_TYPE,
47+
ZX_OK, ZX_TIME_INFINITE, ZX_TIME_INFINITE,
4748
};
4849

4950
// The lowest two bits of a `zx_handle_t` are always set, so the lowest bit is used to mark the
@@ -120,13 +121,19 @@ impl Mutex {
120121
to_owner(state),
121122
ZX_TIME_INFINITE,
122123
) {
123-
ZX_OK | ZX_ERR_BAD_STATE => (),
124-
// Deadlock even in the case of reentrant locking, as leaking a guard
125-
// could lead to the same condition if the thread id is reused, but
126-
// panicking is not expected in that situation. This makes things
127-
// quite a bit harder to debug, but encourages portable programming.
128-
_ if to_owner(state) == thread_self => loop {},
129-
error => panic!("futex operation failed with error code {error}"),
124+
ZX_OK | ZX_ERR_BAD_STATE | ZX_ERR_TIMED_OUT => (),
125+
// Either the current thread is trying to lock a mutex it has already locked,
126+
// or the previous owner did not unlock the mutex before exiting. Since it is
127+
// not possible to reliably detect which is the case, the current thread is
128+
// deadlocked. This makes debugging these cases quite a bit harder, but encourages
129+
// portable programming, since all other platforms do the same.
130+
//
131+
// Note that if the thread handle is reused, an arbitrary thread's priority could
132+
// be boosted by the wait, but there is currently no way to prevent that.
133+
ZX_ERR_INVALID_ARGS | ZX_ERR_BAD_HANDLE | ZX_ERR_WRONG_TYPE => loop {
134+
zx_nanosleep(ZX_TIME_INFINITE);
135+
},
136+
error => unreachable!("unexpected error code in futex wait: {error}"),
130137
}
131138
}
132139
}

0 commit comments

Comments
 (0)