rename sleep to sleep_lock

This commit is contained in:
Garen Tyler 2023-10-21 22:44:06 -06:00
parent 38e44e020b
commit d06d271c9e
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
11 changed files with 193 additions and 77 deletions

View File

@ -13,14 +13,7 @@
//! - Only one process at a time can use a buffer,
//! so do not keep them longer than necessary.
use crate::{
sync::sleeplock::{holdingsleep, acquiresleep, releasesleep},
sync::spinlock::Spinlock,
buf::Buffer,
param::NBUF,
virtio_disk::virtio_disk_rw,
};
use core::ptr::addr_of_mut;
use crate::{buf::Buffer, param::NBUF, sync::spinlock::Spinlock};
pub struct BufferCache {
pub buffers: [Buffer; NBUF],
@ -34,7 +27,6 @@ impl BufferCache {
for buf in &mut self.buffers {
if buf.dev == dev && buf.blockno == blockno {
buf.refcnt += 1;
}
}
}

View File

@ -108,6 +108,8 @@ pub unsafe extern "C" fn consoleread(user_dst: i32, mut dst: u64, mut n: i32) ->
// cons.lock.unlock();
return -1;
}
// let channel = addr_of_mut!(console.read_index).cast();
// console.sleep(channel);
sleep_mutex(addr_of_mut!(console.read_index).cast(), &mut console);
}

View File

@ -0,0 +1,18 @@
use crate::sync::spinlock::Spinlock;
use core::ffi::c_char;
pub const PIPESIZE: usize = 512usize;
#[repr(C)]
pub struct Pipe {
lock: Spinlock,
data: [c_char; PIPESIZE],
/// Number of bytes read.
nread: u32,
/// Number of bytes written.
nwrite: u32,
/// Read fd is still open.
readopen: i32,
/// Write fd is still open.
writeopen: i32,
}

View File

@ -4,7 +4,7 @@ use crate::{
kalloc::kfree,
param::*,
riscv::{self, Pagetable, PTE_W},
sync::spinlock::{pop_off, push_off, Spinlock},
sync::spinlock::Spinlock,
sync::spinmutex::SpinMutexGuard,
};
use core::{
@ -241,8 +241,7 @@ pub unsafe extern "C" fn mycpu() -> *mut Cpu {
pub unsafe extern "C" fn myproc() -> *mut Proc {
let _ = crate::trap::InterruptBlocker::new();
let c = mycpu();
let p = (*c).proc;
p
(*c).proc
}
#[no_mangle]
@ -408,7 +407,7 @@ pub unsafe extern "C" fn sched() {
/// Atomically release lock and sleep on chan.
/// Reacquires lock when awakened.
#[no_mangle]
pub unsafe extern "C" fn sleep(chan: *mut c_void, lock: *mut Spinlock) {
pub unsafe extern "C" fn sleep_lock(chan: *mut c_void, lock: *mut Spinlock) {
let p = myproc();
// Must acquire p->lock in order to
@ -455,6 +454,20 @@ pub unsafe fn sleep_mutex<T>(chan: *mut c_void, mutex: &mut SpinMutexGuard<T>) {
core::mem::forget(guard);
}
// pub unsafe fn sleep(chan: *mut c_void) {
// let p = myproc();
// let _guard = (*p).lock.lock();
//
// // Go to sleep.
// (*p).chan = chan;
// (*p).state = ProcState::Sleeping;
//
// sched();
//
// // Clean up.
// (*p).chan = null_mut();
// }
/// Kill the process with the given pid.
/// The victim won't exit until it tries to return
/// to user space (see usertrap() in trap.c).
@ -486,6 +499,5 @@ pub unsafe extern "C" fn setkilled(p: *mut Proc) {
#[no_mangle]
pub unsafe extern "C" fn killed(p: *mut Proc) -> i32 {
let _guard = (*p).lock.lock();
let k = (*p).killed;
k
(*p).killed
}

View File

@ -1,3 +1,4 @@
pub(crate) mod mutex;
pub mod sleeplock;
pub mod spinlock;
pub mod spinmutex;

View File

@ -0,0 +1,85 @@
// use core::{
// cell::UnsafeCell,
// ops::{Deref, DerefMut, Drop},
// sync::atomic::{AtomicBool, Ordering},
// ffi::c_void,
// ptr::addr_of,
// };
// use crate::proc::{sleep, wakeup, sched, myproc, ProcState};
//
// pub struct Mutex<T> {
// locked: AtomicBool,
// inner: UnsafeCell<T>,
// }
// impl<T> Mutex<T> {
// pub const fn new(value: T) -> Mutex<T> {
// Mutex {
// locked: AtomicBool::new(false),
// inner: UnsafeCell::new(value),
// }
// }
// pub unsafe fn get_inner(&self) -> *mut T {
// self.inner.get()
// }
// /// Spin until the mutex is unlocked, acquiring afterwards.
// pub fn spin_lock(&self) -> MutexGuard<'_, T> {
// while self.locked.swap(true, Ordering::Acquire) {
// core::hint::spin_loop();
// }
//
// MutexGuard { mutex: self }
// }
// /// Sleep until the mutex is unlocked, acquiring afterwards.
// pub fn sleep_lock(&self) -> MutexGuard<'_, T> {
// while self.locked.swap(true, Ordering::Acquire) {
// unsafe {
// sleep(addr_of!(*self).cast_mut().cast());
// }
// }
//
// MutexGuard { mutex: self }
// }
// pub unsafe fn unlock(&self) {
// self.locked.store(false, Ordering::Release);
// wakeup(addr_of!(*self).cast_mut().cast());
// }
// }
// unsafe impl<T> Sync for Mutex<T> where T: Send {}
//
// pub struct MutexGuard<'m, T> {
// pub mutex: &'m Mutex<T>,
// }
// impl<'m, T> MutexGuard<'m, T> {
// pub unsafe fn sleep(&mut self, channel: *mut c_void){
// let p = myproc();
// let _guard = (*p).lock.lock();
// self.mutex.unlock();
//
// // Go to sleep.
// (*p).chan = channel;
// (*p).state = ProcState::Sleeping;
// sched();
//
// // Clean up.
// let guard = self.mutex.spin_lock();
// core::mem::forget(guard);
// }
// }
// impl<'m, T> Deref for MutexGuard<'m, T> {
// type Target = T;
//
// fn deref(&self) -> &Self::Target {
// unsafe { &*self.mutex.get_inner() }
// }
// }
// impl<'m, T> DerefMut for MutexGuard<'m, T> {
// fn deref_mut(&mut self) -> &mut Self::Target {
// unsafe { &mut *self.mutex.get_inner() }
// }
// }
// impl<'m, T> Drop for MutexGuard<'m, T> {
// fn drop(&mut self) {
// unsafe { self.mutex.unlock() }
// }
// }
//

View File

@ -1,8 +1,11 @@
use crate::{
proc::{myproc, sleep, wakeup},
sync::spinlock::{self, Spinlock},
proc::{myproc, sleep_lock, wakeup},
sync::spinlock::Spinlock,
};
use core::{
ffi::c_char,
ptr::{addr_of, null_mut},
};
use core::{ffi::c_char, ptr::{addr_of, null_mut}};
#[repr(C)]
pub struct Sleeplock {
@ -33,10 +36,14 @@ impl Sleeplock {
pub fn held_by_current_proc(&self) -> bool {
self.locked > 0 && self.pid == unsafe { (*myproc()).pid }
}
#[allow(clippy::while_immutable_condition)]
pub unsafe fn lock_unguarded(&self) {
let _guard = self.inner.lock();
while self.locked > 0 {
sleep(addr_of!(*self).cast_mut().cast(), addr_of!(self.inner).cast_mut().cast());
sleep_lock(
addr_of!(*self).cast_mut().cast(),
addr_of!(self.inner).cast_mut().cast(),
);
}
let this: &mut Self = &mut *addr_of!(*self).cast_mut();
this.locked = 1;

View File

@ -4,7 +4,7 @@ use crate::{
};
use core::{
ffi::c_char,
ptr::{null_mut, addr_of},
ptr::{addr_of, null_mut},
sync::atomic::{AtomicBool, Ordering},
};

View File

@ -1,5 +1,5 @@
use crate::{
proc::{exit, fork, growproc, kill, killed, myproc, sleep, wait},
proc::{exit, fork, growproc, kill, killed, myproc, sleep_lock, wait},
syscall::{argaddr, argint},
};
use core::ptr::addr_of_mut;
@ -53,7 +53,7 @@ pub unsafe extern "C" fn sys_sleep() -> u64 {
crate::trap::tickslock.unlock();
return -1i64 as u64;
}
sleep(
sleep_lock(
addr_of_mut!(crate::trap::ticks).cast(),
addr_of_mut!(crate::trap::tickslock).cast(),
)

View File

@ -3,7 +3,7 @@
use crate::{
console::consoleintr,
proc::{sleep, sleep_mutex, wakeup},
proc::{sleep_lock, wakeup},
riscv::memlayout::UART0,
sync::spinlock::Spinlock,
sync::spinmutex::SpinMutex,
@ -171,7 +171,7 @@ pub(crate) unsafe fn uartputc(c: u8) {
while uart_tx_w == uart_tx_r + UART_TX_BUF_SIZE {
// Buffer is full.
// Wait for uartstart() to open up space in the buffer.
sleep(
sleep_lock(
addr_of_mut!(uart_tx_r).cast(),
addr_of_mut!(uart_tx_lock).cast(),
);

View File

@ -6,7 +6,7 @@
//! The virtio spec: https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.pdf
//! qemu ... -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
use crate::{sync::spinlock::Spinlock, buf::Buffer};
use crate::{buf::Buffer, sync::spinlock::Spinlock};
use core::ffi::c_char;
// Virtio MMIO control registers, mapped starting at 0x10001000
@ -177,7 +177,6 @@ pub struct Disk {
/// Indexed by first descriptor index of chain.
pub info: [DiskInfo; NUM_DESCRIPTORS],
/// Disk command headers.
/// One-for-one with descriptors, for convenience.
pub ops: [VirtioBlockRequest; NUM_DESCRIPTORS],