]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/mutex.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / libstd / sys / unix / mutex.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use cell::UnsafeCell;
12 use libc;
13 use mem;
14
15 pub struct Mutex { inner: UnsafeCell<libc::pthread_mutex_t> }
16
17 #[inline]
18 pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
19     m.inner.get()
20 }
21
22 unsafe impl Send for Mutex {}
23 unsafe impl Sync for Mutex {}
24
25 #[allow(dead_code)] // sys isn't exported yet
26 impl Mutex {
27     pub const fn new() -> Mutex {
28         // Might be moved and address is changing it is better to avoid
29         // initialization of potentially opaque OS data before it landed
30         Mutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
31     }
32     #[inline]
33     pub unsafe fn init(&mut self) {
34         // Issue #33770
35         //
36         // A pthread mutex initialized with PTHREAD_MUTEX_INITIALIZER will have
37         // a type of PTHREAD_MUTEX_DEFAULT, which has undefined behavior if you
38         // try to re-lock it from the same thread when you already hold a lock.
39         //
40         // In practice, glibc takes advantage of this undefined behavior to
41         // implement hardware lock elision, which uses hardware transactional
42         // memory to avoid acquiring the lock. While a transaction is in
43         // progress, the lock appears to be unlocked. This isn't a problem for
44         // other threads since the transactional memory will abort if a conflict
45         // is detected, however no abort is generated if re-locking from the
46         // same thread.
47         //
48         // Since locking the same mutex twice will result in two aliasing &mut
49         // references, we instead create the mutex with type
50         // PTHREAD_MUTEX_NORMAL which is guaranteed to deadlock if we try to
51         // re-lock it from the same thread, thus avoiding undefined behavior.
52         let mut attr: libc::pthread_mutexattr_t = mem::uninitialized();
53         let r = libc::pthread_mutexattr_init(&mut attr);
54         debug_assert_eq!(r, 0);
55         let r = libc::pthread_mutexattr_settype(&mut attr, libc::PTHREAD_MUTEX_NORMAL);
56         debug_assert_eq!(r, 0);
57         let r = libc::pthread_mutex_init(self.inner.get(), &attr);
58         debug_assert_eq!(r, 0);
59         let r = libc::pthread_mutexattr_destroy(&mut attr);
60         debug_assert_eq!(r, 0);
61     }
62     #[inline]
63     pub unsafe fn lock(&self) {
64         let r = libc::pthread_mutex_lock(self.inner.get());
65         debug_assert_eq!(r, 0);
66     }
67     #[inline]
68     pub unsafe fn unlock(&self) {
69         let r = libc::pthread_mutex_unlock(self.inner.get());
70         debug_assert_eq!(r, 0);
71     }
72     #[inline]
73     pub unsafe fn try_lock(&self) -> bool {
74         libc::pthread_mutex_trylock(self.inner.get()) == 0
75     }
76     #[inline]
77     #[cfg(not(target_os = "dragonfly"))]
78     pub unsafe fn destroy(&self) {
79         let r = libc::pthread_mutex_destroy(self.inner.get());
80         debug_assert_eq!(r, 0);
81     }
82     #[inline]
83     #[cfg(target_os = "dragonfly")]
84     pub unsafe fn destroy(&self) {
85         use libc;
86         let r = libc::pthread_mutex_destroy(self.inner.get());
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     }
93 }
94
95 pub struct ReentrantMutex { inner: UnsafeCell<libc::pthread_mutex_t> }
96
97 unsafe impl Send for ReentrantMutex {}
98 unsafe impl Sync for ReentrantMutex {}
99
100 impl ReentrantMutex {
101     pub unsafe fn uninitialized() -> ReentrantMutex {
102         ReentrantMutex { inner: mem::uninitialized() }
103     }
104
105     pub unsafe fn init(&mut self) {
106         let mut attr: libc::pthread_mutexattr_t = mem::uninitialized();
107         let result = libc::pthread_mutexattr_init(&mut attr as *mut _);
108         debug_assert_eq!(result, 0);
109         let result = libc::pthread_mutexattr_settype(&mut attr as *mut _,
110                                                     libc::PTHREAD_MUTEX_RECURSIVE);
111         debug_assert_eq!(result, 0);
112         let result = libc::pthread_mutex_init(self.inner.get(), &attr as *const _);
113         debug_assert_eq!(result, 0);
114         let result = libc::pthread_mutexattr_destroy(&mut attr as *mut _);
115         debug_assert_eq!(result, 0);
116     }
117
118     pub unsafe fn lock(&self) {
119         let result = libc::pthread_mutex_lock(self.inner.get());
120         debug_assert_eq!(result, 0);
121     }
122
123     #[inline]
124     pub unsafe fn try_lock(&self) -> bool {
125         libc::pthread_mutex_trylock(self.inner.get()) == 0
126     }
127
128     pub unsafe fn unlock(&self) {
129         let result = libc::pthread_mutex_unlock(self.inner.get());
130         debug_assert_eq!(result, 0);
131     }
132
133     pub unsafe fn destroy(&self) {
134         let result = libc::pthread_mutex_destroy(self.inner.get());
135         debug_assert_eq!(result, 0);
136     }
137 }