use crate::{
clock::ExecutorClock,
drive::*,
handle::ExecutorHandle,
hooks::ExecutorHooks,
local::ExecutorLocal,
parking::{ThreadNotifier, ThreadParker},
policy::{ExecutorSnapshot, IdlePolicy},
registry::TaskRegistry,
returns::ReturnQueue,
routing::{ExecutorSchedule, LocalReadySubmit},
state::ExecutorState,
timer::ExecutorTimer,
};
use pi_async_runtime_core::{
block_on::RootWakeState,
definitions::{RuntimeId, RuntimeStatus},
};
use pi_async_task::{Runnable, TaskThread};
use pi_async_task_queue::{idle::IdleQueueManager, ExternalQueueManager, TaskQueue};
use std::{marker::PhantomData, rc::Rc, sync::Arc, thread::ThreadId};
struct TaskStep {
ran: bool,
deferred_task: Option<pi_async_task::definitions::TaskId>,
}
struct TimerStep {
runs: usize,
pending: bool,
deferred_task: Option<pi_async_task::definitions::TaskId>,
}
#[derive(Clone, Copy, Default)]
struct ImportResult {
deferred: bool,
}
pub struct DefaultExecutor<C, E, I, T, L>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = C::Instant> + 'static,
L: LocalReadySubmit,
{
pub(crate) clock: C,
pub(crate) external: Option<E>,
pub(crate) idle: Option<I>,
pub(crate) local: Rc<ExecutorLocal<T>>,
pub(crate) handle: ExecutorHandle<E::Handle, I::Handle, T, L>,
pub(crate) parker: ThreadParker,
pub(crate) options: ExecutorOptions,
}
pub struct DriveLease {
state: Arc<ExecutorState>,
runtime: RuntimeId,
thread: ThreadId,
local: PhantomData<Rc<()>>,
}
impl Drop for DriveLease {
fn drop(&mut self) {
drive_lease_drop_inner(self)
}
}
struct ExecutorBuildGuard<T: ExecutorTimer + 'static> {
owner: Option<TaskThread>,
ready: Option<TaskQueue>,
timer: Option<T>,
returns: Option<ReturnQueue>,
registry: Option<TaskRegistry>,
}
impl<T: ExecutorTimer + 'static> Drop for ExecutorBuildGuard<T> {
fn drop(&mut self) {
executor_build_drop_inner(self)
}
}
enum ExecutorExitPhase {
External,
Idle,
Local,
Done,
}
struct InternalTransferGuard {
state: Arc<ExecutorState>,
count: usize,
}
impl Drop for InternalTransferGuard {
fn drop(&mut self) {
internal_transfer_drop_inner(self)
}
}
struct ExecutorExitGuard<'a, C, E, I, T, L>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = C::Instant> + 'static,
L: LocalReadySubmit,
{
executor: &'a mut DefaultExecutor<C, E, I, T, L>,
phase: ExecutorExitPhase,
current_failed: bool,
failed: bool,
}
impl<C, E, I, T, L> DefaultExecutor<C, E, I, T, L>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = C::Instant> + 'static,
L: LocalReadySubmit,
{
pub fn new(
clock: C,
external: E,
idle: I,
options: ExecutorOptions,
make_timer: impl FnOnce(RuntimeId, &C, ThreadNotifier) -> pi_result::Result<T>,
) -> pi_result::Result<Self> {
executor_new_inner(clock, external, idle, options, make_timer)
}
pub fn handle(&self) -> ExecutorHandle<E::Handle, I::Handle, T, L> {
self.handle.clone()
}
pub fn local(&self) -> &Rc<ExecutorLocal<T>> {
&self.local
}
pub fn begin_drive(&self) -> DriveLease {
executor_begin_drive_inner(self)
}
pub fn run_once<A: AdmissionRule, P: IdlePolicy, H: ExecutorHooks>(
&mut self,
admission: &A,
policy: &mut P,
hooks: &mut H,
) -> pi_result::Result<TurnResult> {
executor_once_inner(self, admission, policy, hooks)
}
pub fn run<A: AdmissionRule, P: IdlePolicy, H: ExecutorHooks>(
&mut self,
admission: &A,
policy: &mut P,
hooks: &mut H,
) -> pi_result::Result<()> {
executor_run_inner(self, admission, policy, hooks)
}
pub fn turn_with_lease<A: AdmissionRule, P: IdlePolicy, H: ExecutorHooks>(
&mut self,
lease: &DriveLease,
admission: &A,
policy: &mut P,
hooks: &mut H,
) -> pi_result::Result<TurnResult> {
executor_turn_inner(self, lease, admission, policy, hooks)
}
pub fn park_with_lease<A: AdmissionRule>(
&mut self,
lease: &DriveLease,
admission: &A,
last: &TurnResult,
root: Option<&RootWakeState>,
) -> pi_result::Result<()> {
executor_park_inner(self, lease, admission, last, root)
}
pub(crate) fn snapshot(
&self,
admit: bool,
timer_pending: bool,
maintenance: bool,
readiness: crate::policy::WorkReadiness,
) -> ExecutorSnapshot<'_> {
executor_snapshot_inner(self, admit, timer_pending, maintenance, readiness)
}
pub(crate) fn import_returns(&mut self) -> pi_result::Result<ImportResult> {
executor_returns_inner(self)
}
pub(crate) fn import_external<A: AdmissionRule>(
&mut self,
admission: &A,
) -> pi_result::Result<ImportResult> {
executor_external_inner(self, admission)
}
pub(crate) fn service_timers<H: ExecutorHooks>(
&mut self,
hooks: &mut H,
) -> pi_result::Result<TimerStep> {
executor_timers_inner(self, hooks)
}
pub(crate) fn run_task<H: ExecutorHooks>(
&mut self,
task: Runnable,
hooks: &mut H,
) -> pi_result::Result<TaskStep> {
executor_task_inner(self, task, hooks)
}
pub(crate) fn close_service(&mut self) -> pi_result::Result<()> {
executor_close_inner(self)
}
pub(crate) fn prepare_for_queue(&self, task: Runnable) -> pi_result::Result<Option<Runnable>> {
executor_prepare_inner(self, task)
}
}
impl<C, E, I, T, L> Drop for DefaultExecutor<C, E, I, T, L>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = C::Instant> + 'static,
L: LocalReadySubmit,
{
fn drop(&mut self) {
executor_drop_inner(self)
}
}
impl<C, E, I, T, L> ExecutorExitGuard<'_, C, E, I, T, L>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = C::Instant> + 'static,
L: LocalReadySubmit,
{
fn drain(&mut self) {
executor_exit_drain_inner(self)
}
}
impl<C, E, I, T, L> Drop for ExecutorExitGuard<'_, C, E, I, T, L>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = C::Instant> + 'static,
L: LocalReadySubmit,
{
fn drop(&mut self) {
executor_exit_drop_inner(self)
}
}
include!("engine_inner.rs");