diff --git a/framework/aster-frame/src/arch/x86/kernel/apic/mod.rs b/framework/aster-frame/src/arch/x86/kernel/apic/mod.rs index 9a536e445..b1b9b5d2b 100644 --- a/framework/aster-frame/src/arch/x86/kernel/apic/mod.rs +++ b/framework/aster-frame/src/arch/x86/kernel/apic/mod.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use alloc::{boxed::Box, sync::Arc}; +use alloc::sync::Arc; use log::info; use spin::Once; @@ -11,7 +11,7 @@ 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; @@ -71,7 +71,7 @@ pub fn init() -> Result<(), ApicInitError> { version & 0xff, (version >> 16) & 0xff ); - APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(Box::new(x2apic)))); + APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(x2apic))); Ok(()) } else if let Some(mut xapic) = xapic::XApic::new() { xapic.enable(); @@ -82,7 +82,7 @@ pub fn init() -> Result<(), ApicInitError> { version & 0xff, (version >> 16) & 0xff ); - APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(Box::new(xapic)))); + APIC_INSTANCE.call_once(|| Arc::new(Mutex::new(xapic))); Ok(()) } else { log::warn!("Not found x2APIC or xAPIC"); diff --git a/framework/aster-frame/src/sync/mutex.rs b/framework/aster-frame/src/sync/mutex.rs index 4aabb4f62..d5925b113 100644 --- a/framework/aster-frame/src/sync/mutex.rs +++ b/framework/aster-frame/src/sync/mutex.rs @@ -10,22 +10,24 @@ use core::{ use super::WaitQueue; /// A mutex with waitqueue. -pub struct Mutex { - val: UnsafeCell, +pub struct Mutex { lock: AtomicBool, queue: WaitQueue, + val: UnsafeCell, } impl Mutex { /// Create a new mutex. pub const fn new(val: T) -> Self { Self { - val: UnsafeCell::new(val), lock: AtomicBool::new(false), queue: WaitQueue::new(), + val: UnsafeCell::new(val), } } +} +impl Mutex { /// Acquire the mutex. /// /// This method runs in a block way until the mutex can be acquired. @@ -57,27 +59,27 @@ impl Mutex { } } -impl fmt::Debug for Mutex { +impl fmt::Debug for Mutex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&self.val, f) } } -unsafe impl Send for Mutex {} -unsafe impl Sync for Mutex {} +unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} #[clippy::has_significant_drop] -pub struct MutexGuard<'a, T> { +pub struct MutexGuard<'a, T: ?Sized> { mutex: &'a Mutex, } -impl<'a, T> MutexGuard<'a, T> { +impl<'a, T: ?Sized> MutexGuard<'a, T> { fn new(mutex: &'a Mutex) -> MutexGuard<'a, T> { MutexGuard { mutex } } } -impl<'a, T> Deref for MutexGuard<'a, T> { +impl<'a, T: ?Sized> Deref for MutexGuard<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { @@ -85,24 +87,24 @@ impl<'a, T> Deref for MutexGuard<'a, T> { } } -impl<'a, T> DerefMut for MutexGuard<'a, T> { +impl<'a, T: ?Sized> DerefMut for MutexGuard<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.mutex.val.get() } } } -impl<'a, T> Drop for MutexGuard<'a, T> { +impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> { fn drop(&mut self) { self.mutex.unlock(); } } -impl<'a, T: fmt::Debug> fmt::Debug for MutexGuard<'a, T> { +impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl<'a, T> !Send for MutexGuard<'a, T> {} +impl<'a, T: ?Sized> !Send for MutexGuard<'a, T> {} -unsafe impl Sync for MutexGuard<'_, T> {} +unsafe impl Sync for MutexGuard<'_, T> {} diff --git a/framework/aster-frame/src/sync/rwlock.rs b/framework/aster-frame/src/sync/rwlock.rs index 00ebf7260..a5626b7de 100644 --- a/framework/aster-frame/src/sync/rwlock.rs +++ b/framework/aster-frame/src/sync/rwlock.rs @@ -96,14 +96,14 @@ use crate::{ /// assert_eq!(*w2, 7); /// } // write lock is dropped at this point /// ``` -pub struct RwLock { - val: UnsafeCell, +pub struct RwLock { /// The internal representation of the lock state is as follows: /// - **Bit 63:** Writer lock. /// - **Bit 62:** Upgradeable reader lock. /// - **Bit 61:** Indicates if an upgradeable reader is being upgraded. /// - **Bits 60-0:** Reader lock count. lock: AtomicUsize, + val: UnsafeCell, } const READER: usize = 1; @@ -120,7 +120,9 @@ impl RwLock { lock: AtomicUsize::new(0), } } +} +impl RwLock { /// Acquire a read lock while disabling the local IRQs and spin-wait /// until it can be acquired. /// @@ -386,7 +388,7 @@ impl RwLock { } } -impl fmt::Debug for RwLock { +impl fmt::Debug for RwLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&self.val, f) } @@ -394,17 +396,17 @@ impl fmt::Debug for RwLock { /// Because there can be more than one readers to get the T's immutable ref, /// so T must be Sync to guarantee the sharing safety. -unsafe impl Send for RwLock {} -unsafe impl Sync for RwLock {} +unsafe impl Send for RwLock {} +unsafe impl Sync for RwLock {} -impl<'a, T> !Send for RwLockWriteGuard<'a, T> {} -unsafe impl Sync for RwLockWriteGuard<'_, T> {} +impl<'a, T: ?Sized> !Send for RwLockWriteGuard<'a, T> {} +unsafe impl Sync for RwLockWriteGuard<'_, T> {} -impl<'a, T> !Send for RwLockReadGuard<'a, T> {} -unsafe impl Sync for RwLockReadGuard<'_, T> {} +impl<'a, T: ?Sized> !Send for RwLockReadGuard<'a, T> {} +unsafe impl Sync for RwLockReadGuard<'_, T> {} -impl<'a, T> !Send for RwLockUpgradeableGuard<'a, T> {} -unsafe impl Sync for RwLockUpgradeableGuard<'_, T> {} +impl<'a, T: ?Sized> !Send for RwLockUpgradeableGuard<'a, T> {} +unsafe impl Sync for RwLockUpgradeableGuard<'_, T> {} enum InnerGuard { IrqGuard(DisabledLocalIrqGuard), @@ -412,12 +414,12 @@ enum InnerGuard { } /// A guard that provides immutable data access. -pub struct RwLockReadGuard<'a, T> { - inner: &'a RwLock, +pub struct RwLockReadGuard<'a, T: ?Sized> { inner_guard: InnerGuard, + inner: &'a RwLock, } -impl<'a, T> Deref for RwLockReadGuard<'a, T> { +impl<'a, T: ?Sized> Deref for RwLockReadGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -425,25 +427,25 @@ impl<'a, T> Deref for RwLockReadGuard<'a, T> { } } -impl<'a, T> Drop for RwLockReadGuard<'a, T> { +impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T> { fn drop(&mut self) { self.inner.lock.fetch_sub(READER, Release); } } -impl<'a, T: fmt::Debug> fmt::Debug for RwLockReadGuard<'a, T> { +impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for RwLockReadGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } /// A guard that provides mutable data access. -pub struct RwLockWriteGuard<'a, T> { - inner: &'a RwLock, +pub struct RwLockWriteGuard<'a, T: ?Sized> { inner_guard: InnerGuard, + inner: &'a RwLock, } -impl<'a, T> Deref for RwLockWriteGuard<'a, T> { +impl<'a, T: ?Sized> Deref for RwLockWriteGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -451,19 +453,19 @@ impl<'a, T> Deref for RwLockWriteGuard<'a, T> { } } -impl<'a, T> DerefMut for RwLockWriteGuard<'a, T> { +impl<'a, T: ?Sized> DerefMut for RwLockWriteGuard<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.inner.val.get() } } } -impl<'a, T> Drop for RwLockWriteGuard<'a, T> { +impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T> { fn drop(&mut self) { self.inner.lock.fetch_and(!WRITER, Release); } } -impl<'a, T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'a, T> { +impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for RwLockWriteGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } @@ -471,12 +473,12 @@ impl<'a, T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'a, T> { /// A guard that provides immutable data access but can be atomically /// upgraded to `RwLockWriteGuard`. -pub struct RwLockUpgradeableGuard<'a, T> { - inner: &'a RwLock, +pub struct RwLockUpgradeableGuard<'a, T: ?Sized> { inner_guard: InnerGuard, + inner: &'a RwLock, } -impl<'a, T> RwLockUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized> RwLockUpgradeableGuard<'a, T> { /// Upgrade this upread guard to a write guard atomically. /// /// After calling this method, subsequent readers will be blocked @@ -517,7 +519,7 @@ impl<'a, T> RwLockUpgradeableGuard<'a, T> { } } -impl<'a, T> Deref for RwLockUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized> Deref for RwLockUpgradeableGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -525,13 +527,13 @@ impl<'a, T> Deref for RwLockUpgradeableGuard<'a, T> { } } -impl<'a, T> Drop for RwLockUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized> Drop for RwLockUpgradeableGuard<'a, T> { fn drop(&mut self) { self.inner.lock.fetch_sub(UPGRADEABLE_READER, Release); } } -impl<'a, T: fmt::Debug> fmt::Debug for RwLockUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for RwLockUpgradeableGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } diff --git a/framework/aster-frame/src/sync/rwmutex.rs b/framework/aster-frame/src/sync/rwmutex.rs index c7e18311a..18c99ff17 100644 --- a/framework/aster-frame/src/sync/rwmutex.rs +++ b/framework/aster-frame/src/sync/rwmutex.rs @@ -84,8 +84,7 @@ use super::WaitQueue; /// assert_eq!(*w2, 7); /// } // write mutex is dropped at this point /// ``` -pub struct RwMutex { - val: UnsafeCell, +pub struct RwMutex { /// The internal representation of the mutex state is as follows: /// - **Bit 63:** Writer mutex. /// - **Bit 62:** Upgradeable reader mutex. @@ -94,6 +93,7 @@ pub struct RwMutex { lock: AtomicUsize, /// Threads that fail to acquire the mutex will sleep on this waitqueue. queue: WaitQueue, + val: UnsafeCell, } const READER: usize = 1; @@ -111,7 +111,9 @@ impl RwMutex { queue: WaitQueue::new(), } } +} +impl RwMutex { /// Acquire a read mutex and sleep until it can be acquired. /// /// The calling thread will sleep until there are no writers or upgrading @@ -188,7 +190,7 @@ impl RwMutex { } } -impl fmt::Debug for RwMutex { +impl fmt::Debug for RwMutex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&self.val, f) } @@ -196,24 +198,24 @@ impl fmt::Debug for RwMutex { /// Because there can be more than one readers to get the T's immutable ref, /// so T must be Sync to guarantee the sharing safety. -unsafe impl Send for RwMutex {} -unsafe impl Sync for RwMutex {} +unsafe impl Send for RwMutex {} +unsafe impl Sync for RwMutex {} -impl<'a, T> !Send for RwMutexWriteGuard<'a, T> {} -unsafe impl Sync for RwMutexWriteGuard<'_, T> {} +impl<'a, T: ?Sized> !Send for RwMutexWriteGuard<'a, T> {} +unsafe impl Sync for RwMutexWriteGuard<'_, T> {} -impl<'a, T> !Send for RwMutexReadGuard<'a, T> {} -unsafe impl Sync for RwMutexReadGuard<'_, T> {} +impl<'a, T: ?Sized> !Send for RwMutexReadGuard<'a, T> {} +unsafe impl Sync for RwMutexReadGuard<'_, T> {} -impl<'a, T> !Send for RwMutexUpgradeableGuard<'a, T> {} -unsafe impl Sync for RwMutexUpgradeableGuard<'_, T> {} +impl<'a, T: ?Sized> !Send for RwMutexUpgradeableGuard<'a, T> {} +unsafe impl Sync for RwMutexUpgradeableGuard<'_, T> {} /// A guard that provides immutable data access. -pub struct RwMutexReadGuard<'a, T> { +pub struct RwMutexReadGuard<'a, T: ?Sized> { inner: &'a RwMutex, } -impl<'a, T> Deref for RwMutexReadGuard<'a, T> { +impl<'a, T: ?Sized> Deref for RwMutexReadGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -221,7 +223,7 @@ impl<'a, T> Deref for RwMutexReadGuard<'a, T> { } } -impl<'a, T> Drop for RwMutexReadGuard<'a, T> { +impl<'a, T: ?Sized> Drop for RwMutexReadGuard<'a, T> { fn drop(&mut self) { // When there are no readers, wake up a waiting writer. if self.inner.lock.fetch_sub(READER, Release) == READER { @@ -231,11 +233,11 @@ impl<'a, T> Drop for RwMutexReadGuard<'a, T> { } /// A guard that provides mutable data access. -pub struct RwMutexWriteGuard<'a, T> { +pub struct RwMutexWriteGuard<'a, T: ?Sized> { inner: &'a RwMutex, } -impl<'a, T> Deref for RwMutexWriteGuard<'a, T> { +impl<'a, T: ?Sized> Deref for RwMutexWriteGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -243,13 +245,13 @@ impl<'a, T> Deref for RwMutexWriteGuard<'a, T> { } } -impl<'a, T> DerefMut for RwMutexWriteGuard<'a, T> { +impl<'a, T: ?Sized> DerefMut for RwMutexWriteGuard<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.inner.val.get() } } } -impl<'a, T> Drop for RwMutexWriteGuard<'a, T> { +impl<'a, T: ?Sized> Drop for RwMutexWriteGuard<'a, T> { fn drop(&mut self) { self.inner.lock.fetch_and(!WRITER, Release); @@ -263,11 +265,11 @@ impl<'a, T> Drop for RwMutexWriteGuard<'a, T> { /// A guard that provides immutable data access but can be atomically /// upgraded to `RwMutexWriteGuard`. -pub struct RwMutexUpgradeableGuard<'a, T> { +pub struct RwMutexUpgradeableGuard<'a, T: ?Sized> { inner: &'a RwMutex, } -impl<'a, T> RwMutexUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized> RwMutexUpgradeableGuard<'a, T> { /// Upgrade this upread guard to a write guard atomically. /// /// After calling this method, subsequent readers will be blocked @@ -307,7 +309,7 @@ impl<'a, T> RwMutexUpgradeableGuard<'a, T> { } } -impl<'a, T> Deref for RwMutexUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized> Deref for RwMutexUpgradeableGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -315,7 +317,7 @@ impl<'a, T> Deref for RwMutexUpgradeableGuard<'a, T> { } } -impl<'a, T> Drop for RwMutexUpgradeableGuard<'a, T> { +impl<'a, T: ?Sized> Drop for RwMutexUpgradeableGuard<'a, T> { fn drop(&mut self) { let res = self.inner.lock.fetch_sub(UPGRADEABLE_READER, Release); if res == 0 { diff --git a/framework/aster-frame/src/sync/spin.rs b/framework/aster-frame/src/sync/spin.rs index 1a6d8abb8..2e477e7a1 100644 --- a/framework/aster-frame/src/sync/spin.rs +++ b/framework/aster-frame/src/sync/spin.rs @@ -13,20 +13,22 @@ use crate::{ }; /// A spin lock. -pub struct SpinLock { - val: UnsafeCell, +pub struct SpinLock { lock: AtomicBool, + val: UnsafeCell, } impl SpinLock { /// Creates a new spin lock. pub const fn new(val: T) -> Self { Self { - val: UnsafeCell::new(val), lock: AtomicBool::new(false), + val: UnsafeCell::new(val), } } +} +impl SpinLock { /// Acquire the spin lock with disabling the local IRQs. This is the most secure /// locking way. /// @@ -102,15 +104,15 @@ impl SpinLock { } } -impl fmt::Debug for SpinLock { +impl fmt::Debug for SpinLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&self.val, f) } } // Safety. Only a single lock holder is permitted to access the inner data of Spinlock. -unsafe impl Send for SpinLock {} -unsafe impl Sync for SpinLock {} +unsafe impl Send for SpinLock {} +unsafe impl Sync for SpinLock {} enum InnerGuard { IrqGuard(DisabledLocalIrqGuard), @@ -118,12 +120,12 @@ enum InnerGuard { } /// The guard of a spin lock that disables the local IRQs. -pub struct SpinLockGuard<'a, T> { - lock: &'a SpinLock, +pub struct SpinLockGuard<'a, T: ?Sized> { inner_guard: InnerGuard, + lock: &'a SpinLock, } -impl<'a, T> Deref for SpinLockGuard<'a, T> { +impl<'a, T: ?Sized> Deref for SpinLockGuard<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -131,26 +133,26 @@ impl<'a, T> Deref for SpinLockGuard<'a, T> { } } -impl<'a, T> DerefMut for SpinLockGuard<'a, T> { +impl<'a, T: ?Sized> DerefMut for SpinLockGuard<'a, T> { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { &mut *self.lock.val.get() } } } -impl<'a, T> Drop for SpinLockGuard<'a, T> { +impl<'a, T: ?Sized> Drop for SpinLockGuard<'a, T> { fn drop(&mut self) { self.lock.release_lock(); } } -impl<'a, T: fmt::Debug> fmt::Debug for SpinLockGuard<'a, T> { +impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for SpinLockGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl<'a, T> !Send for SpinLockGuard<'a, T> {} +impl<'a, T: ?Sized> !Send for SpinLockGuard<'a, T> {} // Safety. `SpinLockGuard` can be shared between tasks/threads in same CPU. // As `lock()` is only called when there are no race conditions caused by interrupts. -unsafe impl Sync for SpinLockGuard<'_, T> {} +unsafe impl Sync for SpinLockGuard<'_, T> {} diff --git a/kernel/aster-nix/src/net/iface/virtio.rs b/kernel/aster-nix/src/net/iface/virtio.rs index 180dcb17b..39d565bbd 100644 --- a/kernel/aster-nix/src/net/iface/virtio.rs +++ b/kernel/aster-nix/src/net/iface/virtio.rs @@ -12,7 +12,7 @@ use super::{common::IfaceCommon, internal::IfaceInternal, Iface}; use crate::prelude::*; pub struct IfaceVirtio { - driver: Arc>>, + driver: Arc>, common: IfaceCommon, dhcp_handle: SocketHandle, weak_self: Weak, @@ -32,7 +32,7 @@ impl IfaceVirtio { )); config }; - let mut interface = smoltcp::iface::Interface::new(config, &mut **virtio_net.lock()); + let mut interface = smoltcp::iface::Interface::new(config, &mut *virtio_net.lock()); interface.update_ip_addrs(|ip_addrs| { debug_assert!(ip_addrs.is_empty()); ip_addrs.push(ip_addr).unwrap(); @@ -115,7 +115,7 @@ impl Iface for IfaceVirtio { fn poll(&self) { let mut driver = self.driver.lock_irq_disabled(); - self.common.poll(&mut **driver); + self.common.poll(&mut *driver); self.process_dhcp(); } } diff --git a/kernel/comps/network/src/lib.rs b/kernel/comps/network/src/lib.rs index 36bb3eb5a..adb546780 100644 --- a/kernel/comps/network/src/lib.rs +++ b/kernel/comps/network/src/lib.rs @@ -10,7 +10,7 @@ pub mod driver; extern crate alloc; -use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::Arc, vec::Vec}; +use alloc::{collections::BTreeMap, string::String, sync::Arc, vec::Vec}; use core::{any::Any, fmt::Debug}; use aster_frame::sync::SpinLock; @@ -50,7 +50,7 @@ pub trait AnyNetworkDevice: Send + Sync + Any + Debug { pub trait NetDeviceIrqHandler = Fn() + Send + Sync + 'static; -pub fn register_device(name: String, device: Arc>>) { +pub fn register_device(name: String, device: Arc>) { COMPONENT .get() .unwrap() @@ -59,7 +59,7 @@ pub fn register_device(name: String, device: Arc Option>>> { +pub fn get_device(str: &str) -> Option>> { let lock = COMPONENT.get().unwrap().network_device_table.lock(); let (_, device) = lock.get(str)?; Some(device.clone()) @@ -106,7 +106,7 @@ fn init() -> Result<(), ComponentInitError> { } type NetDeviceIrqHandlerListRef = Arc>>>; -type NetworkDeviceRef = Arc>>; +type NetworkDeviceRef = Arc>; struct Component { /// Device list, the key is device name, value is (callbacks, device); diff --git a/kernel/comps/virtio/src/device/network/device.rs b/kernel/comps/virtio/src/device/network/device.rs index ad7f3776a..91d53e9a3 100644 --- a/kernel/comps/virtio/src/device/network/device.rs +++ b/kernel/comps/virtio/src/device/network/device.rs @@ -102,7 +102,7 @@ impl NetworkDevice { aster_network::register_device( super::DEVICE_NAME.to_string(), - Arc::new(SpinLock::new(Box::new(device))), + Arc::new(SpinLock::new(device)), ); Ok(()) }