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