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