Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
o8vm committed Apr 4, 2024
1 parent 7ed42b8 commit 5d7bbba
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/kernel/bio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct Lru {
tail: Option<Weak<Buf>>,
n: usize,
}
unsafe impl Send for Lru {}

#[derive(Debug)]
pub struct Buf {
Expand Down
1 change: 1 addition & 0 deletions src/kernel/buddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct BuddyAllocator {
nsize: usize, // number of entries in self.sizes array
sizes: Option<NonNull<[SzInfo]>>,
}
unsafe impl Send for BuddyAllocator {}

// The allocator has SzInfo for each size k. Each SzInfo has a free
// list, an array alloc to keep track which blocks have been
Expand Down
27 changes: 12 additions & 15 deletions src/kernel/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ impl Cpus {
// # Safety
// interrupts must be disabled.
#[allow(clippy::mut_from_ref)]
pub unsafe fn mycpu(&self) -> &mut Cpu {
pub unsafe fn mycpu(&self) -> *mut Cpu {
let id = Self::cpu_id();
&mut *self.0[id].get()
self.0[id].get()
}

// Return the current proc pointer: Some(Arc<Proc>), or None if none.
pub fn myproc() -> Option<Arc<Proc>> {
let _intr_lock = Self::lock_mycpu("withoutspin");
let c;
unsafe {
c = CPUS.mycpu();
c = &*CPUS.mycpu();
}
c.proc.clone()
}
Expand All @@ -84,7 +84,7 @@ impl Cpus {
pub fn lock_mycpu(name: &str) -> IntrLock {
let old = intr_get();
intr_off();
unsafe { CPUS.mycpu().locked(old, name) }
unsafe { (*CPUS.mycpu()).locked(old, name) }
}
}

Expand All @@ -103,14 +103,11 @@ impl Cpu {

// if all `IntrLock`'s are dropped, interrupts may recover
// to previous state.
// # Safety:
// interrupts must be disabled
unsafe fn locked(&mut self, old: bool, name: &str) -> IntrLock {
fn locked(&mut self, old: bool, _name: &str) -> IntrLock {
if self.noff == 0 {
self.intena = old;
}
self.noff += 1;
self.nest[(self.noff - 1) as usize] = &*(name as *const _);
IntrLock
}

Expand All @@ -132,7 +129,7 @@ pub struct IntrLock;

impl Drop for IntrLock {
fn drop(&mut self) {
unsafe { CPUS.mycpu().unlock() }
unsafe { (&mut *CPUS.mycpu()).unlock() }
}
}

Expand Down Expand Up @@ -675,13 +672,13 @@ pub fn scheduler() -> ! {
// to release its lock and then reacquire it
// before jumping back to us.
inner.state = ProcState::RUNNING;
c.proc.replace(Arc::clone(p));
unsafe {
swtch(&mut c.context, &p.data().context);
(*c).proc.replace(Arc::clone(p));
swtch(&mut (*c).context, &p.data().context);
// Process is done running for now.
// It should have changed its p->state before coming back.
(*c).proc.take();
}
// Process is done running for now.
// It should have changed its p->state before coming back.
c.proc.take();
}
}
}
Expand All @@ -693,7 +690,7 @@ pub fn scheduler() -> ! {
// kernel thread, not this CPU.
fn sched<'a>(guard: MutexGuard<'a, ProcInner>, ctx: &mut Context) -> MutexGuard<'a, ProcInner> {
unsafe {
let c = CPUS.mycpu();
let c = &mut *CPUS.mycpu();
assert!(guard.holding(), "sched proc lock");
assert!(
c.noff == 1,
Expand Down
8 changes: 4 additions & 4 deletions src/kernel/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ impl<T> Mutex<T> {
pub unsafe fn force_unlock(&self) {
assert!(self.holding(), "force unlock {}", self.name);
self.locked.store(ptr::null_mut(), Ordering::Release);
CPUS.mycpu().unlock()
(&mut *CPUS.mycpu()).unlock()
}
}

unsafe impl<T> Sync for Mutex<T> {}

unsafe impl<T> Send for Mutex<T> {}
unsafe impl<T: Send> Sync for Mutex<T> {}

impl<'a, T: 'a> MutexGuard<'a, T> {
// Returns a reference to the original 'Mutex' object.
Expand Down Expand Up @@ -116,3 +114,5 @@ impl<'a, T: 'a> DerefMut for MutexGuard<'a, T> {
unsafe { &mut *self.mutex.data.get() }
}
}

unsafe impl<T: Sync> Sync for MutexGuard<'_, T> {}

0 comments on commit 5d7bbba

Please sign in to comment.