Skip to content

Commit

Permalink
Change Guard trait -> WaitableGuard trait
Browse files Browse the repository at this point in the history
  • Loading branch information
travis1829 committed Jan 20, 2021
1 parent 944b459 commit 3b9bcc8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
10 changes: 8 additions & 2 deletions kernel-rs/src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
riscv::{intr_get, intr_on, r_tp, PGSIZE},
some_or,
spinlock::{
pop_off, push_off, Guard, RawSpinlock, Spinlock, SpinlockProtected, SpinlockProtectedGuard,
pop_off, push_off, RawSpinlock, Spinlock, SpinlockProtected, SpinlockProtectedGuard,
},
string::safestrcpy,
trap::usertrapret,
Expand Down Expand Up @@ -208,6 +208,11 @@ pub enum Procstate {
USED,
}

pub trait WaitableGuard {
/// Returns a reference to the inner `RawSpinlock`.
fn get_raw(&self) -> &RawSpinlock;
}

pub struct WaitChannel {
/// Required to make this type non-zero-sized. If it were zero-sized, multiple wait channels may
/// have the same address, spuriously waking up more threads.
Expand All @@ -221,10 +226,11 @@ impl WaitChannel {

/// Atomically release lock and sleep on waitchannel.
/// Reacquires lock when awakened.
///
/// # Safety
///
/// Make sure `lk` is the only lock we currently hold.
pub unsafe fn sleep(&self, lk: &mut dyn Guard) {
pub unsafe fn sleep<T: WaitableGuard>(&self, lk: &mut T) {
let p = &*myproc();

// Must acquire p->lock in order to
Expand Down
6 changes: 3 additions & 3 deletions kernel-rs/src/sleepablelock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Sleepable locks
use crate::proc::WaitChannel;
use crate::spinlock::{Guard, RawSpinlock};
use crate::proc::{WaitChannel, WaitableGuard};
use crate::spinlock::RawSpinlock;
use core::cell::UnsafeCell;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<T> SleepablelockGuard<'_, T> {
}
}

impl<T> Guard for SleepablelockGuard<'_, T> {
impl<T> WaitableGuard for SleepablelockGuard<'_, T> {
fn get_raw(&self) -> &RawSpinlock {
&self.lock.lock
}
Expand Down
11 changes: 3 additions & 8 deletions kernel-rs/src/spinlock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
kernel::kernel,
proc::Cpu,
proc::{Cpu, WaitableGuard},
riscv::{intr_get, intr_off, intr_on},
};
use core::cell::UnsafeCell;
Expand Down Expand Up @@ -101,11 +101,6 @@ impl RawSpinlock {
}
}

pub trait Guard {
/// Returns a reference to the inner `RawSpinlock`.
fn get_raw(&self) -> &RawSpinlock;
}

pub struct SpinlockGuard<'s, T> {
lock: &'s Spinlock<T>,
_marker: PhantomData<*const ()>,
Expand Down Expand Up @@ -203,7 +198,7 @@ impl<T> SpinlockGuard<'_, T> {
}
}

impl<T> Guard for SpinlockGuard<'_, T> {
impl<T> WaitableGuard for SpinlockGuard<'_, T> {
fn get_raw(&self) -> &RawSpinlock {
&self.lock.lock
}
Expand Down Expand Up @@ -269,7 +264,7 @@ impl<T> SpinlockProtected<T> {
}
}

impl Guard for SpinlockProtectedGuard<'_> {
impl WaitableGuard for SpinlockProtectedGuard<'_> {
fn get_raw(&self) -> &RawSpinlock {
&self.lock
}
Expand Down

0 comments on commit 3b9bcc8

Please sign in to comment.