pub struct Br<const N: XwSz>where
    [XwBmp; { _ }]: Sized,{ /* private fields */ }Expand description
线程栅栏对象结构体
Implementations§
source§impl<const N: XwSz> Br<N>where
    [XwBmp; { _ }]: Sized,
 
impl<const N: XwSz> Br<N>where
    [XwBmp; { _ }]: Sized,
sourcepub fn wait(&self) -> BrError
 
pub fn wait(&self) -> BrError
等待所有线程到达栅栏
- 当线程栅栏中的线程数量小于指定数量,线程会阻塞等待。
- 当线程栅栏中的线程数量达到指定数量,全部线程被唤醒,然后返回 BrError::Ok。
- 当线程阻塞等待被中断时,返回 BrError::Interrupt。
§上下文
- 线程
§错误码
- BrError::Ok没有错误
- BrError::NotInit线程栅栏没有初始化
- BrError::OutOfRange线程数量超出范围
- BrError::Interrupt等待被中断
- BrError::NotThreadContext不在线程上下文内
§示例
extern crate alloc;
use alloc::sync::Arc;
use xwrust::xwos::sync::br::*;
pub fn xwrust_example_br() {
    let br = Arc::new(Br::<8>::new());
    br.init();
    for idx in 0..8 {
        let c = br.clone();
        let _ = thd::spawn(move |_| { // 子线程闭包
            c.wait();
        });
    }
}sourcepub fn wait_to(&self, to: XwTm) -> BrError
 
pub fn wait_to(&self, to: XwTm) -> BrError
限时等待所有线程到达栅栏
- 当线程栅栏中的线程数量小于指定数量,线程会阻塞等待,等待时会指定一个唤醒时间点。
- 当线程栅栏中的线程数量达到指定数量,全部线程被唤醒,然后返回 BrError::Ok。
- 当线程阻塞等待被中断时,返回 BrError::Interrupt。
- 当到达指定的唤醒时间点,线程被唤醒,并返回 BrError::Timedout。
§参数说明
- to: 期望唤醒的时间点
§上下文
- 线程
§错误码
- BrError::Ok没有错误
- BrError::NotInit线程栅栏没有初始化
- BrError::OutOfRange线程数量超出范围
- BrError::Interrupt等待被中断
- BrError::Timedout等待超时
- BrError::NotThreadContext不在线程上下文内
§示例
extern crate alloc;
use alloc::sync::Arc;
use xwrust::xwos::sync::br::*;
pub fn xwrust_example_br() {
    let br = Arc::new(Br::<8>::new());
    br.init();
    for idx in 0..8 {
        let c = br.clone();
        let _ = thd::spawn(move |_| { // 子线程闭包
            c.wait_to(xwtm::ft(xwtm::s(3)));
        });
    }
}sourcepub fn bind<'a, const M: XwSz>(
    &'a self,
    sel: &'a Sel<M>,
    pos: XwSq,
) -> Result<BrSel<'a, N, M>, BrError>where
    [XwBmp; { _ }]: Sized,
 
pub fn bind<'a, const M: XwSz>(
    &'a self,
    sel: &'a Sel<M>,
    pos: XwSq,
) -> Result<BrSel<'a, N, M>, BrError>where
    [XwBmp; { _ }]: Sized,
绑定线程栅栏对象到信号选择器
- 线程栅栏绑定到信号选择器上时,采用 非独占 的方式进行绑定。
- 绑定成功,通过 Ok()返回BrSel<'a, N, M>。
- 如果位置已被其他 同步对象 以 独占 的方式占领,通过 Err()返回BrError::SelPosBusy。
- 当指定的位置超出范围(例如 Sel<M>只有8个位置,用户偏偏要绑定到位置9 ),通过Err()返回BrError::OutOfSelPos。
- 重复绑定,通过 Err()返回BrError::AlreadyBound。
BrSel<'a, N, M> 中包含线程栅栏的绑定信息。 BrSel<'a, N, M> 与 Br<N> 与 Sel<M> 具有相同的生命周期约束 'a 。
BrSel::selected() 可用来判断线程栅栏是否被选择。当 BrSel<'a, N, M> drop() 时,会自动解绑。
§参数说明
- sel: 信号选择器的引用
- pos: 位置
§上下文
- 任意
§错误码
- BrError::OutOfSelPos信号选择器的位置超出范围
- BrError::AlreadyBound线程栅栏已经绑定
- BrError::SelPosBusy信号选择器的位置被占用
§示例
pub fn xwrust_example_sel() {
    // ...省略...
    let br0 = Arc::new(Br::new());
    br0.init();
    let br0sel = match br0.bind(&sel, 0) {
        Ok(s) => { // 绑定成功,`s` 为 `BrSel`
            s
        },
        Err(e) => { // 绑定失败,`e` 为 `SelError`
            return;
        }
    };
    // ...省略...
}