use crate::{
external_binding::{ExternalAccessOperations, LocalTaskQueue, LocalTaskSourceAccess},
optional_queues::TimerBinding, 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, InternalTaskQueue, QueuePushError, TaskQueue};
use std::{
cell::{Cell, RefCell},
collections::{HashMap, VecDeque},
rc::Rc,
sync::Arc,
};
pub struct ExecutorLocal<T: TimerBinding, Q: InternalTaskQueue = TaskQueue> {
pub(crate) runtime: RuntimeId,
pub(crate) owner_id: TaskOwnerId,
pub(crate) owner: RefCell<Option<TaskThread>>,
pub(crate) ready: RefCell<Option<Q>>,
local_external: Option<LocalTaskSourceAccess>,
pub(crate) timer: T::LocalTimer,
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,
register_pool_tasks: bool,
close_ready: bool,
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: TimerBinding, Q: InternalTaskQueue> {
local: &'a ExecutorLocal<T, Q>,
task: &'a Runnable,
armed: bool,
}
impl<T: ExecutorTimer + 'static, Q: InternalTaskQueue> ExecutorLocal<T, Q> {
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)
}
}
impl<T: TimerBinding, Q: InternalTaskQueue> Drop for AdmissionAttemptGuard<'_, T, Q> {
fn drop(&mut self) {
admission_attempt_drop_inner(self)
}
}
enum LocalExitPhase {
Owner,
Ready,
Timer,
Returns,
Active,
Registry,
Finish,
Done,
}
struct LocalExitGuard<'a, T, Q, F>
where
T: TimerBinding,
Q: InternalTaskQueue,
F: FnOnce() -> pi_result::Result<()>,
{
local: &'a ExecutorLocal<T, Q>,
phase: LocalExitPhase,
current_failed: bool,
failed: bool,
domain_unconfirmed: usize,
admitted_unconfirmed: usize,
close_report: Option<&'a mut Option<pi_result::Error>>,
finish_external: Option<F>,
}
struct LocalActivityGuard<'a, T: TimerBinding, Q: InternalTaskQueue> {
local: &'a ExecutorLocal<T, Q>,
}
struct ReadyReleaseGuard<'a> {
state: &'a ExecutorState,
count: usize,
local_external: Option<&'a LocalTaskSourceAccess>,
}
impl<T: TimerBinding, Q: InternalTaskQueue> ExecutorLocal<T, Q> {
pub(crate) fn new_with_internal_queue(
runtime: RuntimeId,
owner: TaskThread,
ready: Q,
timer: T::LocalTimer,
returns: ReturnQueue,
registry: TaskRegistry,
state: Arc<ExecutorState>,
notifier: ThreadNotifier,
close_ready: bool,
local_external: Option<LocalTaskSourceAccess>,
) -> Self {
local_new_inner(
runtime, owner, ready, timer, returns, registry, state, notifier, close_ready, local_external,
)
}
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(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 needs_external_stop(&self) -> bool {
!self.exit_requested.get()
}
pub(crate) fn finish_exit<F: FnOnce() -> pi_result::Result<()>>(
&self,
source_failed: bool,
close_report: Option<&mut Option<pi_result::Error>>,
finish_external: Option<F>,
) -> bool {
local_finish_exit_inner(self, source_failed, close_report, finish_external)
}
}
impl<T: TimerBinding, Q: InternalTaskQueue> Drop for ExecutorLocal<T, Q> {
fn drop(&mut self) {
local_drop_inner(self)
}
}
impl<T: TimerBinding, Q: InternalTaskQueue> Drop for LocalActivityGuard<'_, T, Q> {
fn drop(&mut self) {
local_activity_drop_inner(self)
}
}
impl Drop for ReadyReleaseGuard<'_> {
fn drop(&mut self) {
ready_release_inner(self)
}
}
impl<T, Q, F> LocalExitGuard<'_, T, Q, F>
where
T: TimerBinding,
Q: InternalTaskQueue,
F: FnOnce() -> pi_result::Result<()>,
{
fn drain(&mut self) {
local_exit_drain_inner(self)
}
fn close_internal_queue(&mut self, queue: &mut Q, first_error: &mut Option<pi_result::Error>) {
local_close_internal_queue_inner(self, queue, first_error)
}
}
impl<T, Q, F> Drop for LocalExitGuard<'_, T, Q, F>
where
T: TimerBinding,
Q: InternalTaskQueue,
F: FnOnce() -> pi_result::Result<()>,
{
fn drop(&mut self) {
local_exit_drop_inner(self)
}
}
impl<T: TimerBinding> ExecutorLocal<T, LocalTaskQueue> {
pub(crate) fn submit_ordinary(
&self,
task: Runnable,
) -> pi_result::RawResult<(), QueuePushError> {
local_submit_ordinary_inner(self, task)
}
}
include!("local_inner.rs");
include!("member_executor_selection.rs");