pi_async_executor/
policy.rs1use crate::registry::TaskRegistryHandle;
3use pi_async_runtime_core::definitions::{IdleState, RuntimeId, RuntimeStatus};
4use std::time::Duration;
5#[derive(Clone, Copy, Debug, Default)]
6pub(crate) struct WorkReadiness {
7 pub external_deferred: bool,
8 pub returns_deferred: bool,
9 pub external_count: usize,
10 pub internal_count: usize,
11}
12
13#[derive(Clone, Copy)]
15pub struct ExecutorSnapshot<'a> {
16 pub runtime: RuntimeId,
17 pub status: RuntimeStatus,
18 pub external_tasks: usize,
20 pub internal_tasks: usize,
21 pub ready_tasks: usize,
23 pub external_deferred: bool,
25 pub returns_deferred: bool,
27 pub timer_registrations: usize,
29 pub idle_tasks: usize,
31 pub in_flight_tasks: usize,
33 pub(crate) registry: &'a TaskRegistryHandle,
34 pub can_admit_external: bool,
36 pub timer_work_pending: bool,
38 pub maintenance_pending: bool,
40 pub last_idle_state: IdleState,
41}
42pub trait IdlePolicy {
45 fn is_idle(&mut self, state: &ExecutorSnapshot<'_>) -> bool {
47 default_idle_inner(state)
48 }
49 fn sleep_duration(&mut self, state: &ExecutorSnapshot<'_>) -> Option<Duration> {
52 default_sleep_inner(state)
53 }
54}
55#[derive(Default, Clone, Copy, Debug)]
57pub struct DefaultIdlePolicy;
58impl IdlePolicy for DefaultIdlePolicy {}
59impl ExecutorSnapshot<'_> {
60 pub fn waiting_for_wake_tasks(&self) -> usize {
62 self.registry.waiting_for_wake_count()
63 }
64 pub fn pending_cancel_tasks(&self) -> usize {
66 self.registry.pending_cancel_count()
67 }
68}
69include!("policy_inner.rs");