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