Skip to content

Commit

Permalink
Use mutex to protect disk accessment
Browse files Browse the repository at this point in the history
  • Loading branch information
yodalee committed May 9, 2023
1 parent 2252f8b commit f3e706f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use crate::virtio::header::VirtioHeader;
use crate::virtio::block::VirtioBlock;
use crate::memorylayout::VIRTIO0;

static mut DISK: Option<VirtioBlock> = None;
use spin::Mutex;

static mut DISK: Mutex<Option<VirtioBlock>> = Mutex::new(None);

pub fn init_disk() {
let header = unsafe { &mut *(VIRTIO0 as *mut VirtioHeader) };
let block = VirtioBlock::new(header);
match block {
Ok(block) => unsafe {
DISK = Some(block);
let mut disk = DISK.lock();
*disk = Some(block);
},
Err(_err) => panic!("Error: Disk initialization"),
}
}

pub fn get_disk() -> &'static mut VirtioBlock {
unsafe {
DISK.as_mut().unwrap()
}
pub fn read_disk() {
let disk = unsafe { DISK.lock().as_mut().unwrap() };
}

0 comments on commit f3e706f

Please sign in to comment.