use crate::{
external_binding::{ExternalAccess, LocalTaskQueue, LocalTaskSourceAccess},
local::ExecutorLocal,
optional_queues::{IdleAccess, TimerBinding},
parking::ThreadNotifier,
registry::TaskRegistryHandle,
routing::{ExecutorSchedule, LocalReadySubmit},
state::ExecutorState,
timer::ExecutorTimer,
};
use pi_async_runtime_core::{
control::RuntimeControl,
definitions::*,
query::RuntimeQuery,
task::*,
time::{TimedTaskNotifier, TimerAccess},
};
use pi_async_task::{definitions::TaskOwnerId, LocalOutput};
use pi_async_task_queue::{
idle::IdleTaskQueue, ExternalTaskQueue, InternalTaskQueue,
};
#[cfg(not(target_arch = "wasm32"))]
use pi_async_task_queue::{
ExternalPushError, ExternalPushErrorKind, QueueSelection, SerialPublication, TaskWeight,
WeightedSerialParQueueHandle,
};
use std::{future::Future, marker::PhantomData, sync::Arc, time::Duration};
pub struct ExecutorHandle<
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
> {
pub(crate) factory: TaskFactory<ExecutorSchedule<L>, ThreadNotifier>,
pub(crate) external: E,
pub(crate) idle: I,
pub(crate) timer: T::SharedAccess,
pub(crate) timer_query: T::SharedQuery,
pub(crate) state: Arc<ExecutorState>,
pub(crate) registry: TaskRegistryHandle,
pub(crate) notifier: ThreadNotifier,
pub(crate) owner: TaskOwnerId,
pub(crate) types: PhantomData<fn() -> T>,
}
struct SourceNotifyGuard<'a> {
notifier: &'a ThreadNotifier,
armed: bool,
}
#[cfg(not(target_arch = "wasm32"))]
enum WeightedSubmission<'a> {
Parallel(TaskWeight),
Serial(SerialPublication<'a>),
}
impl Drop for SourceNotifyGuard<'_> {
fn drop(&mut self) {
source_notify_drop_inner(self)
}
}
impl<
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
> Clone for ExecutorHandle<E, I, T, L>
{
fn clone(&self) -> Self {
handle_clone_inner(self)
}
}
impl<
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
> ExecutorHandle<E, I, T, L>
{
pub fn create<F, M>(
&self,
future: F,
config: TaskConfig<M>,
) -> pi_result::RawResult<
(UnsubmittedTask, TaskHandle<ThreadNotifier, F::Output, M>),
CreateError<F, M>,
>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
handle_create_inner(self, future, config)
}
pub fn submit(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError>
where
E: ExternalTaskQueue,
{
handle_submit_inner(self, task)
}
pub fn submit_idle(&self, task: UnsubmittedTask) -> pi_result::RawResult<(), SubmitError>
where
I: IdleTaskQueue,
{
handle_submit_idle_inner(self, task)
}
fn submit_to_source(
&self,
task: UnsubmittedTask,
mode: SubmissionMode,
closed: bool,
closed_cause: SubmitErrorKind,
accept: impl FnOnce(pi_async_task::Runnable)
-> pi_result::RawResult<(), (pi_async_task::Runnable, SubmitErrorKind)>,
) -> pi_result::RawResult<(), SubmitError> {
handle_submit_to_source_inner(self, task, mode, closed, closed_cause, accept)
}
pub fn create_local<F, M>(
&self,
local: &ExecutorLocal<T, impl InternalTaskQueue>,
future: F,
config: TaskConfig<M>,
) -> pi_result::RawResult<
(
UnsubmittedTask,
TaskHandle<ThreadNotifier, F::Output, M, LocalOutput>,
),
CreateError<F, M>,
>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
handle_create_local_inner(self, local, future, config)
}
pub fn submit_local(
&self,
local: &ExecutorLocal<T, impl InternalTaskQueue>,
task: UnsubmittedTask,
) -> pi_result::RawResult<(), SubmitError> {
handle_submit_local_inner(self, local, task, false)
}
pub fn submit_local_front(
&self,
local: &ExecutorLocal<T, impl InternalTaskQueue>,
task: UnsubmittedTask,
) -> pi_result::RawResult<(), SubmitError> {
handle_submit_local_inner(self, local, task, true)
}
pub fn spawn_timing<F, M>(
&self,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> pi_result::RawResult<
TaskHandle<TimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M>,
SpawnError<F, TimedTaskNotifier<ThreadNotifier, T::Access>, M>,
>
where
T: ExecutorTimer,
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
handle_timed_inner(self, future, delay, config)
}
pub fn spawn_timing_local<F, M>(
&self,
local: &ExecutorLocal<T, impl InternalTaskQueue>,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> pi_result::RawResult<
TaskHandle<TimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M, LocalOutput>,
SpawnError<F, TimedTaskNotifier<ThreadNotifier, T::Access>, M, LocalOutput>,
>
where
T: ExecutorTimer,
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
handle_timed_local_inner(self, local, future, delay, config)
}
}
impl<E, I, T, L> ExecutorHandle<E, I, T, L>
where
E: ExternalAccess,
I: IdleAccess,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
{
pub fn timer_access(&self) -> T::Access {
self.timer.clone()
}
}
impl<
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
> RuntimeQuery for ExecutorHandle<E, I, T, L>
{
fn id(&self) -> RuntimeId {
self.factory.runtime_id()
}
fn status(&self) -> RuntimeStatus {
self.state.status()
}
fn is_driving(&self) -> bool {
self.state.is_driving()
}
fn last_idle_state(&self) -> IdleState {
self.state.last_idle_state()
}
fn close_state(&self) -> CloseState {
self.state.close_state()
}
fn external_task_count(&self) -> usize {
self.external.external_count()
}
fn internal_task_count(&self) -> usize {
handle_internal_count_inner(self)
}
fn timer_registration_count(&self) -> usize {
T::timer_count(&self.timer_query)
}
fn idle_task_count(&self) -> usize {
self.idle.idle_count()
}
fn in_flight_task_count(&self) -> usize {
handle_in_flight_inner(self)
}
fn waiting_for_wake_count(&self) -> usize {
self.registry.waiting_for_wake_count()
}
fn pending_cancel_task_count(&self) -> usize {
self.registry.pending_cancel_count()
}
}
impl<
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
> RuntimeControl for ExecutorHandle<E, I, T, L>
{
fn request_close(&self) -> CloseRequestStatus {
self.state.request_close(&self.notifier)
}
}
impl<I, T, L> ExecutorHandle<LocalTaskSourceAccess, I, T, L>
where
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
{
#[doc(hidden)]
pub fn submit_same_task_queue(
&self,
local: &ExecutorLocal<T, LocalTaskQueue>,
task: UnsubmittedTask,
) -> pi_result::RawResult<(), SubmitError> {
handle_submit_same_task_queue_inner(self, local, task)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl<I, T, L> ExecutorHandle<WeightedSerialParQueueHandle, I, T, L>
where
I: IdleAccess,
T: TimerBinding,
L: LocalReadySubmit,
{
pub fn spawn_with_queue<F>(
&self,
future: F,
queue: QueueSelection,
) -> pi_result::RawResult<TaskHandle<ThreadNotifier, F::Output>, SpawnError<F, ThreadNotifier>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
handle_spawn_with_queue_inner(self, future, queue)
}
}
include!("handle_inner.rs");
include!("member_executor_access.rs");