RawSpinLock

Struct RawSpinLock 

Source
pub struct RawSpinLock {
    lock: AtomicBool,
}
Expand description

自旋锁结构体,提供互斥访问临界区的能力。 基于原子操作实现自旋锁机制,结合 IntrGuard 实现中断保护。 不可重入 (即不能嵌套调用 RawSpinLock::lock())。 使用示例:

let lock = RawSpinLock::new();
{
  let guard = lock.lock(); // 获取锁,禁用中断
  // 临界区代码
} // 离开作用域,自动释放锁并恢复中断状态

Fields§

§lock: AtomicBool

Implementations§

Source§

impl RawSpinLock

Source

pub const fn new() -> Self

Source

pub fn lock(&self) -> RawSpinLockGuard<'_>

尝试获取自旋锁,并返回一个 RAII 保护器。

内部原子地获取锁,并在当前 CPU 禁用本地中断。

Source

pub fn try_lock(&self) -> Option<RawSpinLockGuard<'_>>

尝试获取自旋锁,如果成功则返回 RAII 保护器,否则返回 None。

内部原子地尝试获取锁,并在当前 CPU 禁用本地中断。 如果获取失败,会立即恢复中断状态(通过 Drop IntrGuard)。

Source

fn unlock(&self)

仅释放锁标志。

Trait Implementations§

Source§

impl Debug for RawSpinLock

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for RawSpinLock

§

impl RefUnwindSafe for RawSpinLock

§

impl Send for RawSpinLock

§

impl Sync for RawSpinLock

§

impl Unpin for RawSpinLock

§

impl UnwindSafe for RawSpinLock

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.