Skip to content

Commit

Permalink
Avoid creating &muts in Windows ReentrantMutex.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Sep 16, 2020
1 parent 3fadc60 commit 0bb96e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@
#![feature(try_reserve)]
#![feature(unboxed_closures)]
#![feature(unsafe_block_in_unsafe_fn)]
#![feature(unsafe_cell_raw_get)]
#![feature(untagged_unions)]
#![feature(unwind_attributes)]
#![feature(vec_into_raw_parts)]
Expand Down
14 changes: 7 additions & 7 deletions library/std/src/sys/windows/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,35 @@ fn kind() -> Kind {
}

pub struct ReentrantMutex {
inner: UnsafeCell<MaybeUninit<c::CRITICAL_SECTION>>,
inner: MaybeUninit<UnsafeCell<c::CRITICAL_SECTION>>,
}

unsafe impl Send for ReentrantMutex {}
unsafe impl Sync for ReentrantMutex {}

impl ReentrantMutex {
pub const fn uninitialized() -> ReentrantMutex {
ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninit()) }
ReentrantMutex { inner: MaybeUninit::uninit() }
}

pub unsafe fn init(&self) {
c::InitializeCriticalSection((&mut *self.inner.get()).as_mut_ptr());
c::InitializeCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
}

pub unsafe fn lock(&self) {
c::EnterCriticalSection((&mut *self.inner.get()).as_mut_ptr());
c::EnterCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
}

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

pub unsafe fn unlock(&self) {
c::LeaveCriticalSection((&mut *self.inner.get()).as_mut_ptr());
c::LeaveCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
}

pub unsafe fn destroy(&self) {
c::DeleteCriticalSection((&mut *self.inner.get()).as_mut_ptr());
c::DeleteCriticalSection(UnsafeCell::raw_get(self.inner.as_ptr()));
}
}

0 comments on commit 0bb96e7

Please sign in to comment.