xwrust::xwos::sync::br

Struct Br

source
pub struct Br<const N: XwSz>
where [XwBmp; { _ }]: Sized,
{ /* private fields */ }
Expand description

线程栅栏对象结构体

Implementations§

source§

impl<const N: XwSz> Br<N>
where [XwBmp; { _ }]: Sized,

source

pub const fn new() -> Self

新建线程栅栏对象

此方法是编译期方法。

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

static GLOBAL_BR: Br<8> = Br::new();
  • 在heap中创建:
extern crate alloc;
use alloc::sync::Arc;

pub fn xwrust_example_br() {
    let br = Arc::new(Br::<8>::new());
}
source

pub fn init(&self)

初始化线程栅栏对象

线程栅栏对象必须调用此方法一次,方可正常使用。

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

static GLOBAL_BR: Br<8> = Br::new();

pub fn xwrust_example_br() {
    // ...省略...
    GLOBAL_BR.init();
    // 从此处开始 GLOBAL_BR 可正常使用
}
source

pub const fn get_num(&self) -> XwSz

获取线程栅栏中线程槽的数量

source

pub fn wait(&self) -> BrError

等待所有线程到达栅栏

  • 当线程栅栏中的线程数量小于指定数量,线程会阻塞等待。
  • 当线程栅栏中的线程数量达到指定数量,全部线程被唤醒,然后返回 BrError::Ok
  • 当线程阻塞等待被中断时,返回 BrError::Interrupt
§上下文
  • 线程
§错误码
§示例
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();
        });
    }
}
source

pub fn wait_to(&self, to: XwTm) -> BrError

限时等待所有线程到达栅栏

  • 当线程栅栏中的线程数量小于指定数量,线程会阻塞等待,等待时会指定一个唤醒时间点。
  • 当线程栅栏中的线程数量达到指定数量,全部线程被唤醒,然后返回 BrError::Ok
  • 当线程阻塞等待被中断时,返回 BrError::Interrupt
  • 当到达指定的唤醒时间点,线程被唤醒,并返回 BrError::Timedout
§参数说明
  • to: 期望唤醒的时间点
§上下文
  • 线程
§错误码
§示例
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)));
        });
    }
}
source

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

绑定线程栅栏对象到信号选择器

BrSel<'a, N, M> 中包含线程栅栏的绑定信息。 BrSel<'a, N, M>Br<N>Sel<M> 具有相同的生命周期约束 'aBrSel::selected() 可用来判断线程栅栏是否被选择。当 BrSel<'a, N, M> drop() 时,会自动解绑。

§参数说明
  • sel: 信号选择器的引用
  • pos: 位置
§上下文
  • 任意
§错误码
§示例
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;
        }
    };
    // ...省略...
}

Trait Implementations§

source§

impl<const N: XwSz> Drop for Br<N>
where [XwBmp; { _ }]: Sized,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<const N: XwSz> Send for Br<N>
where [XwBmp; { _ }]: Sized,

source§

impl<const N: XwSz> Sync for Br<N>
where [XwBmp; { _ }]: Sized,

Auto Trait Implementations§

§

impl<const N: usize> !Freeze for Br<N>

§

impl<const N: usize> !RefUnwindSafe for Br<N>

§

impl<const N: usize> Unpin for Br<N>
where [usize; { _ }]: Sized,

§

impl<const N: usize> UnwindSafe for Br<N>
where [usize; { _ }]: 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<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.