xwrust::xwos::thd

Struct SThd

source
pub struct SThd<const N: XwSz, R>
where [XwStk; N]: Sized, R: Send,
{ /* private fields */ }
Expand description

静态线程对象结构体

此结构体用于创建具有 'static 生命周期的线程对象。

Implementations§

source§

impl<const N: XwSz, R> SThd<N, R>
where [XwStk; N]: Sized, R: Send,

source

pub const fn new(name: &'static str, privileged: bool) -> Self

新建静态线程对象

此方法是编译期方法。

§参数说明
  • name: 线程的名字
  • privileged: 线程的是否具有系统特权
§示例
use xwrust::xwos::thd::*;

// 线程栈:1024 * 4; 线程返回值类型:&str;线程名字:"SThd";是否为特权线程:true;
static STHD: SThd<1024, &str> = SThd::new("SThd", true);
source

pub fn run<'a>(&'static self, f: fn(_: &Self) -> R) -> SThdHandle<'a, N, R>

以线程函数 f 启动静态线程

此方法限定了参数的 [&'static self] ,因此若试图在函数局部定义静态线程, 调用此方法时,编译器会提示错误:生命周期不够长。

此约束的目的是让用户只能在函数外部定义静态线程对象。

§线程函数

线程函数的原型为 fn(&Self) -> R ,线程运行时,系统会以静态线程自身的引用作为参数调用线程函数 f

用户可通过此引用调用线程的方法或访问线程的名字,栈大小等信息。

§返回值

此方法会返回静态线程的句柄 [SThdHandle<'a, N, R>] ,通过此句柄,其他线程可通知此线程退出并获取返回值。

§上下文
  • 线程、中断、中断底半部、空闲任务
§示例
use xwrust::xwos::thd::*;

static STHD: SThd<1024, &str> = SThd::new("SThd", true);
pub fn xwrust_example_sthd() {
    STHD.run(|sthd| { // 子线程
        // 线程功能
        "OK" // 返回值
    });
}
source

pub fn name(&self) -> &str

返回静态线程名字的引用

§上下文
  • 任意
source

pub fn privileged(&self) -> bool

返回静态线程是否具有特权

§上下文
  • 任意
source

pub const fn stack_size(&self) -> XwSz

返回静态线程的栈大小

§上下文
  • 任意
source

pub fn intr(&self) -> XwEr

中断线程的阻塞态和睡眠态

此方法用于中断线程的 阻塞状态睡眠状态

§上下文
  • 线程、中断、中断底半部、空闲任务
source

pub fn quit(&self) -> XwEr

通知静态线程退出

此方法用于向线程设置 退出状态

调用此方法的线程不会等待被设置 退出状态 的线程退出。

此方法可被重复调用,线程的 退出状态 一旦被设置,不可被清除。

§上下文
  • 线程、中断、中断底半部、空闲任务
source

pub fn migrate(&self, cpuid: XwId) -> XwEr

将线程迁移到目标CPU。

Trait Implementations§

source§

impl<const N: XwSz, R> !Send for SThd<N, R>
where [XwStk; N]: Sized, R: Send,

source§

impl<const N: XwSz, R> Sync for SThd<N, R>
where [XwStk; N]: Sized, R: Send,

Auto Trait Implementations§

§

impl<const N: usize, R> !Freeze for SThd<N, R>

§

impl<const N: usize, R> !RefUnwindSafe for SThd<N, R>

§

impl<const N: usize, R> Unpin for SThd<N, R>
where [usize; N]: Sized, R: Unpin,

§

impl<const N: usize, R> UnwindSafe for SThd<N, R>
where [usize; N]: Sized, R: UnwindSafe,

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.