|
| 1 | +use crate::cell::UnsafeCell; |
| 2 | +use crate::io::Error; |
| 3 | +use crate::mem::{forget, MaybeUninit}; |
| 4 | +use crate::sys::cvt_nz; |
| 5 | +use crate::sys_common::lazy_box::{LazyBox, LazyInit}; |
| 6 | + |
| 7 | +struct AllocatedMutex(UnsafeCell<libc::pthread_mutex_t>); |
| 8 | + |
| 9 | +pub struct Mutex { |
| 10 | + inner: LazyBox<AllocatedMutex>, |
| 11 | +} |
| 12 | + |
| 13 | +#[inline] |
| 14 | +pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t { |
| 15 | + m.inner.0.get() |
| 16 | +} |
| 17 | + |
| 18 | +unsafe impl Send for AllocatedMutex {} |
| 19 | +unsafe impl Sync for AllocatedMutex {} |
| 20 | + |
| 21 | +impl LazyInit for AllocatedMutex { |
| 22 | + fn init() -> Box<Self> { |
| 23 | + let mutex = Box::new(AllocatedMutex(UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER))); |
| 24 | + |
| 25 | + // Issue #33770 |
| 26 | + // |
| 27 | + // A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have |
| 28 | + // a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you |
| 29 | + // try to re-lock it from the same thread when you already hold a lock |
| 30 | + // (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_init.html). |
| 31 | + // This is the case even if PTHREAD_MUTEX_DEFAULT == PTHREAD_MUTEX_NORMAL |
| 32 | + // (https://github.com/rust-lang/rust/issues/33770#issuecomment-220847521) -- in that |
| 33 | + // case, `pthread_mutexattr_settype(PTHREAD_MUTEX_DEFAULT)` will of course be the same |
| 34 | + // as setting it to `PTHREAD_MUTEX_NORMAL`, but not setting any mode will result in |
| 35 | + // a Mutex where re-locking is UB. |
| 36 | + // |
| 37 | + // In practice, glibc takes advantage of this undefined behavior to |
| 38 | + // implement hardware lock elision, which uses hardware transactional |
| 39 | + // memory to avoid acquiring the lock. While a transaction is in |
| 40 | + // progress, the lock appears to be unlocked. This isn't a problem for |
| 41 | + // other threads since the transactional memory will abort if a conflict |
| 42 | + // is detected, however no abort is generated when re-locking from the |
| 43 | + // same thread. |
| 44 | + // |
| 45 | + // Since locking the same mutex twice will result in two aliasing &mut |
| 46 | + // references, we instead create the mutex with type |
| 47 | + // PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to |
| 48 | + // re-lock it from the same thread, thus avoiding undefined behavior. |
| 49 | + unsafe { |
| 50 | + let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit(); |
| 51 | + cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap(); |
| 52 | + let attr = PthreadMutexAttr(&mut attr); |
| 53 | + cvt_nz(libc::pthread_mutexattr_settype( |
| 54 | + attr.0.as_mut_ptr(), |
| 55 | + libc::PTHREAD_MUTEX_NORMAL, |
| 56 | + )) |
| 57 | + .unwrap(); |
| 58 | + cvt_nz(libc::pthread_mutex_init(mutex.0.get(), attr.0.as_ptr())).unwrap(); |
| 59 | + } |
| 60 | + |
| 61 | + mutex |
| 62 | + } |
| 63 | + |
| 64 | + fn destroy(mutex: Box<Self>) { |
| 65 | + // We're not allowed to pthread_mutex_destroy a locked mutex, |
| 66 | + // so check first if it's unlocked. |
| 67 | + if unsafe { libc::pthread_mutex_trylock(mutex.0.get()) == 0 } { |
| 68 | + unsafe { libc::pthread_mutex_unlock(mutex.0.get()) }; |
| 69 | + drop(mutex); |
| 70 | + } else { |
| 71 | + // The mutex is locked. This happens if a MutexGuard is leaked. |
| 72 | + // In this case, we just leak the Mutex too. |
| 73 | + forget(mutex); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fn cancel_init(_: Box<Self>) { |
| 78 | + // In this case, we can just drop it without any checks, |
| 79 | + // since it cannot have been locked yet. |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Drop for AllocatedMutex { |
| 84 | + #[inline] |
| 85 | + fn drop(&mut self) { |
| 86 | + let r = unsafe { libc::pthread_mutex_destroy(self.0.get()) }; |
| 87 | + if cfg!(target_os = "dragonfly") { |
| 88 | + // On DragonFly pthread_mutex_destroy() returns EINVAL if called on a |
| 89 | + // mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER. |
| 90 | + // Once it is used (locked/unlocked) or pthread_mutex_init() is called, |
| 91 | + // this behaviour no longer occurs. |
| 92 | + debug_assert!(r == 0 || r == libc::EINVAL); |
| 93 | + } else { |
| 94 | + debug_assert_eq!(r, 0); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl Mutex { |
| 100 | + #[inline] |
| 101 | + pub const fn new() -> Mutex { |
| 102 | + Mutex { inner: LazyBox::new() } |
| 103 | + } |
| 104 | + |
| 105 | + #[inline] |
| 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 | + |
| 114 | + let r = libc::pthread_mutex_lock(raw(self)); |
| 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 | + } |
| 125 | + } |
| 126 | + |
| 127 | + #[inline] |
| 128 | + pub unsafe fn unlock(&self) { |
| 129 | + let r = libc::pthread_mutex_unlock(raw(self)); |
| 130 | + debug_assert_eq!(r, 0); |
| 131 | + } |
| 132 | + |
| 133 | + #[inline] |
| 134 | + pub unsafe fn try_lock(&self) -> bool { |
| 135 | + libc::pthread_mutex_trylock(raw(self)) == 0 |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>); |
| 140 | + |
| 141 | +impl Drop for PthreadMutexAttr<'_> { |
| 142 | + fn drop(&mut self) { |
| 143 | + unsafe { |
| 144 | + let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr()); |
| 145 | + debug_assert_eq!(result, 0); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments