xwrust::xwos::sync::cond

Struct Cond

source
pub struct Cond { /* private fields */ }
Expand description

条件量对象结构体

Implementations§

source§

impl Cond

source

pub const fn new() -> Self

新建条件量对象

此方法是编译期方法。

§示例
  • 具有 static 约束的全局变量全局变量:
use xwrust::xwos::sync::cond::*;

static GLOBAL_COND: Cond  = Cond::new();
  • 在heap中创建:
extern crate alloc;
use alloc::sync::Arc;

pub fn xwrust_example_cond() {
    let cond = Arc::new(Cond::new());
}
source§

impl Cond

source

pub fn init(&self)

初始化条件量对象

条件量对象必须调用此方法一次,方可正常使用。

§上下文
  • 任意
§示例
use xwrust::xwos::sync::cond::*;

static GLOBAL_COND: Cond = Cond::new();

pub unsafe extern "C" fn xwrust_main() {
    // ...省略...
    GLOBAL_COND.init();
    // 从此处开始 GLOBAL_COND 可正常使用
}
source

pub fn freeze(&self) -> CondError

冻结条件量

条件量被冻结后,可被线程等待,但被能被单播 Cond::unicast() 或广播 Cond::broadcast()

§上下文
  • 任意
§错误码
§示例
use xwrust::xwos::sync::cond::*;

pub unsafe extern "C" fn xwrust_main() {
    // ...省略...
    condvar: Cond = Cond::new();
    condvar.init();
    condvar.freeze();
}
source

pub fn thaw(&self) -> CondError

解冻条件量

被冻结的条件量解冻后,可被单播 Cond::unicast() 或广播 Cond::broadcast()

§上下文
  • 任意
§错误码
§示例
use xwrust::xwos::sync::cond::*;

pub unsafe extern "C" fn xwrust_main() {
    // ...省略...
    condvar: Cond = Cond::new();
    condvar.init();
    condvar.freeze(); // 冻结
    // ...省略...
    condvar.thaw(); // 解冻
}
source

pub fn unicast(&self) -> CondError

单播条件量对象

只会唤醒第一个线程,阻塞线程的队列使用的是先进先出(FIFO)算法 。

§上下文
  • 任意
§错误码
§示例
use xwrust::xwos::sync::cond::*;

pub unsafe extern "C" fn xwrust_main() {
    // ...省略...
    condvar: Cond = Cond::new();
    condvar.init();
    // ...省略...
    condvar.unicast();
}
source

pub fn broadcast(&self) -> CondError

广播条件量

阻塞队列中的线程会全部被唤醒。

§上下文
  • 任意
§错误码
§示例
use xwrust::xwos::sync::cond::*;

pub unsafe extern "C" fn xwrust_main() {
    // ...省略...
    condvar: Cond = Cond::new();
    condvar.init();
    // ...省略...
    condvar.broadcast();
}
source

pub fn bind<'a, const M: XwSz>( &'a self, sel: &'a Sel<M>, pos: XwSq, ) -> Result<CondSel<'a, M>, CondError>
where [XwBmp; { _ }]: Sized,

绑定条件量对象到信号选择器

CondSel<'a, M> 中包含条件量的绑定信息。 CondSel<'a, M>CondSel<M> 具有相同的生命周期约束 'aCondSel::selected() 可用来判断条件量是否被选择。当 CondSel<'a, M> drop() 时,会自动解绑。

§参数说明
  • sel: 信号选择器的引用
  • pos: 位置
§上下文
  • 任意
§错误码
§示例
pub fn xwrust_example_sel() {
    // ...省略...
    let cond0 = Arc::new(Cond::new());
    cond0.init();
    let cond0sel = match cond0.bind(&sel, 0) {
        Ok(s) => { // 绑定成功,`s` 为 `CondSel`
            s
        },
        Err(e) => { // 绑定失败,`e` 为 `SelError`
            return;
        }
    };
    // ...省略...
}

Trait Implementations§

source§

impl Drop for Cond

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for Cond

source§

impl Sync for Cond

Auto Trait Implementations§

§

impl !Freeze for Cond

§

impl !RefUnwindSafe for Cond

§

impl Unpin for Cond

§

impl UnwindSafe for Cond

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.