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 | if let Some(notified) = notified { |
57 | me.schedule_task(notified, false); |
58 | } |
59 | |
60 | handle |
61 | } |
62 | } |
63 | |
64 | impl fmt::Debug for Handle { |
65 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
66 | fmt.debug_struct(name:"multi_thread::Handle { ... }" ).finish() |
67 | } |
68 | } |
69 | |