xwrust::xwos::lock::seqlock

Struct Seqlock

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

顺序锁结构体

Fields§

§data: UnsafeCell<T>

用户数据

Implementations§

source§

impl<T> Seqlock<T>

source

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

新建顺序锁

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

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

static GLOBAL_SEQLOCK: Seqlock<u32>  = Seqlock::new(0);
source§

impl<T: ?Sized> Seqlock<T>

source

pub fn read_begin(&self) -> SeqlockReadGuard<'_, T>

开启共享读临界区

若其他CPU正在访问写临界区,此方法会一直 自旋 测试是否可进入 共享读临界区

§示例
loop {
    let guard = sqlk.read_begin();
    let val = *guard; // 读取
    if !guard.read_retry() {
        break;
    }
}
source

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

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

§参数说明
§示例
use xwrust::xwos::lock::seqlock::*;
static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);

pub fn xwrust_example_seqlock() {
    // ...省略...
    let mut guard GLOBAL_SEQLOCK.lock(SeqlockMode::ReadExclusiveLockCpuirqSave(None));
    *guard = 1; // 写共享变量
    drop(guard); // guard 生命周期结束,自动解锁,并恢复中断标志
}
source

pub fn trylock( &self, mode: SeqlockMode, ) -> Result<SeqlockGuard<'_, T>, SeqlockError>

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

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

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

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

解锁顺序锁,并释放 SeqlockGuard

§示例
use xwrust::xwos::lock::seqlock::*;
static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);

pub fn xwrust_example_seqlock() {
    // ...省略...
    match GLOBAL_SEQLOCK.lock() {
        Ok(mut guard) => { // 上锁成功
            *guard = 1; // 访问共享变量
            Seqlock::unlock(guard); // 主动解锁
        }
        Err(e) => {
            // 上锁失败
        }
    }
}

Trait Implementations§

source§

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

source§

fn default() -> Seqlock<T>

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

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

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> From<T> for Seqlock<T>

source§

fn from(t: T) -> Self

从数据新建顺序锁

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

等价于 Seqlock::new

source§

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

source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for Seqlock<T>

§

impl<T> !RefUnwindSafe for Seqlock<T>

§

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

§

impl<T> UnwindSafe for Seqlock<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.