tokio/runtime/scheduler/multi_thread_alt/
mod.rs

1//! Multi-threaded runtime
2
3mod counters;
4use counters::Counters;
5
6mod handle;
7pub(crate) use handle::Handle;
8
9mod overflow;
10pub(crate) use overflow::Overflow;
11
12mod idle;
13use self::idle::Idle;
14
15mod stats;
16pub(crate) use stats::Stats;
17
18pub(crate) mod queue;
19
20mod worker;
21use worker::Core;
22pub(crate) use worker::{Context, Shared};
23
24// TODO: implement task dump
25mod trace_mock;
26use trace_mock::TraceStatus;
27
28pub(crate) use worker::block_in_place;
29
30use crate::runtime::{
31    self, blocking,
32    driver::{self, Driver},
33    scheduler, Config,
34};
35use crate::util::RngSeedGenerator;
36
37use std::fmt;
38use std::future::Future;
39
40/// Work-stealing based thread pool for executing futures.
41pub(crate) struct MultiThread;
42
43// ===== impl MultiThread =====
44
45impl MultiThread {
46    pub(crate) fn new(
47        size: usize,
48        driver: Driver,
49        driver_handle: driver::Handle,
50        blocking_spawner: blocking::Spawner,
51        seed_generator: RngSeedGenerator,
52        config: Config,
53    ) -> (MultiThread, runtime::Handle) {
54        let handle = worker::create(
55            size,
56            driver,
57            driver_handle,
58            blocking_spawner,
59            seed_generator,
60            config,
61        );
62
63        (MultiThread, handle)
64    }
65
66    /// Blocks the current thread waiting for the future to complete.
67    ///
68    /// The future will execute on the current thread, but all spawned tasks
69    /// will be executed on the thread pool.
70    pub(crate) fn block_on<F>(&self, handle: &scheduler::Handle, future: F) -> F::Output
71    where
72        F: Future,
73    {
74        crate::runtime::context::enter_runtime(handle, true, |blocking| {
75            blocking.block_on(future).expect("failed to park thread")
76        })
77    }
78
79    pub(crate) fn shutdown(&mut self, handle: &scheduler::Handle) {
80        match handle {
81            scheduler::Handle::MultiThreadAlt(handle) => handle.shutdown(),
82            _ => panic!("expected MultiThread scheduler"),
83        }
84    }
85}
86
87impl fmt::Debug for MultiThread {
88    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
89        fmt.debug_struct("MultiThread").finish()
90    }
91}