File tree 1 file changed +18
-1
lines changed
library/std/src/sys/pal/unix/locks
1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 1
1
use crate :: cell:: UnsafeCell ;
2
+ use crate :: io:: Error ;
2
3
use crate :: mem:: { forget, MaybeUninit } ;
3
4
use crate :: sys:: cvt_nz;
4
5
use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
@@ -103,8 +104,24 @@ impl Mutex {
103
104
104
105
#[ inline]
105
106
pub unsafe fn lock ( & self ) {
107
+ #[ cold]
108
+ #[ inline( never) ]
109
+ fn fail ( r : i32 ) -> ! {
110
+ let error = Error :: from_raw_os_error ( r) ;
111
+ panic ! ( "failed to lock mutex: {error}" ) ;
112
+ }
113
+
106
114
let r = libc:: pthread_mutex_lock ( raw ( self ) ) ;
107
- debug_assert_eq ! ( r, 0 ) ;
115
+ // As we set the mutex type to `PTHREAD_MUTEX_NORMAL` above, we expect
116
+ // the lock call to never fail. Unfortunately however, some platforms
117
+ // (Solaris) do not conform to the standard, and instead always provide
118
+ // deadlock detection. How kind of them! Unfortunately that means that
119
+ // we need to check the error code here. To save us from UB on other
120
+ // less well-behaved platforms in the future, we do it even on "good"
121
+ // platforms like macOS. See #120147 for more context.
122
+ if r != 0 {
123
+ fail ( r)
124
+ }
108
125
}
109
126
110
127
#[ inline]
You can’t perform that action at this time.
0 commit comments