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.
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.
15 pub struct Mutex { inner: UnsafeCell<libc::pthread_mutex_t> }
18 pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
22 unsafe impl Send for Mutex {}
23 unsafe impl Sync for Mutex {}
25 #[allow(dead_code)] // sys isn't exported yet
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) }
33 pub unsafe fn init(&mut self) {
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.
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
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);
63 pub unsafe fn lock(&self) {
64 let r = libc::pthread_mutex_lock(self.inner.get());
65 debug_assert_eq!(r, 0);
68 pub unsafe fn unlock(&self) {
69 let r = libc::pthread_mutex_unlock(self.inner.get());
70 debug_assert_eq!(r, 0);
73 pub unsafe fn try_lock(&self) -> bool {
74 libc::pthread_mutex_trylock(self.inner.get()) == 0
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);
83 #[cfg(target_os = "dragonfly")]
84 pub unsafe fn destroy(&self) {
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);
95 pub struct ReentrantMutex { inner: UnsafeCell<libc::pthread_mutex_t> }
97 unsafe impl Send for ReentrantMutex {}
98 unsafe impl Sync for ReentrantMutex {}
100 impl ReentrantMutex {
101 pub unsafe fn uninitialized() -> ReentrantMutex {
102 ReentrantMutex { inner: mem::uninitialized() }
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);
118 pub unsafe fn lock(&self) {
119 let result = libc::pthread_mutex_lock(self.inner.get());
120 debug_assert_eq!(result, 0);
124 pub unsafe fn try_lock(&self) -> bool {
125 libc::pthread_mutex_trylock(self.inner.get()) == 0
128 pub unsafe fn unlock(&self) {
129 let result = libc::pthread_mutex_unlock(self.inner.get());
130 debug_assert_eq!(result, 0);
133 pub unsafe fn destroy(&self) {
134 let result = libc::pthread_mutex_destroy(self.inner.get());
135 debug_assert_eq!(result, 0);