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 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},

View File

@ -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};

View File

@ -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<Mutex<AcpiTables<AcpiMemoryHandler>>> = Once::new();

View File

@ -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;

View File

@ -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<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 {
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");

View File

@ -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;

View File

@ -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

View File

@ -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::{

View File

@ -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};

View File

@ -14,7 +14,7 @@ pub struct Mutex<T> {
impl<T> Mutex<T> {
/// 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),

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> {
inner: &'a RwLock<T>,
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)
}
}

View File

@ -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<R>,
{
if let Some(res) = cond() {
return res;
}
let waiter = Arc::new(Waiter::new());
self.enqueue(&waiter);
loop {

View File

@ -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<Arc<Task>>,

View File

@ -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;

View File

@ -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<IrqAllocateHandle> {

View File

@ -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};

View File

@ -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};

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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,

View File

@ -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};

View File

@ -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;

View File

@ -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]

View File

@ -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,