From 6d101c5b6d600bb11fdbc2a24f21a5bd3dfab6e9 Mon Sep 17 00:00:00 2001 From: Chuandong Li Date: Sat, 19 Aug 2023 17:26:02 +0000 Subject: [PATCH] Use Mutex and RwLock in jinux-frame for jinux-std --- framework/jinux-frame/src/arch/x86/iommu/mod.rs | 3 ++- framework/jinux-frame/src/arch/x86/irq.rs | 3 ++- .../jinux-frame/src/arch/x86/kernel/acpi/mod.rs | 3 ++- .../jinux-frame/src/arch/x86/kernel/apic/ioapic.rs | 3 ++- .../jinux-frame/src/arch/x86/kernel/apic/mod.rs | 10 ++++++---- .../jinux-frame/src/arch/x86/kernel/apic/xapic.rs | 3 ++- framework/jinux-frame/src/arch/x86/kernel/pic.rs | 2 +- framework/jinux-frame/src/arch/x86/mm/mod.rs | 2 +- framework/jinux-frame/src/bus/pci/mod.rs | 2 +- framework/jinux-frame/src/sync/mutex.rs | 2 +- framework/jinux-frame/src/sync/rwlock.rs | 12 ++++++++++++ framework/jinux-frame/src/sync/wait.rs | 6 +++++- framework/jinux-frame/src/task/processor.rs | 2 +- framework/jinux-frame/src/timer.rs | 2 +- framework/jinux-frame/src/trap/irq.rs | 2 +- framework/jinux-frame/src/vm/space.rs | 2 +- services/comps/block/src/virtio.rs | 2 +- services/comps/input/src/lib.rs | 3 ++- services/comps/input/src/virtio.rs | 2 +- services/comps/pci/src/lib.rs | 3 ++- services/comps/time/src/rtc.rs | 3 +-- services/comps/virtio/src/device/input/device.rs | 2 +- services/comps/virtio/src/lib.rs | 3 ++- services/libs/jinux-std/src/fs/ramfs/fs.rs | 2 +- services/libs/jinux-std/src/prelude.rs | 5 +---- services/libs/jinux-std/src/vm/vmar/vm_mapping.rs | 2 +- 26 files changed, 54 insertions(+), 32 deletions(-) diff --git a/framework/jinux-frame/src/arch/x86/iommu/mod.rs b/framework/jinux-frame/src/arch/x86/iommu/mod.rs index 54b0631c2..a8ebdb63d 100644 --- a/framework/jinux-frame/src/arch/x86/iommu/mod.rs +++ b/framework/jinux-frame/src/arch/x86/iommu/mod.rs @@ -3,8 +3,9 @@ mod fault; mod remapping; mod second_stage; +use crate::sync::Mutex; use log::info; -use spin::{Mutex, Once}; +use spin::Once; use crate::{ arch::iommu::{context_table::RootTable, second_stage::PageTableEntry}, diff --git a/framework/jinux-frame/src/arch/x86/irq.rs b/framework/jinux-frame/src/arch/x86/irq.rs index d4e94f1cc..82af30394 100644 --- a/framework/jinux-frame/src/arch/x86/irq.rs +++ b/framework/jinux-frame/src/arch/x86/irq.rs @@ -1,5 +1,6 @@ +use crate::sync::Mutex; use alloc::vec::Vec; -use spin::{Mutex, Once}; +use spin::Once; use crate::{trap::IrqLine, util::recycle_allocator::RecycleAllocator}; diff --git a/framework/jinux-frame/src/arch/x86/kernel/acpi/mod.rs b/framework/jinux-frame/src/arch/x86/kernel/acpi/mod.rs index 564212359..7d04d5e85 100644 --- a/framework/jinux-frame/src/arch/x86/kernel/acpi/mod.rs +++ b/framework/jinux-frame/src/arch/x86/kernel/acpi/mod.rs @@ -7,11 +7,12 @@ use core::{ }; use crate::boot::{self, BootloaderAcpiArg}; +use crate::sync::Mutex; use crate::vm::paddr_to_vaddr; use acpi::{sdt::SdtHeader, AcpiHandler, AcpiTable, AcpiTables}; use alloc::borrow::ToOwned; use log::info; -use spin::{Mutex, Once}; +use spin::Once; /// RSDP information, key is the signature, value is the virtual address of the signature pub static ACPI_TABLES: Once>> = Once::new(); diff --git a/framework/jinux-frame/src/arch/x86/kernel/apic/ioapic.rs b/framework/jinux-frame/src/arch/x86/kernel/apic/ioapic.rs index c7222a762..454815469 100644 --- a/framework/jinux-frame/src/arch/x86/kernel/apic/ioapic.rs +++ b/framework/jinux-frame/src/arch/x86/kernel/apic/ioapic.rs @@ -1,6 +1,7 @@ +use crate::sync::Mutex; use acpi::PlatformInfo; use log::info; -use spin::{Mutex, Once}; +use spin::Once; use x86::apic::ioapic::IoApic; use crate::arch::x86::kernel::acpi::ACPI_TABLES; diff --git a/framework/jinux-frame/src/arch/x86/kernel/apic/mod.rs b/framework/jinux-frame/src/arch/x86/kernel/apic/mod.rs index 8455db43f..be4ecf441 100644 --- a/framework/jinux-frame/src/arch/x86/kernel/apic/mod.rs +++ b/framework/jinux-frame/src/arch/x86/kernel/apic/mod.rs @@ -1,12 +1,14 @@ +use crate::sync::Mutex; +use alloc::boxed::Box; use alloc::sync::Arc; use log::info; -use spin::{Mutex, Once}; +use spin::Once; pub mod ioapic; pub mod x2apic; pub mod xapic; -pub static APIC_INSTANCE: Once>> = Once::new(); +pub static APIC_INSTANCE: Once>>> = Once::new(); pub trait Apic: ApicTimer + Sync + Send { fn id(&self) -> u32; @@ -66,7 +68,7 @@ pub fn init() -> Result<(), ApicInitError> { version & 0xff, (version >> 16) & 0xff ); - APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(x2apic))); + APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(Box::new(x2apic)))); Ok(()) } else if let Some(mut xapic) = xapic::XApic::new() { xapic.enable(); @@ -77,7 +79,7 @@ pub fn init() -> Result<(), ApicInitError> { version & 0xff, (version >> 16) & 0xff ); - APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(xapic))); + APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(Box::new(xapic)))); Ok(()) } else { log::warn!("Not found x2APIC or xAPIC"); diff --git a/framework/jinux-frame/src/arch/x86/kernel/apic/xapic.rs b/framework/jinux-frame/src/arch/x86/kernel/apic/xapic.rs index 58aee5370..76ae1d1ee 100644 --- a/framework/jinux-frame/src/arch/x86/kernel/apic/xapic.rs +++ b/framework/jinux-frame/src/arch/x86/kernel/apic/xapic.rs @@ -1,5 +1,6 @@ +use crate::sync::Mutex; use crate::vm; -use spin::{Mutex, Once}; +use spin::Once; use x86::apic::xapic; use super::ApicTimer; diff --git a/framework/jinux-frame/src/arch/x86/kernel/pic.rs b/framework/jinux-frame/src/arch/x86/kernel/pic.rs index dff5af7b9..5cc17952b 100644 --- a/framework/jinux-frame/src/arch/x86/kernel/pic.rs +++ b/framework/jinux-frame/src/arch/x86/kernel/pic.rs @@ -14,10 +14,10 @@ const IRQ_OFFSET: u8 = 0x20; const TIMER_IRQ_NUM: u8 = 32; +use crate::sync::Mutex; use alloc::vec::Vec; use lazy_static::lazy_static; use log::info; -use spin::Mutex; lazy_static! { /// store the irq, although we have APIC for manage interrupts diff --git a/framework/jinux-frame/src/arch/x86/mm/mod.rs b/framework/jinux-frame/src/arch/x86/mm/mod.rs index f12cd0fd2..eb5b96243 100644 --- a/framework/jinux-frame/src/arch/x86/mm/mod.rs +++ b/framework/jinux-frame/src/arch/x86/mm/mod.rs @@ -1,6 +1,6 @@ +use crate::sync::Mutex; use alloc::{collections::BTreeMap, fmt}; use pod::Pod; -use spin::Mutex; use x86_64::{instructions::tlb, structures::paging::PhysFrame, VirtAddr}; use crate::{ diff --git a/framework/jinux-frame/src/bus/pci/mod.rs b/framework/jinux-frame/src/bus/pci/mod.rs index f733fb6c2..f81f3be20 100644 --- a/framework/jinux-frame/src/bus/pci/mod.rs +++ b/framework/jinux-frame/src/bus/pci/mod.rs @@ -54,8 +54,8 @@ mod cfg_space; mod common_device; mod device_info; +use crate::sync::Mutex; pub use device_info::{PciDeviceId, PciDeviceLocation}; -use spin::Mutex; use self::{bus::PciBus, common_device::PciCommonDevice}; diff --git a/framework/jinux-frame/src/sync/mutex.rs b/framework/jinux-frame/src/sync/mutex.rs index 5e4217b71..f76c35663 100644 --- a/framework/jinux-frame/src/sync/mutex.rs +++ b/framework/jinux-frame/src/sync/mutex.rs @@ -14,7 +14,7 @@ pub struct Mutex { impl Mutex { /// Create a new mutex. - pub fn new(val: T) -> Self { + pub const fn new(val: T) -> Self { Self { val: UnsafeCell::new(val), lock: AtomicBool::new(false), diff --git a/framework/jinux-frame/src/sync/rwlock.rs b/framework/jinux-frame/src/sync/rwlock.rs index b8b9da3be..2f728a057 100644 --- a/framework/jinux-frame/src/sync/rwlock.rs +++ b/framework/jinux-frame/src/sync/rwlock.rs @@ -225,6 +225,12 @@ impl<'a, T> Drop for RwLockReadGuard<'a, T> { } } +impl<'a, T: fmt::Debug> fmt::Debug for RwLockReadGuard<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&**self, f) + } +} + pub struct RwLockWriteGuard<'a, T> { inner: &'a RwLock, inner_guard: InnerGuard, @@ -266,3 +272,9 @@ impl<'a, T> Drop for RwLockWriteGuard<'a, T> { self.inner.lock.fetch_and(!(WRITER), Release); } } + +impl<'a, T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(&**self, f) + } +} diff --git a/framework/jinux-frame/src/sync/wait.rs b/framework/jinux-frame/src/sync/wait.rs index 49af2e378..38a097d8e 100644 --- a/framework/jinux-frame/src/sync/wait.rs +++ b/framework/jinux-frame/src/sync/wait.rs @@ -18,7 +18,7 @@ pub struct WaitQueue { impl WaitQueue { /// Creates a new instance. - pub fn new() -> Self { + pub const fn new() -> Self { WaitQueue { waiters: SpinLock::new(VecDeque::new()), } @@ -38,6 +38,10 @@ impl WaitQueue { where F: FnMut() -> Option, { + if let Some(res) = cond() { + return res; + } + let waiter = Arc::new(Waiter::new()); self.enqueue(&waiter); loop { diff --git a/framework/jinux-frame/src/task/processor.rs b/framework/jinux-frame/src/task/processor.rs index 15ba79d07..d824cd11d 100644 --- a/framework/jinux-frame/src/task/processor.rs +++ b/framework/jinux-frame/src/task/processor.rs @@ -2,6 +2,7 @@ use core::sync::atomic::AtomicUsize; use crate::cpu::CpuLocal; use crate::cpu_local; +use crate::sync::Mutex; use core::sync::atomic::Ordering::Relaxed; @@ -13,7 +14,6 @@ use super::{ use alloc::sync::Arc; use lazy_static::lazy_static; use log::warn; -use spin::Mutex; pub struct Processor { current: Option>, diff --git a/framework/jinux-frame/src/timer.rs b/framework/jinux-frame/src/timer.rs index a02b9bcb3..a71b2db14 100644 --- a/framework/jinux-frame/src/timer.rs +++ b/framework/jinux-frame/src/timer.rs @@ -2,9 +2,9 @@ #[cfg(target_arch = "x86_64")] use crate::arch::x86::timer::{add_timeout_list, TimerCallback, TICK}; +use crate::sync::Mutex; use crate::{config::TIMER_FREQ, prelude::*}; use core::{sync::atomic::Ordering, time::Duration}; -use spin::Mutex; #[cfg(target_arch = "x86_64")] pub use crate::arch::x86::timer::read_monotonic_milli_seconds; diff --git a/framework/jinux-frame/src/trap/irq.rs b/framework/jinux-frame/src/trap/irq.rs index a71de8a52..256dfad91 100644 --- a/framework/jinux-frame/src/trap/irq.rs +++ b/framework/jinux-frame/src/trap/irq.rs @@ -1,11 +1,11 @@ use crate::arch::irq; use crate::arch::irq::{IRQ_LIST, NOT_USING_IRQ}; +use crate::sync::{Mutex, MutexGuard}; use crate::task::{disable_preempt, DisablePreemptGuard}; use crate::util::recycle_allocator::RecycleAllocator; use crate::{prelude::*, Error}; use core::fmt::Debug; -use spin::{Mutex, MutexGuard}; use trapframe::TrapFrame; pub fn allocate_irq() -> Result { diff --git a/framework/jinux-frame/src/vm/space.rs b/framework/jinux-frame/src/vm/space.rs index a949ec577..75818244d 100644 --- a/framework/jinux-frame/src/vm/space.rs +++ b/framework/jinux-frame/src/vm/space.rs @@ -1,8 +1,8 @@ use crate::arch::mm::PageTableFlags; use crate::config::PAGE_SIZE; +use crate::sync::Mutex; use bitflags::bitflags; use core::ops::Range; -use spin::Mutex; use super::VmFrameVec; use super::{is_page_aligned, Vaddr}; diff --git a/services/comps/block/src/virtio.rs b/services/comps/block/src/virtio.rs index 57548622f..eca98db95 100644 --- a/services/comps/block/src/virtio.rs +++ b/services/comps/block/src/virtio.rs @@ -1,11 +1,11 @@ //! Block device based on Virtio +use jinux_frame::sync::Mutex; use jinux_frame::{io_mem::IoMem, trap::TrapFrame}; use jinux_pci::msix::MSIX; use jinux_util::safe_ptr::SafePtr; use jinux_virtio::{device::block::device::BLKDevice, PCIVirtioDevice, VirtioPciCommonCfg}; use log::debug; -use spin::Mutex; use crate::{BlockDevice, BLK_COMPONENT}; diff --git a/services/comps/input/src/lib.rs b/services/comps/input/src/lib.rs index 799a292b7..d6ec790b5 100644 --- a/services/comps/input/src/lib.rs +++ b/services/comps/input/src/lib.rs @@ -14,9 +14,10 @@ use alloc::sync::Arc; use alloc::vec::Vec; use component::init_component; use component::ComponentInitError; +use jinux_frame::sync::Mutex; use jinux_virtio::VirtioDeviceType; -use spin::{Mutex, Once}; +use spin::Once; use virtio::VirtioInputDevice; use virtio_input_decoder::DecodeType; diff --git a/services/comps/input/src/virtio.rs b/services/comps/input/src/virtio.rs index c982ca49b..03f9f7b93 100644 --- a/services/comps/input/src/virtio.rs +++ b/services/comps/input/src/virtio.rs @@ -3,6 +3,7 @@ use alloc::{string::String, sync::Arc, vec::Vec}; use jinux_frame::io_mem::IoMem; use jinux_frame::offset_of; +use jinux_frame::sync::Mutex; use jinux_frame::trap::TrapFrame; use jinux_pci::msix::MSIX; use jinux_util::field_ptr; @@ -14,7 +15,6 @@ use jinux_virtio::{ PCIVirtioDevice, }; use log::{debug, info}; -use spin::Mutex; use virtio_input_decoder::{DecodeType, Decoder}; use crate::INPUTDevice; diff --git a/services/comps/pci/src/lib.rs b/services/comps/pci/src/lib.rs index 626a739ce..ce3a60738 100644 --- a/services/comps/pci/src/lib.rs +++ b/services/comps/pci/src/lib.rs @@ -12,7 +12,8 @@ use component::ComponentInitError; use alloc::{sync::Arc, vec::Vec}; use jinux_frame::bus::pci::PciDeviceLocation; -use spin::{mutex::Mutex, Once}; +use jinux_frame::sync::Mutex; +use spin::Once; use util::CSpaceAccessMethod; pub use crate::util::PciDevice; diff --git a/services/comps/time/src/rtc.rs b/services/comps/time/src/rtc.rs index 5e70e0344..61e4657d3 100644 --- a/services/comps/time/src/rtc.rs +++ b/services/comps/time/src/rtc.rs @@ -1,11 +1,10 @@ use core::sync::atomic::AtomicU8; use core::sync::atomic::Ordering::Relaxed; -use spin::Mutex; - use crate::SystemTime; use jinux_frame::arch::x86::device::cmos::{get_century, CMOS_ADDRESS, CMOS_DATA}; +use jinux_frame::sync::Mutex; pub(crate) static CENTURY_REGISTER: AtomicU8 = AtomicU8::new(0); diff --git a/services/comps/virtio/src/device/input/device.rs b/services/comps/virtio/src/device/input/device.rs index d22b5c7d1..3caabe0b9 100644 --- a/services/comps/virtio/src/device/input/device.rs +++ b/services/comps/virtio/src/device/input/device.rs @@ -1,11 +1,11 @@ use crate::{device::VirtioDeviceError, queue::VirtQueue, VirtioPciCommonCfg}; use alloc::{boxed::Box, vec::Vec}; use bitflags::bitflags; +use jinux_frame::sync::Mutex; use jinux_frame::{io_mem::IoMem, offset_of}; use jinux_pci::{capability::vendor::virtio::CapabilityVirtioData, util::BAR}; use jinux_util::{field_ptr, safe_ptr::SafePtr}; use pod::Pod; -use spin::Mutex; use super::{ InputConfigSelect, InputEvent, VirtioInputConfig, QUEUE_EVENT, QUEUE_SIZE, QUEUE_STATUS, diff --git a/services/comps/virtio/src/lib.rs b/services/comps/virtio/src/lib.rs index 300f02d18..65ad6a4dc 100644 --- a/services/comps/virtio/src/lib.rs +++ b/services/comps/virtio/src/lib.rs @@ -12,12 +12,13 @@ use alloc::{collections::VecDeque, string::String, sync::Arc, vec::Vec}; use bitflags::bitflags; use component::ComponentInitError; use device::VirtioDevice; +use jinux_frame::sync::Mutex; use jinux_frame::{io_mem::IoMem, offset_of, trap::TrapFrame}; use jinux_pci::{util::BAR, PciDevice}; use jinux_util::{field_ptr, safe_ptr::SafePtr}; use log::{debug, info}; use pod::Pod; -use spin::{Mutex, Once}; +use spin::Once; use crate::device::VirtioInfo; use jinux_pci::{capability::vendor::virtio::CapabilityVirtioData, msix::MSIX}; diff --git a/services/libs/jinux-std/src/fs/ramfs/fs.rs b/services/libs/jinux-std/src/fs/ramfs/fs.rs index 71a6f20be..de8d95901 100644 --- a/services/libs/jinux-std/src/fs/ramfs/fs.rs +++ b/services/libs/jinux-std/src/fs/ramfs/fs.rs @@ -3,9 +3,9 @@ use alloc::str; use alloc::string::String; use core::sync::atomic::{AtomicUsize, Ordering}; use core::time::Duration; +use jinux_frame::sync::{RwLock, RwLockWriteGuard}; use jinux_frame::vm::VmFrame; use jinux_util::slot_vec::SlotVec; -use spin::{RwLock, RwLockWriteGuard}; use super::*; use crate::fs::device::Device; diff --git a/services/libs/jinux-std/src/prelude.rs b/services/libs/jinux-std/src/prelude.rs index da7c42dd7..31585b3a6 100644 --- a/services/libs/jinux-std/src/prelude.rs +++ b/services/libs/jinux-std/src/prelude.rs @@ -18,14 +18,11 @@ pub(crate) use core::ffi::CStr; pub(crate) use core::fmt::Debug; pub(crate) use int_to_c_enum::TryFromInt; pub(crate) use jinux_frame::config::PAGE_SIZE; -// pub(crate) use jinux_frame::sync::{Mutex, MutexGuard}; -pub(crate) use jinux_frame::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; -pub(crate) use jinux_frame::sync::{SpinLock, SpinLockGuard}; +pub(crate) use jinux_frame::sync::{Mutex, MutexGuard, RwLock, SpinLock, SpinLockGuard}; pub(crate) use jinux_frame::vm::Vaddr; pub(crate) use jinux_frame::{print, println}; pub(crate) use log::{debug, error, info, trace, warn}; pub(crate) use pod::Pod; -pub(crate) use spin::{Mutex, MutexGuard}; /// return current process #[macro_export] diff --git a/services/libs/jinux-std/src/vm/vmar/vm_mapping.rs b/services/libs/jinux-std/src/vm/vmar/vm_mapping.rs index de0e56afd..3e2d47c0a 100644 --- a/services/libs/jinux-std/src/vm/vmar/vm_mapping.rs +++ b/services/libs/jinux-std/src/vm/vmar/vm_mapping.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use core::ops::Range; +use jinux_frame::sync::Mutex; use jinux_frame::vm::{VmFrame, VmFrameVec, VmIo, VmMapOptions, VmPerm, VmSpace}; -use spin::Mutex; use crate::vm::{ vmo::get_page_idx_range,