1 use crate::cell::UnsafeCell;
2 use crate::mem::{forget, MaybeUninit};
3 use crate::sys::cvt_nz;
4 use crate::sys_common::lazy_box::{LazyBox, LazyInit};
6 struct AllocatedMutex(UnsafeCell<libc::pthread_mutex_t>);
9 inner: LazyBox<AllocatedMutex>,
13 pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
17 unsafe impl Send for AllocatedMutex {}
18 unsafe impl Sync for AllocatedMutex {}
20 impl LazyInit for AllocatedMutex {
21 fn init() -> Box<Self> {
22 let mutex = Box::new(AllocatedMutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER)));
26 // A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have
27 // a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you
28 // try to re-lock it from the same thread when you already hold a lock
29 // (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html).
30 // This is the case even if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL
31 // (https://github.com/rust-lang/rust/issues/33770#issuecomment-220847521) -- in that
32 // case, `pthread_mutexattr_settype(PTHREAD_MUTEX_DEFAULT)` will of course be the same
33 // as setting it to `PTHREAD_MUTEX_NORMAL`, but not setting any mode will result in
34 // a Mutex where re-locking is UB.
36 // In practice, glibc takes advantage of this undefined behavior to
37 // implement hardware lock elision, which uses hardware transactional
38 // memory to avoid acquiring the lock. While a transaction is in
39 // progress, the lock appears to be unlocked. This isn't a problem for
40 // other threads since the transactional memory will abort if a conflict
41 // is detected, however no abort is generated when re-locking from the
44 // Since locking the same mutex twice will result in two aliasing &mut
45 // references, we instead create the mutex with type
46 // PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to
47 // re-lock it from the same thread, thus avoiding undefined behavior.
49 let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
50 cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
51 let attr = PthreadMutexAttr(&mut attr);
52 cvt_nz(libc::pthread_mutexattr_settype(
54 libc::PTHREAD_MUTEX_NORMAL,
57 cvt_nz(libc::pthread_mutex_init(mutex.0.get(), attr.0.as_ptr())).unwrap();
63 fn destroy(mutex: Box<Self>) {
64 // We're not allowed to pthread_mutex_destroy a locked mutex,
65 // so check first if it's unlocked.
66 if unsafe { libc::pthread_mutex_trylock(mutex.0.get()) == 0 } {
67 unsafe { libc::pthread_mutex_unlock(mutex.0.get()) };
70 // The mutex is locked. This happens if a MutexGuard is leaked.
71 // In this case, we just leak the Mutex too.
76 fn cancel_init(_: Box<Self>) {
77 // In this case, we can just drop it without any checks,
78 // since it cannot have been locked yet.
82 impl Drop for AllocatedMutex {
85 let r = unsafe { libc::pthread_mutex_destroy(self.0.get()) };
86 if cfg!(target_os = "dragonfly") {
87 // On DragonFly pthread_mutex_destroy() returns EINVAL if called on a
88 // mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
89 // Once it is used (locked/unlocked) or pthread_mutex_init() is called,
90 // this behaviour no longer occurs.
91 debug_assert!(r == 0 || r == libc::EINVAL);
93 debug_assert_eq!(r, 0);
100 pub const fn new() -> Mutex {
101 Mutex { inner: LazyBox::new() }
105 pub unsafe fn lock(&self) {
106 let r = libc::pthread_mutex_lock(raw(self));
107 debug_assert_eq!(r, 0);
111 pub unsafe fn unlock(&self) {
112 let r = libc::pthread_mutex_unlock(raw(self));
113 debug_assert_eq!(r, 0);
117 pub unsafe fn try_lock(&self) -> bool {
118 libc::pthread_mutex_trylock(raw(self)) == 0
122 pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
124 impl Drop for PthreadMutexAttr<'_> {
127 let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr());
128 debug_assert_eq!(result, 0);