This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed
library/std/src/sys/pal/unix/locks Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 11use crate :: cell:: UnsafeCell ;
2+ use crate :: io:: Error ;
23use crate :: mem:: { forget, MaybeUninit } ;
34use crate :: sys:: cvt_nz;
45use crate :: sys_common:: lazy_box:: { LazyBox , LazyInit } ;
@@ -103,8 +104,24 @@ impl Mutex {
103104
104105 #[ inline]
105106 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+
106114 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+ }
108125 }
109126
110127 #[ inline]
You can’t perform that action at this time.
0 commit comments