Make mutex new constant and reduce unsafe block size

This commit is contained in:
Gwilym Kuiper 2021-08-16 21:02:03 +01:00
parent 1b40fe2b03
commit a500c9dbb1

View file

@ -347,7 +347,7 @@ impl<T> Mutex<T> {
} }
} }
pub fn new(val: T) -> Self { pub const fn new(val: T) -> Self {
Mutex { Mutex {
internal: UnsafeCell::new(val), internal: UnsafeCell::new(val),
state: UnsafeCell::new(MutexState::Unlocked), state: UnsafeCell::new(MutexState::Unlocked),
@ -362,17 +362,17 @@ pub struct MutexRef<'a, T> {
impl<'a, T> Drop for MutexRef<'a, T> { impl<'a, T> Drop for MutexRef<'a, T> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { let state = unsafe { &mut *self.state.get() };
let state = &mut *self.state.get();
let prev_state = *state; let prev_state = *state;
*state = MutexState::Unlocked; *state = MutexState::Unlocked;
match prev_state { match prev_state {
MutexState::Locked(b) => INTERRUPTS_ENABLED.set(b as u16), MutexState::Locked(b) => INTERRUPTS_ENABLED.set(b as u16),
MutexState::Unlocked => {} MutexState::Unlocked => {}
} }
} }
} }
}
impl<'a, T> Deref for MutexRef<'a, T> { impl<'a, T> Deref for MutexRef<'a, T> {
type Target = T; type Target = T;