xwrust::xwos::lock::spinlock

Struct Spinlock

source
pub struct Spinlock<T: ?Sized> {
    pub data: UnsafeCell<T>,
    /* private fields */
}
Expand description

自旋锁结构体

Fields§

§data: UnsafeCell<T>

用户数据

Implementations§

source§

impl<T> Spinlock<T>

source

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

新建自旋锁

此方法是编译期方法,可用于新建 static 约束的全局变量。

§示例
use xwrust::xwos::lock::spinlock::*;

static GLOBAL_SPINLOCK: Spinlock<u32>  = Spinlock::new(0);
source§

impl<T: ?Sized> Spinlock<T>

source

pub fn lock(&self, mode: SpinlockMode) -> SpinlockGuard<'_, T>

获取自旋锁,若线程无法获取自旋锁,就阻塞等待,直到能获得锁为止

§参数说明
§示例
use xwrust::xwos::lock::spinlock::*;
static GLOBAL_SPINLOCK: Spinlock<u32> = Spinlock::new(0);

pub fn xwrust_example_spinlock() {
    // ...省略...
    let mut guard = GLOBAL_SPINLOCK.lock();
    *guard = 1; // 访问共享变量
    drop(guard); // 解锁
}
source

pub fn trylock( &self, mode: SpinlockMode, ) -> Result<SpinlockGuard<'_, T>, SpinlockError>

尝试获取自旋锁,若线程无法获取自旋锁,立即返回错误

§参数说明
§错误码
§示例
use xwrust::xwos::lock::spinlock::*;
static GLOBAL_SPINLOCK: Spinlock<u32> = Spinlock::new(0);

pub fn xwrust_example_spinlock() {
    // ...省略...
    match GLOBAL_SPINLOCK.trylock() {
        Ok(mut guard) => { // 上锁成功
            *guard = 1; // 访问共享变量
        } // guard 生命周期结束,自动解锁
        Err(e) => {
            // 上锁失败
        }
    }
}
source

pub fn unlock(guard: SpinlockGuard<'_, T>)

解锁自旋锁,并释放 SpinlockGuard

§示例
use xwrust::xwos::lock::spinlock::*;
static GLOBAL_SPINLOCK: Spinlock<u32> = Spinlock::new(0);

pub fn xwrust_example_spinlock() {
    // ...省略...
    let mut guard = GLOBAL_SPINLOCK.lock();
    *guard = 1; // 访问共享变量
    Spinlock::unlock(guard); // 主动解锁
}

Trait Implementations§

source§

impl<T: ?Sized + Default> Default for Spinlock<T>

source§

fn default() -> Spinlock<T>

Returns the “default value” for a type. Read more
source§

impl<T: ?Sized> Drop for Spinlock<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> From<T> for Spinlock<T>

source§

fn from(t: T) -> Self

从数据新建自旋锁

此方法会将数据所有权转移到自旋锁的内部

等价于 Spinlock::new

source§

impl<T: ?Sized + Send> Send for Spinlock<T>

source§

impl<T: ?Sized + Send> Sync for Spinlock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Spinlock<T>

§

impl<T> !RefUnwindSafe for Spinlock<T>

§

impl<T> Unpin for Spinlock<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for Spinlock<T>
where T: UnwindSafe + ?Sized,

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<!> for T

§

fn from(t: !) -> T

Converts to this type from the input type.
§

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.