From ddecffe4b4c6f899936629462c3a0bd0159a937c Mon Sep 17 00:00:00 2001 From: travis1829 Date: Wed, 20 Jan 2021 15:48:19 +0000 Subject: [PATCH] Mark `get_raw` as unsafe. --- kernel-rs/src/proc.rs | 12 +++++++++--- kernel-rs/src/sleepablelock.rs | 6 +++--- kernel-rs/src/spinlock.rs | 10 +++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel-rs/src/proc.rs b/kernel-rs/src/proc.rs index 1846c4d41..5c3f0be43 100644 --- a/kernel-rs/src/proc.rs +++ b/kernel-rs/src/proc.rs @@ -207,9 +207,15 @@ pub enum Procstate { USED, } -pub trait WaitableGuard { +/// Represents lock guards that can be slept in a `WaitChannel`. +pub trait Waitable { /// Returns a reference to the inner `RawSpinlock`. - fn get_raw(&self) -> &RawSpinlock; + /// + /// # Safety + /// + /// You should manually prove the correctness when directly accessing + /// the inner `RawSpinlock` instead of using the lock's API. + unsafe fn get_raw(&self) -> &RawSpinlock; } pub struct WaitChannel { @@ -229,7 +235,7 @@ impl WaitChannel { /// # Safety /// /// Make sure `lk` is the only lock we currently hold. - pub unsafe fn sleep(&self, lk: &mut T) { + pub unsafe fn sleep(&self, lk: &mut T) { let p = &*myproc(); // Must acquire p->lock in order to diff --git a/kernel-rs/src/sleepablelock.rs b/kernel-rs/src/sleepablelock.rs index 45cbc9403..6397f6216 100644 --- a/kernel-rs/src/sleepablelock.rs +++ b/kernel-rs/src/sleepablelock.rs @@ -1,5 +1,5 @@ //! Sleepable locks -use crate::proc::{WaitChannel, WaitableGuard}; +use crate::proc::{WaitChannel, Waitable}; use crate::spinlock::RawSpinlock; use core::cell::UnsafeCell; use core::marker::PhantomData; @@ -71,8 +71,8 @@ impl SleepablelockGuard<'_, T> { } } -impl WaitableGuard for SleepablelockGuard<'_, T> { - fn get_raw(&self) -> &RawSpinlock { +impl Waitable for SleepablelockGuard<'_, T> { + unsafe fn get_raw(&self) -> &RawSpinlock { &self.lock.lock } } diff --git a/kernel-rs/src/spinlock.rs b/kernel-rs/src/spinlock.rs index f970754fa..7df42130e 100644 --- a/kernel-rs/src/spinlock.rs +++ b/kernel-rs/src/spinlock.rs @@ -1,6 +1,6 @@ use crate::{ kernel::kernel, - proc::{Cpu, WaitableGuard}, + proc::{Cpu, Waitable}, riscv::{intr_get, intr_off, intr_on}, }; use core::cell::UnsafeCell; @@ -198,8 +198,8 @@ impl SpinlockGuard<'_, T> { } } -impl WaitableGuard for SpinlockGuard<'_, T> { - fn get_raw(&self) -> &RawSpinlock { +impl Waitable for SpinlockGuard<'_, T> { + unsafe fn get_raw(&self) -> &RawSpinlock { &self.lock.lock } } @@ -264,8 +264,8 @@ impl SpinlockProtected { } } -impl WaitableGuard for SpinlockProtectedGuard<'_> { - fn get_raw(&self) -> &RawSpinlock { +impl Waitable for SpinlockProtectedGuard<'_> { + unsafe fn get_raw(&self) -> &RawSpinlock { &self.lock } }