1 use crate::cell
::UnsafeCell
;
2 use crate::mem
::MaybeUninit
;
5 inner
: UnsafeCell
<libc
::pthread_mutex_t
>,
9 pub unsafe fn raw(m
: &Mutex
) -> *mut libc
::pthread_mutex_t
{
13 unsafe impl Send
for Mutex {}
14 unsafe impl Sync
for Mutex {}
16 #[allow(dead_code)] // sys isn't exported yet
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) }
26 pub unsafe fn init(&mut self) {
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.
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
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);
56 pub unsafe fn lock(&self) {
57 let r
= libc
::pthread_mutex_lock(self.inner
.get());
58 debug_assert_eq
!(r
, 0);
61 pub unsafe fn unlock(&self) {
62 let r
= libc
::pthread_mutex_unlock(self.inner
.get());
63 debug_assert_eq
!(r
, 0);
66 pub unsafe fn try_lock(&self) -> bool
{
67 libc
::pthread_mutex_trylock(self.inner
.get()) == 0
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);
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
);
87 pub struct ReentrantMutex
{
88 inner
: UnsafeCell
<libc
::pthread_mutex_t
>,
91 unsafe impl Send
for ReentrantMutex {}
92 unsafe impl Sync
for ReentrantMutex {}
95 pub const unsafe fn uninitialized() -> ReentrantMutex
{
96 ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
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);
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);
112 pub unsafe fn lock(&self) {
113 let result
= libc
::pthread_mutex_lock(self.inner
.get());
114 debug_assert_eq
!(result
, 0);
118 pub unsafe fn try_lock(&self) -> bool
{
119 libc
::pthread_mutex_trylock(self.inner
.get()) == 0
122 pub unsafe fn unlock(&self) {
123 let result
= libc
::pthread_mutex_unlock(self.inner
.get());
124 debug_assert_eq
!(result
, 0);
127 pub unsafe fn destroy(&self) {
128 let result
= libc
::pthread_mutex_destroy(self.inner
.get());
129 debug_assert_eq
!(result
, 0);