@@ -26,6 +26,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
2626 use super :: time:: Timespec ;
2727 use crate :: ptr:: null;
2828 use crate :: sync:: atomic:: Ordering :: Relaxed ;
29+ use crate :: sys:: cvt;
2930
3031 // Calculate the timeout as an absolute timespec.
3132 //
@@ -40,7 +41,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
4041 return true ;
4142 }
4243
43- let r = unsafe {
44+ let r = cvt ( unsafe {
4445 cfg_if:: cfg_if! {
4546 if #[ cfg( target_os = "freebsd" ) ] {
4647 // FreeBSD doesn't have futex(), but it has
@@ -77,12 +78,16 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
7778 compile_error!( "unknown target_os" ) ;
7879 }
7980 }
80- } ;
81-
82- match ( r < 0 ) . then ( super :: os:: errno) {
83- Some ( libc:: ETIMEDOUT ) => return false ,
84- Some ( libc:: EINTR ) => continue ,
85- _ => return true ,
81+ } ) ;
82+
83+ match r {
84+ Ok ( _) => return true ,
85+ Err ( e) => match e. raw_os_error ( ) {
86+ Some ( libc:: ETIMEDOUT ) => return false ,
87+ Some ( libc:: EINTR ) => continue ,
88+ Some ( libc:: EAGAIN ) => return true ,
89+ _ => panic ! ( "failed to wait on futex: {e:?}" ) ,
90+ } ,
8691 }
8792 }
8893}
@@ -95,19 +100,22 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
95100/// On some platforms, this always returns false.
96101#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
97102pub fn futex_wake ( futex : & AtomicU32 ) -> bool {
103+ use crate :: sys:: cvt;
98104 let ptr = futex as * const AtomicU32 ;
99105 let op = libc:: FUTEX_WAKE | libc:: FUTEX_PRIVATE_FLAG ;
100- unsafe { libc:: syscall ( libc:: SYS_futex , ptr, op, 1 ) > 0 }
106+ cvt ( unsafe { libc:: syscall ( libc:: SYS_futex , ptr, op, 1 ) } )
107+ . expect ( "failed to wake futex waiters" )
108+ > 0
101109}
102110
103111/// Wakes up all threads that are waiting on `futex_wait` on this futex.
104112#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
105113pub fn futex_wake_all ( futex : & AtomicU32 ) {
114+ use crate :: sys:: cvt;
106115 let ptr = futex as * const AtomicU32 ;
107116 let op = libc:: FUTEX_WAKE | libc:: FUTEX_PRIVATE_FLAG ;
108- unsafe {
109- libc:: syscall ( libc:: SYS_futex , ptr, op, i32:: MAX ) ;
110- }
117+ cvt ( unsafe { libc:: syscall ( libc:: SYS_futex , ptr, op, i32:: MAX ) } )
118+ . expect ( "failed to wake futex waiters" ) ;
111119}
112120
113121// FreeBSD doesn't tell us how many threads are woken up, so this always returns false.
0 commit comments