1 | use crate::future::Future; |
2 | use crate::loom::sync::Arc; |
3 | use crate::runtime::scheduler::multi_thread::worker; |
4 | use crate::runtime::{ |
5 | blocking, driver, |
6 | task::{self, JoinHandle}, |
7 | }; |
8 | use crate::util::RngSeedGenerator; |
9 | |
10 | use std::fmt; |
11 | |
12 | cfg_metrics! { |
13 | mod metrics; |
14 | } |
15 | |
16 | cfg_taskdump! { |
17 | mod taskdump; |
18 | } |
19 | |
20 | /// Handle to the multi thread scheduler |
21 | pub(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 | |
35 | impl 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 | |
62 | cfg_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 | |
72 | impl 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 | |