From 3968c4c37697cb4453b2af972eed7e2cff2a955b Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Tue, 20 Dec 2022 20:55:49 +0100 Subject: [PATCH] async goes boom Signed-off-by: Julien CLEMENT --- src/drivers/atapi/mod.rs | 15 +++++++++------ src/fs/iso/mod.rs | 5 +++-- src/utils/mutex.rs | 12 +++++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/drivers/atapi/mod.rs b/src/drivers/atapi/mod.rs index cf31e3e..80c4b32 100644 --- a/src/drivers/atapi/mod.rs +++ b/src/drivers/atapi/mod.rs @@ -9,6 +9,7 @@ use core::convert::TryInto; use lazy_static::lazy_static; use spin::Mutex; +use crate::utils::AsyncMutex; use x86_64::instructions::port::Port; const CD_SECTOR_SIZE: usize = 2048; @@ -58,15 +59,16 @@ static ATAPI_SIG: [u8; 4] = [ ]; lazy_static! { - pub static ref DRIVE: Mutex> = { - Mutex::new(ATABus::discover_atapi_drive()) + pub static ref DRIVE: AsyncMutex> = { + AsyncMutex::new(ATABus::discover_atapi_drive()) }; } -pub fn init() { +pub async fn init() { println!("Detecting drives"); - match DRIVE.lock().as_ref() { + let guard = DRIVE.lock().await; + match guard.as_ref() { None => println!("No drive detected :("), Some(drive) => { let drive_type = match drive.current_drive { @@ -322,6 +324,7 @@ impl ATABus { pub async fn print_block() { - DRIVE.lock().as_mut().unwrap().read_block(500).await; - serial_println!("{:x?}", DRIVE.lock().as_mut().unwrap().block); + let mut guard = DRIVE.lock().await; + guard.as_mut().unwrap().read_block(500).await; + serial_println!("{:x?}", guard.as_mut().unwrap().block); } \ No newline at end of file diff --git a/src/fs/iso/mod.rs b/src/fs/iso/mod.rs index 19f997a..3b24bd3 100644 --- a/src/fs/iso/mod.rs +++ b/src/fs/iso/mod.rs @@ -6,9 +6,10 @@ use crate::utils::unserialize; use iso9660::{IsoPrimVolDesc}; pub async fn init_prim_vol_desc() { - let desc_block = DRIVE + let mut guard = DRIVE .lock() - .as_mut() + .await; + let desc_block = guard.as_mut() .unwrap() .read_block(iso9660::ISO_PRIM_VOLDESC_BLOCK) .await; diff --git a/src/utils/mutex.rs b/src/utils/mutex.rs index 6250f71..6802dea 100644 --- a/src/utils/mutex.rs +++ b/src/utils/mutex.rs @@ -4,14 +4,14 @@ use core::cell::UnsafeCell; use core::ops::{Deref, DerefMut, Drop}; use core::sync::atomic::{AtomicBool, Ordering}; use core::task::{Context, Poll}; -use alloc::rc::Rc; +use alloc::sync::Arc; use futures_util::task::AtomicWaker; #[derive(Clone)] struct Lock { - lock: Rc, - waker: Rc, + lock: Arc, + waker: Arc, } pub struct AsyncMutex { @@ -29,8 +29,8 @@ where impl Lock { fn new() -> Self { Lock { - lock: Rc::new(AtomicBool::new(false)), - waker: Rc::new(AtomicWaker::new()), + lock: Arc::new(AtomicBool::new(false)), + waker: Arc::new(AtomicWaker::new()), } } @@ -86,6 +86,8 @@ impl AsyncMutex { } } +unsafe impl Send for AsyncMutex {} +unsafe impl Sync for AsyncMutex {} impl Drop for AsyncMutexGuard<'_, T> { fn drop(&mut self) {