Skip to content

Commit 0bf8528

Browse files
committed
Modify sys_common::ReentrantMutex to use parking_lot
1 parent d375525 commit 0bf8528

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

src/libstd/sys_common/parking_lot/remutex.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ impl RawThreadId {
2424
}
2525
}
2626

27-
struct RawReentrantMutex {
27+
pub struct RawReentrantMutex {
2828
owner: AtomicUsize,
2929
lock_count: Cell<usize>,
3030
mutex: RawMutex,
3131
get_thread_id: RawThreadId,
3232
}
3333

3434
impl RawReentrantMutex {
35+
pub const INIT: RawReentrantMutex = RawReentrantMutex {
36+
owner: AtomicUsize::new(0),
37+
lock_count: Cell::new(0),
38+
mutex: RawMutex::INIT,
39+
get_thread_id: RawThreadId::INIT,
40+
};
41+
3542
#[inline]
3643
fn lock_internal<F: FnOnce() -> bool>(&self, try_lock: F) -> bool {
3744
let id = self.get_thread_id.nonzero_thread_id();
@@ -53,20 +60,20 @@ impl RawReentrantMutex {
5360
}
5461

5562
#[inline]
56-
fn lock(&self) {
63+
pub fn lock(&self) {
5764
self.lock_internal(|| {
5865
self.mutex.lock();
5966
true
6067
});
6168
}
6269

6370
#[inline]
64-
fn try_lock(&self) -> bool {
71+
pub fn try_lock(&self) -> bool {
6572
self.lock_internal(|| self.mutex.try_lock())
6673
}
6774

6875
#[inline]
69-
fn unlock(&self) {
76+
pub fn unlock(&self) {
7077
let lock_count = self.lock_count.get() - 1;
7178
if lock_count == 0 {
7279
self.owner.store(0, Ordering::Relaxed);

src/libstd/sys_common/remutex.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use fmt;
1212
use marker;
1313
use ops::Deref;
1414
use sys_common::poison::{self, TryLockError, TryLockResult, LockResult};
15-
use sys::mutex as sys;
15+
use sys_common::parking_lot::remutex::RawReentrantMutex;
1616
use panic::{UnwindSafe, RefUnwindSafe};
1717

1818
/// A re-entrant mutual exclusion
@@ -21,7 +21,7 @@ use panic::{UnwindSafe, RefUnwindSafe};
2121
/// available. The thread which has already locked the mutex can lock it
2222
/// multiple times without blocking, preventing a common source of deadlocks.
2323
pub struct ReentrantMutex<T> {
24-
inner: Box<sys::ReentrantMutex>,
24+
inner: Box<RawReentrantMutex>,
2525
poison: poison::Flag,
2626
data: T,
2727
}
@@ -59,14 +59,10 @@ impl<'a, T> !marker::Send for ReentrantMutexGuard<'a, T> {}
5959
impl<T> ReentrantMutex<T> {
6060
/// Creates a new reentrant mutex in an unlocked state.
6161
pub fn new(t: T) -> ReentrantMutex<T> {
62-
unsafe {
63-
let mut mutex = ReentrantMutex {
64-
inner: box sys::ReentrantMutex::uninitialized(),
65-
poison: poison::Flag::new(),
66-
data: t,
67-
};
68-
mutex.inner.init();
69-
mutex
62+
ReentrantMutex {
63+
inner: box RawReentrantMutex::INIT,
64+
poison: poison::Flag::new(),
65+
data: t,
7066
}
7167
}
7268

@@ -83,7 +79,7 @@ impl<T> ReentrantMutex<T> {
8379
/// this call will return failure if the mutex would otherwise be
8480
/// acquired.
8581
pub fn lock(&self) -> LockResult<ReentrantMutexGuard<T>> {
86-
unsafe { self.inner.lock() }
82+
self.inner.lock();
8783
ReentrantMutexGuard::new(&self)
8884
}
8985

@@ -100,7 +96,7 @@ impl<T> ReentrantMutex<T> {
10096
/// this call will return failure if the mutex would otherwise be
10197
/// acquired.
10298
pub fn try_lock(&self) -> TryLockResult<ReentrantMutexGuard<T>> {
103-
if unsafe { self.inner.try_lock() } {
99+
if self.inner.try_lock() {
104100
Ok(ReentrantMutexGuard::new(&self)?)
105101
} else {
106102
Err(TryLockError::WouldBlock)
@@ -109,12 +105,7 @@ impl<T> ReentrantMutex<T> {
109105
}
110106

111107
impl<T> Drop for ReentrantMutex<T> {
112-
fn drop(&mut self) {
113-
// This is actually safe b/c we know that there is no further usage of
114-
// this mutex (it's up to the user to arrange for a mutex to get
115-
// dropped, that's not our job)
116-
unsafe { self.inner.destroy() }
117-
}
108+
fn drop(&mut self) { }
118109
}
119110

120111
impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
@@ -159,10 +150,8 @@ impl<'mutex, T> Deref for ReentrantMutexGuard<'mutex, T> {
159150
impl<'a, T> Drop for ReentrantMutexGuard<'a, T> {
160151
#[inline]
161152
fn drop(&mut self) {
162-
unsafe {
163-
self.__lock.poison.done(&self.__poison);
164-
self.__lock.inner.unlock();
165-
}
153+
self.__lock.poison.done(&self.__poison);
154+
self.__lock.inner.unlock();
166155
}
167156
}
168157

0 commit comments

Comments
 (0)