xwrust::xwos::lock::seqlock

Struct SeqlockGuard

source
pub struct SeqlockGuard<'a, T: ?Sized + 'a> { /* private fields */ }
Expand description

顺序锁的RAII Guard

RAII Guard 用于提供 Scoped Lock 机制。

  • SeqlockGuard 中包含 Seqlock 的引用, 当 SeqlockGuard 生命周期结束时,会在 drop() 方法中自动解锁顺序锁。
  • SeqlockGuard 不可在线程之间转移所有权,因为其 drop() 方法包含解锁的语义,上锁和解锁必须在同一线程;
  • SeqlockGuard 虽然可以在多线程中传递引用( Sync 约束),但其实现中没有 unlock() 方法,意味着其他线程即便拿到引用也不能解锁。

Implementations§

source§

impl<'a, T: ?Sized> SeqlockGuard<'a, T>

source

pub fn wait(self, cond: &Cond) -> Result<SeqlockGuard<'a, T>, CondError>

阻塞当前线程,直到被条件量唤醒

此方法会消费顺序锁的守卫(Guard),并当线程阻塞时,在条件量内部释放顺序锁。 当条件成立,线程被唤醒,会在条件量内部上锁顺序锁,并重新返回顺序锁的守卫(Guard)。

  • 当返回顺序锁的守卫 SeqlockGuard 时,顺序锁已经被重新上锁;
  • 当返回 Err 时,顺序锁未被上锁。
§参数说明
  • cond: 条件量的引用
§错误码
§示例
use xwrust::xwos::thd;
use xwrust::xwos::lock::seqlock::*;
use xwrust::xwos::sync::cond::*;
extern crate alloc;
use alloc::sync::Arc;

pub fn xwrust_example_seqlock() {
    let pair = Arc::new((Seqlock::new(true), Cond::new()));
    pair.1.init();
    let pair_c = pair.clone();

    thd::Builder::new()
        .name("child".into())
        .spawn(move |_| { // 子线程闭包
            cthd::sleep(xwtm::ms(500));
            let (lock, cvar) = &*pair_c;
            let mut guard = lock_child.lock(SeqlockMode::WriteLockCpuirqSave(None));
            *guard = false; // 设置共享数据
            drop(guard); // 先解锁再触发条件可提高效率
            cvar.broadcast();
        });
    let (lock, cvar) = &*pair;
    {
        let mut guard = lock.lock(SeqlockMode::WriteLockCpuirqSave(None));
        while *guard {
            match guard.wait(cvar) {
                Ok(g) => { // 唤醒
                    guard = g;
                },
                Err(e) => { // 等待条件量失败
                    break;
                },
            }
        }
    }
}
source

pub fn wait_to( self, cond: &Cond, to: XwTm, ) -> Result<SeqlockGuard<'a, T>, CondError>

限时阻塞当前线程,直到被条件量唤醒

此方法会消费顺序锁的守卫(Guard),并当线程阻塞时,在条件量内部释放顺序锁。 当条件成立,线程被唤醒,会在条件量内部上锁顺序锁,并重新返回顺序锁的守卫(Guard)。 当超时后,将返回错误。

  • 当返回顺序锁的守卫 SeqlockGuard 时,顺序锁已经被重新上锁;
  • 当返回 Err 时,顺序锁未被上锁。
§参数说明
  • cond: 条件量的引用
  • to: 期望唤醒的时间点
§错误码
§示例
use xwrust::xwos::thd;
use xwrust::xwos::lock::seqlock::*;
use xwrust::xwos::sync::cond::*;
extern crate alloc;
use alloc::sync::Arc;

pub fn xwrust_example_seqlock() {
    let pair = Arc::new((Seqlock::new(true), Cond::new()));
    pair.1.init();
    let pair_c = pair.clone();

    thd::Builder::new()
        .name("child".into())
        .spawn(move |_| { // 子线程闭包
            cthd::sleep(xwtm::ms(500));
            let (lock, cvar) = &*pair_c;
            let mut guard = lock_child.lock(SeqlockMode::WriteLockCpuirqSave(None));
            *guard = false; // 设置共享数据
            drop(guard); // 先解锁再触发条件可提高效率
            cvar.broadcast();
        });
    let (lock, cvar) = &*pair;
    {
        let mut guard = lock.lock(SeqlockMode::WriteLockCpuirqSave(None));
        while *guard {
            match guard.wait_to(cvar, xwtm::ft(xwtm::s(2))) {
                Ok(g) => { // 唤醒
                    guard = g;
                },
                Err(e) => { // 等待条件量失败
                    break;
                },
            }
        }
    }
}
source

pub fn wait_unintr(self, cond: &Cond) -> Result<SeqlockGuard<'a, T>, CondError>

阻塞当前线程,直到被条件量唤醒,且阻塞不可被中断

此方法会消费顺序锁的守卫(Guard),并当线程阻塞时,在条件量内部释放顺序锁。 当条件成立,线程被唤醒,会在条件量内部上锁顺序锁,并重新返回顺序锁的守卫(Guard)。

  • 当返回顺序锁的守卫 SeqlockGuard 时,顺序锁已经被重新上锁;
  • 当返回 Err 时,顺序锁未被上锁。
§参数说明
  • cond: 条件量的引用
§错误码
§示例
use xwrust::xwos::thd;
use xwrust::xwos::lock::seqlock::*;
use xwrust::xwos::sync::cond::*;
extern crate alloc;
use alloc::sync::Arc;

pub fn xwrust_example_seqlock() {
    let pair = Arc::new((Seqlock::new(true), Cond::new()));
    pair.1.init();
    let pair_c = pair.clone();

    thd::Builder::new()
        .name("child".into())
        .spawn(move |_| { // 子线程闭包
            cthd::sleep(xwtm::ms(500));
            let (lock, cvar) = &*pair_c;
            let mut guard = lock_child.lock(SeqlockMode::ReadExclusiveLockCpuirqSave(None));
            *guard = false; // 设置共享数据
            drop(guard); // 先解锁再触发条件可提高效率
            cvar.broadcast();
        });
    let (lock, cvar) = &*pair;
    {
        let mut guard = lock.lock(SeqlockMode::ReadExclusiveLockCpuirqSave(None));
        while *guard {
            match guard.wait_unintr(cvar) {
                Ok(g) => { // 唤醒
                    guard = g;
                },
                Err(e) => { // 等待条件量失败
                    break;
                },
            }
        }
    }
}

Trait Implementations§

source§

impl<T: ?Sized> Deref for SeqlockGuard<'_, T>

source§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T: ?Sized> DerefMut for SeqlockGuard<'_, T>

source§

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

Mutably dereferences the value.
source§

impl<T: ?Sized> Drop for SeqlockGuard<'_, T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: ?Sized> !Send for SeqlockGuard<'_, T>

source§

impl<T: ?Sized + Sync> Sync for SeqlockGuard<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for SeqlockGuard<'a, T>
where T: ?Sized,

§

impl<'a, T> !RefUnwindSafe for SeqlockGuard<'a, T>

§

impl<'a, T> Unpin for SeqlockGuard<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for SeqlockGuard<'a, T>

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.