pi-async-runtime 0.1.5

单线程串行及并发异步运行时的静态组合
Documentation
//! 单线程权重运行时(single-threaded weighted runtime)。
use crate::{
    ConcurrentMode, ConcurrentRuntime, ConfiguredRuntime, DefaultIdlePolicy, ExecutorClock,
    ExecutorHooks, IdlePolicy, ManualClock, NoHooks, RuntimeOptions, RuntimeQueues, SpawnError,
    TaskHandle,
};
use pi_async_executor::{IdleBinding, ThreadNotifier, TimerBinding};
use pi_async_task_queue::InternalTaskQueue;
use pi_async_task_queue::{QueueSelection, WeightedSerialParQueue};
use pi_clock_manual::global::GlobalManualInstant;
use std::future::Future;

/// 多线程提交、单线程推进的权重运行时(single-threaded weighted runtime)。
///
/// 使用权重队列(weighted queue)作为外部任务队列,绑定独占 `TaskQueue`,
/// 默认提供定时任务队列(timer task queue)和空闲任务队列(idle task queue)。
/// 创建后由所属线程(owner thread)推进。共享句柄(shared handle)可跨线程派发。
/// 普通派发使用并行中档;需要串行队列或其它档位时用 `spawn_with_queue`。
/// `wasm32` 目标不导出本类型。浏览器(browser)可使用 [`ConcurrentRuntime`] 的单轮推进。
///
/// 关闭永久停收并清理任务,原生池分配保留到进程退出。报告指出这项限制,
/// 关闭查询为 `Closed / Requested`。每次构造都会产生一份原生池分配。
// @trace 1
pub type WeightedRuntime = ConfiguredRuntime<
    ConcurrentMode,
    ManualClock,
    DefaultIdlePolicy,
    NoHooks,
    WeightedSerialParQueue,
>;

// @trace 2
impl WeightedRuntime {
    /// 使用默认选项构造,应用须先安装原全局时钟(global clock)。
    pub fn new_weighted() -> pi_result::Result<Self> {
        weighted_runtime_new_inner(RuntimeOptions::default())
    }

    /// 使用指定运行时选项及固定队列。默认 MPSC 的外部队列选项不适用。
    pub fn with_weighted_options(options: RuntimeOptions) -> pi_result::Result<Self> {
        weighted_runtime_new_inner(options)
    }
}

impl<C, P, H, I, T, Q> ConfiguredRuntime<ConcurrentMode, C, P, H, WeightedSerialParQueue, I, T, Q>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    P: IdlePolicy,
    H: ExecutorHooks,
    I: IdleBinding,
    T: TimerBinding<ClockInstant = GlobalManualInstant>,
    Q: InternalTaskQueue + 'static,
{
    /// 通过同一运行时派发原任务,并选择串行队列或并行档位。
    /// 参见同一运行时的共享句柄(shared handle)上的 `spawn_with_queue`。
    pub fn spawn_with_queue<F>(
        &self,
        future: F,
        queue: QueueSelection,
    ) -> pi_result::RawResult<TaskHandle<F::Output>, SpawnError<F, ThreadNotifier>>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        self.shared().spawn_with_queue(future, queue)
    }
}

include!("weighted_runtime_inner.rs");