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