Skip to content

Commit 7529bd6

Browse files
committed
std: Remove a double-box in ReentrantMutex
Perform unsafe initialization up front and then only afterward the mutex is in place do we initialize it.
1 parent a031325 commit 7529bd6

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/libstd/sys/common/remutex.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use sys::mutex as sys;
1919

2020
/// A re-entrant mutual exclusion
2121
///
22-
/// This mutex will block *other* threads waiting for the lock to become available. The thread
23-
/// which has already locked the mutex can lock it multiple times without blocking, preventing a
24-
/// common source of deadlocks.
22+
/// This mutex will block *other* threads waiting for the lock to become
23+
/// available. The thread which has already locked the mutex can lock it
24+
/// multiple times without blocking, preventing a common source of deadlocks.
2525
pub struct ReentrantMutex<T> {
2626
inner: Box<sys::ReentrantMutex>,
2727
poison: poison::Flag,
@@ -51,10 +51,14 @@ impl<'a, T> !marker::Send for ReentrantMutexGuard<'a, T> {}
5151
impl<T> ReentrantMutex<T> {
5252
/// Creates a new reentrant mutex in an unlocked state.
5353
pub fn new(t: T) -> ReentrantMutex<T> {
54-
ReentrantMutex {
55-
inner: box unsafe { sys::ReentrantMutex::new() },
56-
poison: poison::FLAG_INIT,
57-
data: t,
54+
unsafe {
55+
let mut mutex = ReentrantMutex {
56+
inner: box sys::ReentrantMutex::uninitialized(),
57+
poison: poison::FLAG_INIT,
58+
data: t,
59+
};
60+
mutex.inner.init();
61+
return mutex
5862
}
5963
}
6064

src/libstd/sys/unix/mutex.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,27 @@ impl Mutex {
6969
}
7070
}
7171

72-
// FIXME: remove the box, because box happens twice now, once at the common layer and once here.
73-
// Box is necessary here, because mutex may not change address after it is intialised on some
74-
// platforms. Regular Mutex above handles this by offloading intialisation to the OS on first lock.
75-
// Sadly, as far as reentrant mutexes go, this scheme is not quite portable and we must initialise
76-
// when we create the mutex, in the `new`.
77-
pub struct ReentrantMutex { inner: Box<UnsafeCell<ffi::pthread_mutex_t>> }
72+
pub struct ReentrantMutex { inner: UnsafeCell<ffi::pthread_mutex_t> }
7873

7974
unsafe impl Send for ReentrantMutex {}
8075
unsafe impl Sync for ReentrantMutex {}
8176

8277
impl ReentrantMutex {
83-
pub unsafe fn new() -> ReentrantMutex {
84-
let mutex = ReentrantMutex { inner: box mem::uninitialized() };
78+
pub unsafe fn uninitialized() -> ReentrantMutex {
79+
ReentrantMutex { inner: mem::uninitialized() }
80+
}
81+
82+
pub unsafe fn init(&mut self) {
8583
let mut attr: ffi::pthread_mutexattr_t = mem::uninitialized();
8684
let result = ffi::pthread_mutexattr_init(&mut attr as *mut _);
8785
debug_assert_eq!(result, 0);
8886
let result = ffi::pthread_mutexattr_settype(&mut attr as *mut _,
8987
ffi::PTHREAD_MUTEX_RECURSIVE);
9088
debug_assert_eq!(result, 0);
91-
let result = ffi::pthread_mutex_init(mutex.inner.get(), &attr as *const _);
89+
let result = ffi::pthread_mutex_init(self.inner.get(), &attr as *const _);
9290
debug_assert_eq!(result, 0);
9391
let result = ffi::pthread_mutexattr_destroy(&mut attr as *mut _);
9492
debug_assert_eq!(result, 0);
95-
mutex
9693
}
9794

9895
pub unsafe fn lock(&self) {

src/libstd/sys/windows/mutex.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ impl Mutex {
5959
}
6060
}
6161

62-
pub struct ReentrantMutex { inner: Box<UnsafeCell<ffi::CRITICAL_SECTION>> }
62+
pub struct ReentrantMutex { inner: UnsafeCell<ffi::CRITICAL_SECTION> }
6363

6464
unsafe impl Send for ReentrantMutex {}
6565
unsafe impl Sync for ReentrantMutex {}
6666

6767
impl ReentrantMutex {
68-
pub unsafe fn new() -> ReentrantMutex {
69-
let mutex = ReentrantMutex { inner: box mem::uninitialized() };
70-
ffi::InitializeCriticalSection(mutex.inner.get());
71-
mutex
68+
pub unsafe fn uninitialized() -> ReentrantMutex {
69+
mem::uninitialized()
70+
}
71+
72+
pub unsafe fn init(&mut self) {
73+
ffi::InitializeCriticalSection(self.inner.get());
7274
}
7375

7476
pub unsafe fn lock(&self) {

0 commit comments

Comments
 (0)