Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use MaybeUninit instead of mem::uninitialized for Windows Mutex #56275

Merged
merged 10 commits into from
Dec 2, 2018
18 changes: 9 additions & 9 deletions src/libstd/sys/windows/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! detect recursive locks.

use cell::UnsafeCell;
use mem;
use mem::{self, MaybeUninit};
use sync::atomic::{AtomicUsize, Ordering};
use sys::c;
use sys::compat;
Expand Down Expand Up @@ -157,34 +157,34 @@ fn kind() -> Kind {
return ret;
}

pub struct ReentrantMutex { inner: UnsafeCell<c::CRITICAL_SECTION> }
pub struct ReentrantMutex { inner: MaybeUninit<UnsafeCell<c::CRITICAL_SECTION>> }
RalfJung marked this conversation as resolved.
Show resolved Hide resolved

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

impl ReentrantMutex {
pub unsafe fn uninitialized() -> ReentrantMutex {
mem::uninitialized()
pub fn uninitialized() -> ReentrantMutex {
MaybeUninit::uninitialized()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you forgot to wrap this in ReentrantMutex.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot indeed, thanks!

}

pub unsafe fn init(&mut self) {
c::InitializeCriticalSection(self.inner.get());
c::InitializeCriticalSection(self.inner.get_ref().get());
}

pub unsafe fn lock(&self) {
c::EnterCriticalSection(self.inner.get());
c::EnterCriticalSection(self.inner.get_ref().get());
}

#[inline]
pub unsafe fn try_lock(&self) -> bool {
c::TryEnterCriticalSection(self.inner.get()) != 0
c::TryEnterCriticalSection(self.inner.get_ref().get()) != 0
}

pub unsafe fn unlock(&self) {
c::LeaveCriticalSection(self.inner.get());
c::LeaveCriticalSection(self.inner.get_ref().get());
}

pub unsafe fn destroy(&self) {
c::DeleteCriticalSection(self.inner.get());
c::DeleteCriticalSection(self.inner.get_ref().get());
}
}