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};
5
6 struct AllocatedMutex(UnsafeCell<libc::pthread_mutex_t>);
7
8 pub struct Mutex {
9 inner: LazyBox<AllocatedMutex>,
10 }
11
12 #[inline]
raw(m: &Mutex) -> *mut libc::pthread_mutex_t13 pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
14 m.inner.0.get()
15 }
16
17 unsafe impl Send for AllocatedMutex {}
18 unsafe impl Sync for AllocatedMutex {}
19
20 impl LazyInit for AllocatedMutex {
init() -> Box<Self>21 fn init() -> Box<Self> {
22 let mutex = Box::new(AllocatedMutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER)));
23
24 // Issue #33770
25 //
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.
35 //
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
42 // same thread.
43 //
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.
48 unsafe {
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(
53 attr.0.as_mut_ptr(),
54 libc::PTHREAD_MUTEX_NORMAL,
55 ))
56 .unwrap();
57 cvt_nz(libc::pthread_mutex_init(mutex.0.get(), attr.0.as_ptr())).unwrap();
58 }
59
60 mutex
61 }
62
destroy(mutex: Box<Self>)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()) };
68 drop(mutex);
69 } else {
70 // The mutex is locked. This happens if a MutexGuard is leaked.
71 // In this case, we just leak the Mutex too.
72 forget(mutex);
73 }
74 }
75
cancel_init(_: Box<Self>)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.
79 }
80 }
81
82 impl Drop for AllocatedMutex {
83 #[inline]
drop(&mut self)84 fn drop(&mut self) {
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);
92 } else {
93 debug_assert_eq!(r, 0);
94 }
95 }
96 }
97
98 impl Mutex {
99 #[inline]
new() -> Mutex100 pub const fn new() -> Mutex {
101 Mutex { inner: LazyBox::new() }
102 }
103
104 #[inline]
lock(&self)105 pub unsafe fn lock(&self) {
106 let r = libc::pthread_mutex_lock(raw(self));
107 debug_assert_eq!(r, 0);
108 }
109
110 #[inline]
unlock(&self)111 pub unsafe fn unlock(&self) {
112 let r = libc::pthread_mutex_unlock(raw(self));
113 debug_assert_eq!(r, 0);
114 }
115
116 #[inline]
try_lock(&self) -> bool117 pub unsafe fn try_lock(&self) -> bool {
118 libc::pthread_mutex_trylock(raw(self)) == 0
119 }
120 }
121
122 pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
123
124 impl Drop for PthreadMutexAttr<'_> {
drop(&mut self)125 fn drop(&mut self) {
126 unsafe {
127 let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr());
128 debug_assert_eq!(result, 0);
129 }
130 }
131 }
132