use crate::{
parking::ThreadNotifier, registry::TaskRegistry, returns::ReturnQueue, state::ExecutorState,
timer::ExecutorTimer,
};
use pi_async_runtime_core::definitions::RuntimeId;
use pi_async_task::{
definitions::{TaskId, TaskOwnerId},
Runnable, TaskRegistration, TaskThread,
};
use pi_async_task_queue::{timer::TimerSubmitError, QueuePushError, TaskQueue};
use std::{
cell::{Cell, RefCell},
collections::{HashMap, VecDeque},
rc::Rc,
sync::Arc,
};
pub struct ExecutorLocal<T: ExecutorTimer + 'static> {
pub(crate) runtime: RuntimeId,
pub(crate) owner_id: TaskOwnerId,
pub(crate) owner: RefCell<Option<TaskThread>>,
pub(crate) ready: RefCell<Option<TaskQueue>>,
pub(crate) timer: RefCell<Option<T>>,
pub(crate) returns: RefCell<Option<ReturnQueue>>,
pub(crate) registry: RefCell<Option<TaskRegistry>>,
pub(crate) active: RefCell<HashMap<TaskId, ActiveRecord>>,
pub(crate) active_order: RefCell<VecDeque<TaskId>>,
pub(crate) state: Arc<ExecutorState>,
pub(crate) notifier: ThreadNotifier,
exit_requested: Cell<bool>,
exit_running: Cell<bool>,
source_failed: Cell<bool>,
exit_finished: Cell<bool>,
}
pub(crate) enum ActiveRecord {
Registered(Rc<TaskRegistration>),
Interrupted(pi_async_runtime_core::task::TaskQuery),
}
impl ActiveRecord {
fn snapshot(&self) -> pi_async_task::definitions::TaskSnapshot {
match self {
Self::Registered(r) => r.snapshot(),
Self::Interrupted(q) => q.snapshot(),
}
}
}
pub(crate) enum PreparedTask {
Business(Runnable, Rc<TaskRegistration>),
Cleanup(Runnable),
Settled(Runnable),
}
struct AdmissionAttemptGuard<'a, T: ExecutorTimer + 'static> {
local: &'a ExecutorLocal<T>,
task: &'a Runnable,
armed: bool,
}
impl<T: ExecutorTimer + 'static> Drop for AdmissionAttemptGuard<'_, T> {
fn drop(&mut self) {
admission_attempt_drop_inner(self)
}
}
enum LocalExitPhase {
Owner,
Ready,
Timer,
Returns,
Active,
Registry,
Finish,
Done,
}
struct LocalExitGuard<'a, T: ExecutorTimer + 'static> {
local: &'a ExecutorLocal<T>,
phase: LocalExitPhase,
current_failed: bool,
failed: bool,
domain_unconfirmed: usize,
admitted_unconfirmed: usize,
}
struct LocalActivityGuard<'a, T: ExecutorTimer + 'static> {
local: &'a ExecutorLocal<T>,
}
struct ReadyReleaseGuard<'a> {
state: &'a ExecutorState,
count: usize,
}
impl<T: ExecutorTimer + 'static> ExecutorLocal<T> {
pub(crate) fn new(
runtime: RuntimeId,
owner: TaskThread,
ready: TaskQueue,
timer: T,
returns: ReturnQueue,
registry: TaskRegistry,
state: Arc<ExecutorState>,
notifier: ThreadNotifier,
) -> Self {
local_new_inner(
runtime, owner, ready, timer, returns, registry, state, notifier,
)
}
pub fn runtime_id(&self) -> RuntimeId {
self.runtime
}
pub fn with_owner<R>(&self, action: impl FnOnce(&TaskThread) -> R) -> pi_result::Result<R> {
local_owner_inner(self, action)
}
pub fn try_push(
&self,
task: Runnable,
front: bool,
) -> pi_result::RawResult<(), QueuePushError> {
local_push_inner(self, task, front)
}
pub fn try_push_timer(
&self,
deadline: T::Instant,
item: T::Item,
) -> pi_result::RawResult<T::Key, TimerSubmitError<T::Item>> {
local_timer_push_inner(self, deadline, item)
}
pub(crate) fn selected(&self, task: Runnable) -> pi_result::Result<PreparedTask> {
local_selected_inner(self, task)
}
pub(crate) fn maintain(&self, max_nodes: usize) {
local_maintain_inner(self, max_nodes)
}
pub(crate) fn finish_exit(&self, source_failed: bool) -> bool {
local_finish_exit_inner(self, source_failed)
}
}
impl<T: ExecutorTimer + 'static> Drop for ExecutorLocal<T> {
fn drop(&mut self) {
local_drop_inner(self)
}
}
impl<T: ExecutorTimer + 'static> Drop for LocalActivityGuard<'_, T> {
fn drop(&mut self) {
local_activity_drop_inner(self)
}
}
impl Drop for ReadyReleaseGuard<'_> {
fn drop(&mut self) {
ready_release_inner(self)
}
}
impl<T: ExecutorTimer + 'static> LocalExitGuard<'_, T> {
fn drain(&mut self) {
local_exit_drain_inner(self)
}
}
impl<T: ExecutorTimer + 'static> Drop for LocalExitGuard<'_, T> {
fn drop(&mut self) {
local_exit_drop_inner(self)
}
}
include!("local_inner.rs");