]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/mutex.rs
Rollup merge of #63630 - andjo403:bump_compiler, r=nikomatsakis
[rust.git] / src / libstd / sys / unix / mutex.rs
1 use crate::cell::UnsafeCell;
2 use crate::mem::MaybeUninit;
3
4 pub struct Mutex { inner: UnsafeCell<libc::pthread_mutex_t> }
5
6 #[inline]
7 pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
8     m.inner.get()
9 }
10
11 unsafe impl Send for Mutex {}
12 unsafe impl Sync for Mutex {}
13
14 #[allow(dead_code)] // sys isn't exported yet
15 impl Mutex {
16     pub const fn new() -> Mutex {
17         // Might be moved to a different address, so it is better to avoid
18         // initialization of potentially opaque OS data before it landed.
19         // Be very careful using this newly constructed `Mutex`, reentrant
20         // locking is undefined behavior until `init` is called!
21         Mutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
22     }
23     #[inline]
24     pub unsafe fn init(&mut self) {
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         //
31         // In practice, glibc takes advantage of this undefined behavior to
32         // implement hardware lock elision, which uses hardware transactional
33         // memory to avoid acquiring the lock. While a transaction is in
34         // progress, the lock appears to be unlocked. This isn't a problem for
35         // other threads since the transactional memory will abort if a conflict
36         // is detected, however no abort is generated if re-locking from the
37         // same thread.
38         //
39         // Since locking the same mutex twice will result in two aliasing &mut
40         // references, we instead create the mutex with type
41         // PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to
42         // re-lock it from the same thread, thus avoiding undefined behavior.
43         let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
44         let r = libc::pthread_mutexattr_init(attr.as_mut_ptr());
45         debug_assert_eq!(r, 0);
46         let r = libc::pthread_mutexattr_settype(attr.as_mut_ptr(), libc::PTHREAD_MUTEX_NORMAL);
47         debug_assert_eq!(r, 0);
48         let r = libc::pthread_mutex_init(self.inner.get(), attr.as_ptr());
49         debug_assert_eq!(r, 0);
50         let r = libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
51         debug_assert_eq!(r, 0);
52     }
53     #[inline]
54     pub unsafe fn lock(&self) {
55         let r = libc::pthread_mutex_lock(self.inner.get());
56         debug_assert_eq!(r, 0);
57     }
58     #[inline]
59     pub unsafe fn unlock(&self) {
60         let r = libc::pthread_mutex_unlock(self.inner.get());
61         debug_assert_eq!(r, 0);
62     }
63     #[inline]
64     pub unsafe fn try_lock(&self) -> bool {
65         libc::pthread_mutex_trylock(self.inner.get()) == 0
66     }
67     #[inline]
68     #[cfg(not(target_os = "dragonfly"))]
69     pub unsafe fn destroy(&self) {
70         let r = libc::pthread_mutex_destroy(self.inner.get());
71         debug_assert_eq!(r, 0);
72     }
73     #[inline]
74     #[cfg(target_os = "dragonfly")]
75     pub unsafe fn destroy(&self) {
76         let r = libc::pthread_mutex_destroy(self.inner.get());
77         // On DragonFly pthread_mutex_destroy() returns EINVAL if called on a
78         // mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
79         // Once it is used (locked/unlocked) or pthread_mutex_init() is called,
80         // this behaviour no longer occurs.
81         debug_assert!(r == 0 || r == libc::EINVAL);
82     }
83 }
84
85 pub struct ReentrantMutex { inner: UnsafeCell<libc::pthread_mutex_t> }
86
87 unsafe impl Send for ReentrantMutex {}
88 unsafe impl Sync for ReentrantMutex {}
89
90 impl ReentrantMutex {
91     pub unsafe fn uninitialized() -> ReentrantMutex {
92         ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
93     }
94
95     pub unsafe fn init(&mut self) {
96         let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
97         let result = libc::pthread_mutexattr_init(attr.as_mut_ptr());
98         debug_assert_eq!(result, 0);
99         let result = libc::pthread_mutexattr_settype(attr.as_mut_ptr(),
100                                                     libc::PTHREAD_MUTEX_RECURSIVE);
101         debug_assert_eq!(result, 0);
102         let result = libc::pthread_mutex_init(self.inner.get(), attr.as_ptr());
103         debug_assert_eq!(result, 0);
104         let result = libc::pthread_mutexattr_destroy(attr.as_mut_ptr());
105         debug_assert_eq!(result, 0);
106     }
107
108     pub unsafe fn lock(&self) {
109         let result = libc::pthread_mutex_lock(self.inner.get());
110         debug_assert_eq!(result, 0);
111     }
112
113     #[inline]
114     pub unsafe fn try_lock(&self) -> bool {
115         libc::pthread_mutex_trylock(self.inner.get()) == 0
116     }
117
118     pub unsafe fn unlock(&self) {
119         let result = libc::pthread_mutex_unlock(self.inner.get());
120         debug_assert_eq!(result, 0);
121     }
122
123     pub unsafe fn destroy(&self) {
124         let result = libc::pthread_mutex_destroy(self.inner.get());
125         debug_assert_eq!(result, 0);
126     }
127 }