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