tokio/runtime/scheduler/multi_thread/
handle.rs

1use crate::future::Future;
2use crate::loom::sync::Arc;
3use crate::runtime::scheduler::multi_thread::worker;
4use crate::runtime::{
5    blocking, driver,
6    task::{self, JoinHandle},
7    TaskHooks, TaskMeta,
8};
9use crate::util::RngSeedGenerator;
10
11use std::fmt;
12
13mod metrics;
14
15cfg_taskdump! {
16    mod taskdump;
17}
18
19/// Handle to the multi thread scheduler
20pub(crate) struct Handle {
21    /// Task spawner
22    pub(super) shared: worker::Shared,
23
24    /// Resource driver handles
25    pub(crate) driver: driver::Handle,
26
27    /// Blocking pool spawner
28    pub(crate) blocking_spawner: blocking::Spawner,
29
30    /// Current random number generator seed
31    pub(crate) seed_generator: RngSeedGenerator,
32
33    /// User-supplied hooks to invoke for things
34    pub(crate) task_hooks: TaskHooks,
35}
36
37impl Handle {
38    /// Spawns a future onto the thread pool
39    pub(crate) fn spawn<F>(me: &Arc<Self>, future: F, id: task::Id) -> JoinHandle<F::Output>
40    where
41        F: crate::future::Future + Send + 'static,
42        F::Output: Send + 'static,
43    {
44        Self::bind_new_task(me, future, id)
45    }
46
47    pub(crate) fn shutdown(&self) {
48        self.close();
49    }
50
51    pub(super) fn bind_new_task<T>(me: &Arc<Self>, future: T, id: task::Id) -> JoinHandle<T::Output>
52    where
53        T: Future + Send + 'static,
54        T::Output: Send + 'static,
55    {
56        let (handle, notified) = me.shared.owned.bind(future, me.clone(), id);
57
58        me.task_hooks.spawn(&TaskMeta {
59            id,
60            _phantom: Default::default(),
61        });
62
63        me.schedule_option_task_without_yield(notified);
64
65        handle
66    }
67}
68
69cfg_unstable! {
70    use std::num::NonZeroU64;
71
72    impl Handle {
73        pub(crate) fn owned_id(&self) -> NonZeroU64 {
74            self.shared.owned.id
75        }
76    }
77}
78
79impl fmt::Debug for Handle {
80    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
81        fmt.debug_struct("multi_thread::Handle { ... }").finish()
82    }
83}