Use Mutex and RwLock in jinux-frame for jinux-std

This commit is contained in:
Chuandong Li 2023-08-19 17:26:02 +00:00 committed by Tate, Hongliang Tian
parent df7bd65e70
commit 6d101c5b6d
26 changed files with 54 additions and 32 deletions

View File

@ -3,8 +3,9 @@ mod fault;
mod remapping; mod remapping;
mod second_stage; mod second_stage;
use crate::sync::Mutex;
use log::info; use log::info;
use spin::{Mutex, Once}; use spin::Once;
use crate::{ use crate::{
arch::iommu::{context_table::RootTable, second_stage::PageTableEntry}, arch::iommu::{context_table::RootTable, second_stage::PageTableEntry},

View File

@ -1,5 +1,6 @@
use crate::sync::Mutex;
use alloc::vec::Vec; use alloc::vec::Vec;
use spin::{Mutex, Once}; use spin::Once;
use crate::{trap::IrqLine, util::recycle_allocator::RecycleAllocator}; use crate::{trap::IrqLine, util::recycle_allocator::RecycleAllocator};

View File

@ -7,11 +7,12 @@ use core::{
}; };
use crate::boot::{self, BootloaderAcpiArg}; use crate::boot::{self, BootloaderAcpiArg};
use crate::sync::Mutex;
use crate::vm::paddr_to_vaddr; use crate::vm::paddr_to_vaddr;
use acpi::{sdt::SdtHeader, AcpiHandler, AcpiTable, AcpiTables}; use acpi::{sdt::SdtHeader, AcpiHandler, AcpiTable, AcpiTables};
use alloc::borrow::ToOwned; use alloc::borrow::ToOwned;
use log::info; use log::info;
use spin::{Mutex, Once}; use spin::Once;
/// RSDP information, key is the signature, value is the virtual address of the signature /// RSDP information, key is the signature, value is the virtual address of the signature
pub static ACPI_TABLES: Once<Mutex<AcpiTables<AcpiMemoryHandler>>> = Once::new(); pub static ACPI_TABLES: Once<Mutex<AcpiTables<AcpiMemoryHandler>>> = Once::new();

View File

@ -1,6 +1,7 @@
use crate::sync::Mutex;
use acpi::PlatformInfo; use acpi::PlatformInfo;
use log::info; use log::info;
use spin::{Mutex, Once}; use spin::Once;
use x86::apic::ioapic::IoApic; use x86::apic::ioapic::IoApic;
use crate::arch::x86::kernel::acpi::ACPI_TABLES; use crate::arch::x86::kernel::acpi::ACPI_TABLES;

View File

@ -1,12 +1,14 @@
use crate::sync::Mutex;
use alloc::boxed::Box;
use alloc::sync::Arc; use alloc::sync::Arc;
use log::info; use log::info;
use spin::{Mutex, Once}; use spin::Once;
pub mod ioapic; pub mod ioapic;
pub mod x2apic; pub mod x2apic;
pub mod xapic; pub mod xapic;
pub static APIC_INSTANCE: Once<Arc<Mutex<dyn Apic + 'static>>> = Once::new(); pub static APIC_INSTANCE: Once<Arc<Mutex<Box<dyn Apic + 'static>>>> = Once::new();
pub trait Apic: ApicTimer + Sync + Send { pub trait Apic: ApicTimer + Sync + Send {
fn id(&self) -> u32; fn id(&self) -> u32;
@ -66,7 +68,7 @@ pub fn init() -> Result<(), ApicInitError> {
version & 0xff, version & 0xff,
(version >> 16) & 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(()) Ok(())
} else if let Some(mut xapic) = xapic::XApic::new() { } else if let Some(mut xapic) = xapic::XApic::new() {
xapic.enable(); xapic.enable();
@ -77,7 +79,7 @@ pub fn init() -> Result<(), ApicInitError> {
version & 0xff, version & 0xff,
(version >> 16) & 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(()) Ok(())
} else { } else {
log::warn!("Not found x2APIC or xAPIC"); log::warn!("Not found x2APIC or xAPIC");

View File

@ -1,5 +1,6 @@
use crate::sync::Mutex;
use crate::vm; use crate::vm;
use spin::{Mutex, Once}; use spin::Once;
use x86::apic::xapic; use x86::apic::xapic;
use super::ApicTimer; use super::ApicTimer;

View File

@ -14,10 +14,10 @@ const IRQ_OFFSET: u8 = 0x20;
const TIMER_IRQ_NUM: u8 = 32; const TIMER_IRQ_NUM: u8 = 32;
use crate::sync::Mutex;
use alloc::vec::Vec; use alloc::vec::Vec;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::info; use log::info;
use spin::Mutex;
lazy_static! { lazy_static! {
/// store the irq, although we have APIC for manage interrupts /// store the irq, although we have APIC for manage interrupts

View File

@ -1,6 +1,6 @@
use crate::sync::Mutex;
use alloc::{collections::BTreeMap, fmt}; use alloc::{collections::BTreeMap, fmt};
use pod::Pod; use pod::Pod;
use spin::Mutex;
use x86_64::{instructions::tlb, structures::paging::PhysFrame, VirtAddr}; use x86_64::{instructions::tlb, structures::paging::PhysFrame, VirtAddr};
use crate::{ use crate::{

View File

@ -54,8 +54,8 @@ mod cfg_space;
mod common_device; mod common_device;
mod device_info; mod device_info;
use crate::sync::Mutex;
pub use device_info::{PciDeviceId, PciDeviceLocation}; pub use device_info::{PciDeviceId, PciDeviceLocation};
use spin::Mutex;
use self::{bus::PciBus, common_device::PciCommonDevice}; use self::{bus::PciBus, common_device::PciCommonDevice};

View File

@ -14,7 +14,7 @@ pub struct Mutex<T> {
impl<T> Mutex<T> { impl<T> Mutex<T> {
/// Create a new mutex. /// Create a new mutex.
pub fn new(val: T) -> Self { pub const fn new(val: T) -> Self {
Self { Self {
val: UnsafeCell::new(val), val: UnsafeCell::new(val),
lock: AtomicBool::new(false), lock: AtomicBool::new(false),

View File

@ -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> { pub struct RwLockWriteGuard<'a, T> {
inner: &'a RwLock<T>, inner: &'a RwLock<T>,
inner_guard: InnerGuard, inner_guard: InnerGuard,
@ -266,3 +272,9 @@ impl<'a, T> Drop for RwLockWriteGuard<'a, T> {
self.inner.lock.fetch_and(!(WRITER), Release); 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)
}
}

View File

@ -18,7 +18,7 @@ pub struct WaitQueue {
impl WaitQueue { impl WaitQueue {
/// Creates a new instance. /// Creates a new instance.
pub fn new() -> Self { pub const fn new() -> Self {
WaitQueue { WaitQueue {
waiters: SpinLock::new(VecDeque::new()), waiters: SpinLock::new(VecDeque::new()),
} }
@ -38,6 +38,10 @@ impl WaitQueue {
where where
F: FnMut() -> Option<R>, F: FnMut() -> Option<R>,
{ {
if let Some(res) = cond() {
return res;
}
let waiter = Arc::new(Waiter::new()); let waiter = Arc::new(Waiter::new());
self.enqueue(&waiter); self.enqueue(&waiter);
loop { loop {

View File

@ -2,6 +2,7 @@ use core::sync::atomic::AtomicUsize;
use crate::cpu::CpuLocal; use crate::cpu::CpuLocal;
use crate::cpu_local; use crate::cpu_local;
use crate::sync::Mutex;
use core::sync::atomic::Ordering::Relaxed; use core::sync::atomic::Ordering::Relaxed;
@ -13,7 +14,6 @@ use super::{
use alloc::sync::Arc; use alloc::sync::Arc;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::warn; use log::warn;
use spin::Mutex;
pub struct Processor { pub struct Processor {
current: Option<Arc<Task>>, current: Option<Arc<Task>>,

View File

@ -2,9 +2,9 @@
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use crate::arch::x86::timer::{add_timeout_list, TimerCallback, TICK}; use crate::arch::x86::timer::{add_timeout_list, TimerCallback, TICK};
use crate::sync::Mutex;
use crate::{config::TIMER_FREQ, prelude::*}; use crate::{config::TIMER_FREQ, prelude::*};
use core::{sync::atomic::Ordering, time::Duration}; use core::{sync::atomic::Ordering, time::Duration};
use spin::Mutex;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub use crate::arch::x86::timer::read_monotonic_milli_seconds; pub use crate::arch::x86::timer::read_monotonic_milli_seconds;

View File

@ -1,11 +1,11 @@
use crate::arch::irq; use crate::arch::irq;
use crate::arch::irq::{IRQ_LIST, NOT_USING_IRQ}; use crate::arch::irq::{IRQ_LIST, NOT_USING_IRQ};
use crate::sync::{Mutex, MutexGuard};
use crate::task::{disable_preempt, DisablePreemptGuard}; use crate::task::{disable_preempt, DisablePreemptGuard};
use crate::util::recycle_allocator::RecycleAllocator; use crate::util::recycle_allocator::RecycleAllocator;
use crate::{prelude::*, Error}; use crate::{prelude::*, Error};
use core::fmt::Debug; use core::fmt::Debug;
use spin::{Mutex, MutexGuard};
use trapframe::TrapFrame; use trapframe::TrapFrame;
pub fn allocate_irq() -> Result<IrqAllocateHandle> { pub fn allocate_irq() -> Result<IrqAllocateHandle> {

View File

@ -1,8 +1,8 @@
use crate::arch::mm::PageTableFlags; use crate::arch::mm::PageTableFlags;
use crate::config::PAGE_SIZE; use crate::config::PAGE_SIZE;
use crate::sync::Mutex;
use bitflags::bitflags; use bitflags::bitflags;
use core::ops::Range; use core::ops::Range;
use spin::Mutex;
use super::VmFrameVec; use super::VmFrameVec;
use super::{is_page_aligned, Vaddr}; use super::{is_page_aligned, Vaddr};

View File

@ -1,11 +1,11 @@
//! Block device based on Virtio //! Block device based on Virtio
use jinux_frame::sync::Mutex;
use jinux_frame::{io_mem::IoMem, trap::TrapFrame}; use jinux_frame::{io_mem::IoMem, trap::TrapFrame};
use jinux_pci::msix::MSIX; use jinux_pci::msix::MSIX;
use jinux_util::safe_ptr::SafePtr; use jinux_util::safe_ptr::SafePtr;
use jinux_virtio::{device::block::device::BLKDevice, PCIVirtioDevice, VirtioPciCommonCfg}; use jinux_virtio::{device::block::device::BLKDevice, PCIVirtioDevice, VirtioPciCommonCfg};
use log::debug; use log::debug;
use spin::Mutex;
use crate::{BlockDevice, BLK_COMPONENT}; use crate::{BlockDevice, BLK_COMPONENT};

View File

@ -14,9 +14,10 @@ use alloc::sync::Arc;
use alloc::vec::Vec; use alloc::vec::Vec;
use component::init_component; use component::init_component;
use component::ComponentInitError; use component::ComponentInitError;
use jinux_frame::sync::Mutex;
use jinux_virtio::VirtioDeviceType; use jinux_virtio::VirtioDeviceType;
use spin::{Mutex, Once}; use spin::Once;
use virtio::VirtioInputDevice; use virtio::VirtioInputDevice;
use virtio_input_decoder::DecodeType; use virtio_input_decoder::DecodeType;

View File

@ -3,6 +3,7 @@
use alloc::{string::String, sync::Arc, vec::Vec}; use alloc::{string::String, sync::Arc, vec::Vec};
use jinux_frame::io_mem::IoMem; use jinux_frame::io_mem::IoMem;
use jinux_frame::offset_of; use jinux_frame::offset_of;
use jinux_frame::sync::Mutex;
use jinux_frame::trap::TrapFrame; use jinux_frame::trap::TrapFrame;
use jinux_pci::msix::MSIX; use jinux_pci::msix::MSIX;
use jinux_util::field_ptr; use jinux_util::field_ptr;
@ -14,7 +15,6 @@ use jinux_virtio::{
PCIVirtioDevice, PCIVirtioDevice,
}; };
use log::{debug, info}; use log::{debug, info};
use spin::Mutex;
use virtio_input_decoder::{DecodeType, Decoder}; use virtio_input_decoder::{DecodeType, Decoder};
use crate::INPUTDevice; use crate::INPUTDevice;

View File

@ -12,7 +12,8 @@ use component::ComponentInitError;
use alloc::{sync::Arc, vec::Vec}; use alloc::{sync::Arc, vec::Vec};
use jinux_frame::bus::pci::PciDeviceLocation; use jinux_frame::bus::pci::PciDeviceLocation;
use spin::{mutex::Mutex, Once}; use jinux_frame::sync::Mutex;
use spin::Once;
use util::CSpaceAccessMethod; use util::CSpaceAccessMethod;
pub use crate::util::PciDevice; pub use crate::util::PciDevice;

View File

@ -1,11 +1,10 @@
use core::sync::atomic::AtomicU8; use core::sync::atomic::AtomicU8;
use core::sync::atomic::Ordering::Relaxed; use core::sync::atomic::Ordering::Relaxed;
use spin::Mutex;
use crate::SystemTime; use crate::SystemTime;
use jinux_frame::arch::x86::device::cmos::{get_century, CMOS_ADDRESS, CMOS_DATA}; 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); pub(crate) static CENTURY_REGISTER: AtomicU8 = AtomicU8::new(0);

View File

@ -1,11 +1,11 @@
use crate::{device::VirtioDeviceError, queue::VirtQueue, VirtioPciCommonCfg}; use crate::{device::VirtioDeviceError, queue::VirtQueue, VirtioPciCommonCfg};
use alloc::{boxed::Box, vec::Vec}; use alloc::{boxed::Box, vec::Vec};
use bitflags::bitflags; use bitflags::bitflags;
use jinux_frame::sync::Mutex;
use jinux_frame::{io_mem::IoMem, offset_of}; use jinux_frame::{io_mem::IoMem, offset_of};
use jinux_pci::{capability::vendor::virtio::CapabilityVirtioData, util::BAR}; use jinux_pci::{capability::vendor::virtio::CapabilityVirtioData, util::BAR};
use jinux_util::{field_ptr, safe_ptr::SafePtr}; use jinux_util::{field_ptr, safe_ptr::SafePtr};
use pod::Pod; use pod::Pod;
use spin::Mutex;
use super::{ use super::{
InputConfigSelect, InputEvent, VirtioInputConfig, QUEUE_EVENT, QUEUE_SIZE, QUEUE_STATUS, InputConfigSelect, InputEvent, VirtioInputConfig, QUEUE_EVENT, QUEUE_SIZE, QUEUE_STATUS,

View File

@ -12,12 +12,13 @@ use alloc::{collections::VecDeque, string::String, sync::Arc, vec::Vec};
use bitflags::bitflags; use bitflags::bitflags;
use component::ComponentInitError; use component::ComponentInitError;
use device::VirtioDevice; use device::VirtioDevice;
use jinux_frame::sync::Mutex;
use jinux_frame::{io_mem::IoMem, offset_of, trap::TrapFrame}; use jinux_frame::{io_mem::IoMem, offset_of, trap::TrapFrame};
use jinux_pci::{util::BAR, PciDevice}; use jinux_pci::{util::BAR, PciDevice};
use jinux_util::{field_ptr, safe_ptr::SafePtr}; use jinux_util::{field_ptr, safe_ptr::SafePtr};
use log::{debug, info}; use log::{debug, info};
use pod::Pod; use pod::Pod;
use spin::{Mutex, Once}; use spin::Once;
use crate::device::VirtioInfo; use crate::device::VirtioInfo;
use jinux_pci::{capability::vendor::virtio::CapabilityVirtioData, msix::MSIX}; use jinux_pci::{capability::vendor::virtio::CapabilityVirtioData, msix::MSIX};

View File

@ -3,9 +3,9 @@ use alloc::str;
use alloc::string::String; use alloc::string::String;
use core::sync::atomic::{AtomicUsize, Ordering}; use core::sync::atomic::{AtomicUsize, Ordering};
use core::time::Duration; use core::time::Duration;
use jinux_frame::sync::{RwLock, RwLockWriteGuard};
use jinux_frame::vm::VmFrame; use jinux_frame::vm::VmFrame;
use jinux_util::slot_vec::SlotVec; use jinux_util::slot_vec::SlotVec;
use spin::{RwLock, RwLockWriteGuard};
use super::*; use super::*;
use crate::fs::device::Device; use crate::fs::device::Device;

View File

@ -18,14 +18,11 @@ pub(crate) use core::ffi::CStr;
pub(crate) use core::fmt::Debug; pub(crate) use core::fmt::Debug;
pub(crate) use int_to_c_enum::TryFromInt; pub(crate) use int_to_c_enum::TryFromInt;
pub(crate) use jinux_frame::config::PAGE_SIZE; pub(crate) use jinux_frame::config::PAGE_SIZE;
// pub(crate) use jinux_frame::sync::{Mutex, MutexGuard}; pub(crate) use jinux_frame::sync::{Mutex, MutexGuard, RwLock, SpinLock, SpinLockGuard};
pub(crate) use jinux_frame::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
pub(crate) use jinux_frame::sync::{SpinLock, SpinLockGuard};
pub(crate) use jinux_frame::vm::Vaddr; pub(crate) use jinux_frame::vm::Vaddr;
pub(crate) use jinux_frame::{print, println}; pub(crate) use jinux_frame::{print, println};
pub(crate) use log::{debug, error, info, trace, warn}; pub(crate) use log::{debug, error, info, trace, warn};
pub(crate) use pod::Pod; pub(crate) use pod::Pod;
pub(crate) use spin::{Mutex, MutexGuard};
/// return current process /// return current process
#[macro_export] #[macro_export]

View File

@ -1,7 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use core::ops::Range; use core::ops::Range;
use jinux_frame::sync::Mutex;
use jinux_frame::vm::{VmFrame, VmFrameVec, VmIo, VmMapOptions, VmPerm, VmSpace}; use jinux_frame::vm::{VmFrame, VmFrameVec, VmIo, VmMapOptions, VmPerm, VmSpace};
use spin::Mutex;
use crate::vm::{ use crate::vm::{
vmo::get_page_idx_range, vmo::get_page_idx_range,