Skip to main content

MultiTaskRuntimeBuilder

Struct MultiTaskRuntimeBuilder 

Source
pub struct MultiTaskRuntimeBuilder<O: Default + 'static = (), P: AsyncTaskPoolExt<O> + AsyncTaskPool<O> = StealableTaskPool<O>> { /* private fields */ }
Expand description

异步多线程任务运行时构建器

Implementations§

Source§

impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O, Pool = P>> MultiTaskRuntimeBuilder<O, P>

Source

pub fn new(pool: P) -> Self

构建指定任务池、线程名前缀、初始线程数量、最少线程数量、最大线程数量、线程栈大小、线程空闲时最长休眠时间和是否使用本地定时器的多线程任务池

Source

pub fn thread_prefix(self, prefix: &str) -> Self

设置工作者线程名称前缀

Source

pub fn thread_stack_size(self, stack_size: usize) -> Self

设置工作者线程栈大小

Source

pub fn init_worker_size(self, init: usize) -> Self

设置初始工作者数量

Source

pub fn set_worker_limit(self, min: usize, max: usize) -> Self

设置最小工作者数量和最大工作者数量

Source

pub fn set_timeout(self, timeout: u64) -> Self

设置工作者空闲时最大休眠时长

Source

pub fn set_timer_interval(self, interval: usize) -> Self

设置工作者定时器间隔

Source

pub fn build(self) -> MultiTaskRuntime<O, P>

构建并启动多线程异步运行时。

说明:

  • 该函数消费 builder,创建 runtime、定时器、waiting worker 队列,并启动初始 worker 线程。
  • 本轮保持公开 API 和启动流程不变,只在构建期增加 worker 数边界收敛,并确保 任务池保存 runtime 共享 waits 队列。

入参:

  • 使用 builder 中已经配置好的 pool、线程名前缀、栈大小、worker 数、sleep timeout 和 timer interval。

返回:

  • 已启动的 MultiTaskRuntime<O, P>

边界条件:

  • 如果 pool 的 worker_len() 为 0,立即 panic;有效任务池不允许没有 worker slot。
  • 如果 init/max 大于 pool worker slot 数,会收敛到 pool.worker_len()
  • 如果收敛后 min > max,会把 min 收敛到 max
  • 上述收敛只避免内部 worker slot 越界,不改变已存在的公开方法签名。

性能:

  • 构建时间 O(W),空间 O(W),W 为最终 max worker 数。
  • 该函数不是任务调度热路径。

副作用:

  • 非纯函数,会分配 runtime 内部结构、注入 waits 队列、启动 worker 线程。
  • 不执行用户 future;worker 启动后由工作循环正常消费任务。

安全性:

  • 不引入新的 unsafe。
  • 线程安全依赖 AsyncTaskPoolExt::set_waits 在 pool 被放入 Arc 前完成,之后 waits 通过 Arc<ArrayQueue<...>> 在线程间共享。

Trait Implementations§

Source§

impl<O: Default + 'static> Default for MultiTaskRuntimeBuilder<O>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Send for MultiTaskRuntimeBuilder<O, P>

Source§

impl<O: Default + 'static, P: AsyncTaskPoolExt<O> + AsyncTaskPool<O>> Sync for MultiTaskRuntimeBuilder<O, P>

Auto Trait Implementations§

§

impl<O, P> Freeze for MultiTaskRuntimeBuilder<O, P>
where P: Freeze,

§

impl<O, P> RefUnwindSafe for MultiTaskRuntimeBuilder<O, P>

§

impl<O, P> Unpin for MultiTaskRuntimeBuilder<O, P>
where P: Unpin, O: Unpin,

§

impl<O, P> UnsafeUnpin for MultiTaskRuntimeBuilder<O, P>
where P: UnsafeUnpin,

§

impl<O, P> UnwindSafe for MultiTaskRuntimeBuilder<O, P>
where P: UnwindSafe, O: UnwindSafe,

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ThreadSend for T
where T: Send,

Source§

impl<T> ThreadSync for T
where T: Sync + Send,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V