@@ -42,8 +42,9 @@ use crate::sync::atomic::{
4242 Ordering :: { Acquire , Relaxed , Release } ,
4343} ;
4444use 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