Skip to main content

IrqSpinlock

Struct IrqSpinlock 

Source
pub struct IrqSpinlock<T> { /* private fields */ }
Expand description

中断安全的互斥锁

在持有锁期间会自动禁用中断,适用于内核临界区保护。

Implementations§

Source§

impl<T> IrqSpinlock<T>

Source

pub const fn new(data: T) -> Self

创建一个新的IrqSpinlock实例

§Arguments
  • data - 要保护的数据
§Returns

返回一个包含指定数据的IrqSpinlock实例

§Examples
use sparreal_kernel::os::sync::spinlock::IrqSpinlock;

let lock = IrqSpinlock::new(42);
Source

pub const fn empty() -> IrqSpinlock<()>

创建一个空的IrqSpinlock

Source

pub fn lock(&self) -> IrqMutexGuard<'_, T>

获取锁,如果锁被占用则自旋等待

调用此方法会禁用中断,直到返回的守卫被drop。

§Returns

返回一个守卫,通过它可以访问受保护的数据

§Safety

在持有锁期间,中断会被禁用,因此调用者需要确保:

  1. 尽快释放锁
  2. 不要在持有锁期间执行可能长时间阻塞的操作
  3. 避免嵌套获取同一个锁
Source

pub fn try_lock(&self) -> Option<IrqMutexGuard<'_, T>>

尝试获取锁,如果失败则立即返回

这个方法不会自旋等待,而是立即返回结果。 如果成功获取锁,中断也会被禁用,直到守卫被drop。

§Returns
  • Some(guard) - 成功获取锁时的守卫,中断已禁用
  • None - 锁已被占用时,中断状态不变
Source

pub fn is_locked(&self) -> bool

检查锁是否被占用

§Returns
  • true - 锁被占用
  • false - 锁空闲
Source

pub unsafe fn get(&self) -> *mut T

获取内部数据的不可变引用

§Safety

此方法不提供同步保证,调用者需要确保没有其他地方在并发访问数据

Source

pub unsafe fn get_mut(&mut self) -> &mut T

获取内部数据的可变引用

§Safety

此方法不提供同步保证,调用者需要确保没有其他地方在并发访问数据

Source

pub fn into_inner(self) -> T

消费IrqSpinlock并返回内部数据

§Returns

返回受保护的数据

Source

pub unsafe fn lock_raw(&self) -> &IrqRawSpinlock

获取锁但不使用RAII守卫(高级用法)

§Safety

调用者必须确保:

  1. 手动管理中断禁用
  2. 在适当的时机调用unlock
  3. 不会发生死锁或竞态条件

Trait Implementations§

Source§

impl<T: Send> Send for IrqSpinlock<T>

Source§

impl<T: Send> Sync for IrqSpinlock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for IrqSpinlock<T>

§

impl<T> !RefUnwindSafe for IrqSpinlock<T>

§

impl<T> Unpin for IrqSpinlock<T>
where T: Unpin,

§

impl<T> UnwindSafe for IrqSpinlock<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Background for T

Source§

fn bg(self, rgb: Rgb<u8>) -> WithBackground<T>

Adds the given background color
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Foreground for T

Source§

fn fg(self, rgb: Rgb<u8>) -> WithForeground<T>

Adds the given foreground color
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.