Skip to content

Commit 0bb96e7

Browse files
committed
Avoid creating &muts in Windows ReentrantMutex.
1 parent 3fadc60 commit 0bb96e7

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
#![feature(try_reserve)]
316316
#![feature(unboxed_closures)]
317317
#![feature(unsafe_block_in_unsafe_fn)]
318+
#![feature(unsafe_cell_raw_get)]
318319
#![feature(untagged_unions)]
319320
#![feature(unwind_attributes)]
320321
#![feature(vec_into_raw_parts)]

library/std/src/sys/windows/mutex.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -148,35 +148,35 @@ fn kind() -> Kind {
148148
}
149149

150150
pub struct ReentrantMutex {
151-
inner: UnsafeCell<MaybeUninit<c::CRITICAL_SECTION>>,
151+
inner: MaybeUninit<UnsafeCell<c::CRITICAL_SECTION>>,
152152
}
153153

154154
unsafe impl Send for ReentrantMutex {}
155155
unsafe impl Sync for ReentrantMutex {}
156156

157157
impl ReentrantMutex {
158158
pub const fn uninitialized() -> ReentrantMutex {
159-
ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninit()) }
159+
ReentrantMutex { inner: MaybeUninit::uninit() }
160160
}
161161

162162
pub unsafe fn init(&self) {
163-
c::InitializeCriticalSection((&mut *self.inner.get()).as_mut_ptr());
163+
c::InitializeCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
164164
}
165165

166166
pub unsafe fn lock(&self) {
167-
c::EnterCriticalSection((&mut *self.inner.get()).as_mut_ptr());
167+
c::EnterCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
168168
}
169169

170170
#[inline]
171171
pub unsafe fn try_lock(&self) -> bool {
172-
c::TryEnterCriticalSection((&mut *self.inner.get()).as_mut_ptr()) != 0
172+
c::TryEnterCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr())) != 0
173173
}
174174

175175
pub unsafe fn unlock(&self) {
176-
c::LeaveCriticalSection((&mut *self.inner.get()).as_mut_ptr());
176+
c::LeaveCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
177177
}
178178

179179
pub unsafe fn destroy(&self) {
180-
c::DeleteCriticalSection((&mut *self.inner.get()).as_mut_ptr());
180+
c::DeleteCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
181181
}
182182
}

0 commit comments

Comments
 (0)