Struct AsyncScheduler

Source
pub struct AsyncScheduler { /* private fields */ }
Expand description

异步任务调度器

实现了基于优先级的协作式多任务调度。任务按照优先级和调度间隔被执行, 优先级越高的任务执行频率越高。调度器使用轮询方式处理任务队列, 支持任务的动态优先级调整和新任务的生成。

§调度策略

  • 使用单队列存储所有任务
  • 基于 2^(n+1)-1 序列确定调度间隔
  • 当任务pending时自动降低优先级
  • 支持任务完成时的结果设置

§优先级系统

  • 0: 最高优先级,每步都执行
  • 1: 中等优先级,每3步执行一次
  • 2: 低优先级,每7步执行一次
  • 3: 最低优先级,每15步执行一次

Implementations§

Source§

impl AsyncScheduler

Source

pub fn new(main_task: Task) -> Self

创建一个新的异步调度器

§参数
  • main_task - 主要任务,调度器会首先执行此任务
§返回值

返回新创建的 AsyncScheduler 实例,主任务已添加到队列中

Trait Implementations§

Source§

impl Runnable for AsyncScheduler

Source§

fn step(&mut self, gc: &mut GC<OnionObjectCell>) -> StepResult

执行一个调度步骤

这是调度器的核心方法,负责遍历任务队列并按优先级调度执行任务。 每次调用会增加步数计数器,并检查每个任务是否应该在当前步执行。

§参数
  • gc - 垃圾收集器的可变引用,用于内存管理
§返回值
  • StepResult::Continue - 还有任务需要继续执行
  • StepResult::Return(_) - 所有任务完成,返回主任务的结果
  • StepResult::Error(_) - 执行过程中出现错误
§调度逻辑
  1. 检查队列是否为空,为空则返回主任务结果
  2. 增加步数计数器
  3. 遍历队列中的每个任务
  4. 根据任务优先级和当前步数决定是否执行
  5. 处理任务执行结果:完成、继续、挂起或错误
  6. 动态调整任务优先级和队列位置
Source§

fn receive( &mut self, _step_result: &StepResult, _gc: &mut GC<OnionObjectCell>, ) -> Result<(), RuntimeError>

接收来自其他 Runnable 的结果

AsyncScheduler 不支持接收外部结果,因为它是顶层调度器。 任何尝试向调度器发送结果的操作都会返回错误。

§参数
  • _step_result - 被忽略的步骤结果
  • _gc - 被忽略的垃圾收集器引用
§返回值

总是返回 RuntimeError::DetailedError,表示不支持此操作

Source§

fn format_context(&self) -> String

格式化调度器的当前上下文信息

生成包含调度器状态和所有任务详细信息的格式化字符串, 用于调试和监控调度器的运行状态。

§返回值

返回包含以下信息的格式化字符串:

  • 调度器当前状态(步数、队列长度)
  • 每个任务的详细信息(优先级、下次执行步数、类型)
  • 每个任务内部的上下文信息(缩进显示)
§输出格式
-> AsyncScheduler Status:
   - Current Step: 42
   - Total Tasks in Queue: 3
--- Task Queue Details ---
  [Task #0] Priority: 1 (Next run at step 45), Type: SomeRunnable
    ... (task internal context)
  [Task #1] Priority: 0 (Next run at step 43), Type: AnotherRunnable
    ... (task internal context)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.