put the MaybeUninit inside the UnsafeCell

This commit is contained in:
Ralf Jung 2018-11-28 09:29:56 +01:00
parent a4f12344c6
commit 12d90aa949
2 changed files with 7 additions and 13 deletions

View File

@ -1106,9 +1106,6 @@ impl<T> MaybeUninit<T> {
///
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
/// state, otherwise this will immediately cause undefined behavior.
// FIXME: We currently rely on the above being incorrect, i.e., we have references
// to uninitialized data (e.g. in `libstd/sys/windows/mutex.rs`). We should make
// a final decision about the rules before stabilization.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub unsafe fn get_ref(&self) -> &T {

View File

@ -157,37 +157,34 @@ fn kind() -> Kind {
return ret;
}
pub struct ReentrantMutex { inner: MaybeUninit<UnsafeCell<c::CRITICAL_SECTION>> }
pub struct ReentrantMutex { inner: UnsafeCell<MaybeUninit<c::CRITICAL_SECTION>> }
unsafe impl Send for ReentrantMutex {}
unsafe impl Sync for ReentrantMutex {}
impl ReentrantMutex {
pub fn uninitialized() -> ReentrantMutex {
ReentrantMutex { inner: MaybeUninit::uninitialized() }
ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninitialized()) }
}
pub unsafe fn init(&mut self) {
// FIXME: Technically, this is calling `get_ref` on an uninitialized
// `MaybeUninit`. Revisit this once we decided whether that is valid
// or not.
c::InitializeCriticalSection(self.inner.get_ref().get());
c::InitializeCriticalSection(self.inner.get().as_mut_ptr());
}
pub unsafe fn lock(&self) {
c::EnterCriticalSection(self.inner.get_ref().get());
c::EnterCriticalSection(self.inner.get().get_ref());
}
#[inline]
pub unsafe fn try_lock(&self) -> bool {
c::TryEnterCriticalSection(self.inner.get_ref().get()) != 0
c::TryEnterCriticalSection(self.inner.get().get_ref()) != 0
}
pub unsafe fn unlock(&self) {
c::LeaveCriticalSection(self.inner.get_ref().get());
c::LeaveCriticalSection(self.inner.get().get_ref());
}
pub unsafe fn destroy(&self) {
c::DeleteCriticalSection(self.inner.get_ref().get());
c::DeleteCriticalSection(self.inner.get().get_ref());
}
}