use pi_async_task_queue::InternalTaskQueue;
use pi_async_executor::{ExternalAccess, IdleAccess, IdleBinding, TimerBinding};
use crate::{
binding::RuntimeTag,
lifecycle::{LocalSource, RuntimeBinding, RuntimeExternalSource, RuntimeSourceBinding},
mode::{QueuePair, ConcurrentMode, RuntimeMode, SerialMode},
options::RuntimeOptions,
queues::{RuntimeInternalFactory, RuntimeQueues, RuntimeTimerFactory},
};
use pi_async_executor::{
timer::{DefaultTimerAccess, DefaultTimerDriver},
DefaultExecutor, DefaultIdlePolicy, ExecutorClock, ExecutorHandle, ExecutorHooks,
ExecutorLocal, IdlePolicy, LocalTaskQueue, LocalTaskSourceAccess, ManualClock, NoHooks,
ThreadNotifier,
};
use pi_async_task_queue::{
CancelTimerOptions, ExternalQueueManager, ExternalTaskQueue,
MpscIdleTaskQueue, MpscIdleTaskQueueHandle, MpscTaskQueue, MpscTaskQueueHandle, TaskQueue,
};
use pi_async_runtime_core::{RuntimeId, SubmitError};
#[cfg(not(target_arch = "wasm32"))]
use pi_async_task_queue::{QueueSelection, WeightedSerialParQueueHandle};
use pi_clock_manual::global::GlobalManualInstant;
use std::{cell::RefCell, rc::Rc, sync::Arc};
pub(crate) type Timer<C, Q = TaskQueue> = DefaultTimerDriver<C, RuntimeTag<C, (), Q>>;
pub(crate) type Executor<C, E, I, T, Q = TaskQueue> = DefaultExecutor<C, <E as RuntimeSourceBinding>::Source, I, T, RuntimeTag<C, T, Q>, Q>;
pub(crate) type Shared<C, E, I, T, Q = TaskQueue> = ExecutorHandle<E, I, T, RuntimeTag<C, T, Q>>;
pub type RuntimeTimerAccess<C = ManualClock, Q = TaskQueue> = DefaultTimerAccess<C, RuntimeTag<C, (), Q>>;
pub type TaskHandle<T, V = ()> = pi_async_runtime_core::task::TaskHandle<ThreadNotifier, T, V>;
pub type LocalTaskHandle<T, V = ()> =
pi_async_runtime_core::task::TaskHandle<ThreadNotifier, T, V, pi_async_task::LocalOutput>;
pub type TimedTaskHandle<T, V = (), C = ManualClock, Q = TaskQueue> = pi_async_runtime_core::task::TaskHandle<
pi_async_runtime_core::time::TimedTaskNotifier<ThreadNotifier, RuntimeTimerAccess<C, Q>>,
T,
V,
>;
pub type TimedTaskNotifier<C = ManualClock, Q = TaskQueue> =
pi_async_runtime_core::time::TimedTaskNotifier<ThreadNotifier, RuntimeTimerAccess<C, Q>>;
pub struct ConfiguredRuntime<M, C, P, H, E = MpscTaskQueue, I = MpscIdleTaskQueue, T = Timer<C>, Q = TaskQueue>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: RuntimeSourceBinding,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
pub(crate) shared: RuntimeHandle<C, E::Handle, I::Handle, T, Q>,
pub(crate) data: RefCell<Option<RuntimeData<M, C, P, H, E, I, T, Q>>>,
pub(crate) binding: Rc<RuntimeBinding>,
}
pub(crate) struct RuntimeData<M, C, P, H, E, I, T, Q>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: RuntimeSourceBinding,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
pub(crate) executor: Executor<C, E, I, T, Q>,
pub(crate) mode: M,
pub(crate) policy: P,
pub(crate) hooks: H,
}
pub type Handle = RuntimeHandle<ManualClock>;
pub type Runtime = ConfiguredRuntime<ConcurrentMode, ManualClock, DefaultIdlePolicy, NoHooks>;
pub struct RuntimeHandle<C, E = MpscTaskQueueHandle, I = MpscIdleTaskQueueHandle, T = Timer<C>, Q = TaskQueue>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
pub(crate) inner: Arc<Shared<C, E, I, T, Q>>,
}
pub type SerialRuntime = ConfiguredRuntime<SerialMode, ManualClock, DefaultIdlePolicy, NoHooks>;
pub type ConcurrentRuntime =
ConfiguredRuntime<ConcurrentMode, ManualClock, DefaultIdlePolicy, NoHooks>;
pub(crate) type LocalService<I, T> = ConfiguredRuntime<
ConcurrentMode, ManualClock, DefaultIdlePolicy, NoHooks, LocalSource, I, T, LocalTaskQueue,
>;
impl<I, T> LocalService<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
pub(crate) fn from_local_executor(
executor: Executor<ManualClock, LocalSource, I, T, LocalTaskQueue>,
binding: Rc<RuntimeBinding>,
) -> Self {
Self::from_executor(executor, binding, DefaultIdlePolicy, NoHooks)
}
}
impl<M: RuntimeMode> ConfiguredRuntime<M, ManualClock, DefaultIdlePolicy, NoHooks> {
pub fn new() -> pi_result::Result<Self> {
runtime_new_inner(RuntimeOptions::default(), RuntimeQueues::default())
}
pub fn with_options(options: RuntimeOptions) -> pi_result::Result<Self> {
runtime_new_inner(options, RuntimeQueues::default())
}
pub fn with_components<C, P, H>(
clock: C,
policy: P,
hooks: H,
options: RuntimeOptions,
) -> pi_result::Result<ConfiguredRuntime<M, C, P, H>>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
{
runtime_components_inner(|| Ok(clock), policy, hooks, options, RuntimeQueues::default())
}
pub fn with_queues<E, I, F, T, Q, B>(
options: RuntimeOptions,
queues: RuntimeQueues<ManualClock, E, I, F, B>,
) -> pi_result::Result<ConfiguredRuntime<M, ManualClock, DefaultIdlePolicy, NoHooks, E, I, T, Q>>
where
M: QueuePair<E, Q>,
E: ExternalQueueManager,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
F: RuntimeTimerFactory<ManualClock, T, Q>,
Q: InternalTaskQueue + 'static,
B: RuntimeInternalFactory<Q>,
{
runtime_new_inner(options, queues)
}
pub fn with_components_and_queues<C, P, H, E, I, F, T, Q, B>(
clock: C,
policy: P,
hooks: H,
options: RuntimeOptions,
queues: RuntimeQueues<C, E, I, F, B>,
) -> pi_result::Result<ConfiguredRuntime<M, C, P, H, E, I, T, Q>>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
M: QueuePair<E, Q>,
E: ExternalQueueManager,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
F: RuntimeTimerFactory<C, T, Q>,
Q: InternalTaskQueue + 'static,
B: RuntimeInternalFactory<Q>,
{
runtime_components_inner(|| Ok(clock), policy, hooks, options, queues)
}
}
impl<M, C, P, H> ConfiguredRuntime<M, C, P, H>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
{
pub fn from_components(
clock: C,
policy: P,
hooks: H,
options: RuntimeOptions,
) -> pi_result::Result<Self> {
runtime_components_inner(|| Ok(clock), policy, hooks, options, RuntimeQueues::default())
}
}
impl<M, C, P, H, E, I, T, Q> ConfiguredRuntime<M, C, P, H, E, I, T, Q>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: RuntimeSourceBinding,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
pub fn handle(&self) -> RuntimeHandle<C, E::Handle, I::Handle, T, Q> {
self.shared.clone()
}
pub(crate) fn shared(&self) -> &RuntimeHandle<C, E::Handle, I::Handle, T, Q> {
&self.shared
}
fn from_executor(
executor: Executor<C, E, I, T, Q>,
binding: Rc<RuntimeBinding>,
policy: P,
hooks: H,
) -> Self {
runtime_from_executor_inner(executor, binding, policy, hooks)
}
}
impl<C, E, I, T, Q> RuntimeHandle<C, E, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
pub(crate) fn shared(&self) -> &Self {
self
}
pub(crate) fn create_task<F, M>(
&self,
future: F,
config: TaskConfig<M>,
) -> pi_result::RawResult<(UnsubmittedTask, TaskHandle<F::Output, M>), CreateError<F, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
self.inner.create(future, config)
}
pub(crate) fn submit_idle_task(
&self,
task: UnsubmittedTask,
) -> pi_result::RawResult<(), SubmitError>
where
I: IdleTaskQueue,
{
self.inner.submit_idle(task)
}
pub(crate) fn spawn_timing_task<F, M>(
&self,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> pi_result::RawResult<
pi_async_runtime_core::TaskHandle<CoreTimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M>,
SpawnError<F, CoreTimedTaskNotifier<ThreadNotifier, T::Access>, M>,
>
where
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
self.inner.spawn_timing(future, delay, config)
}
pub(crate) fn spawn_timing_local_task<F, M>(
&self,
local: &ExecutorLocal<T, Q>,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> pi_result::RawResult<
pi_async_runtime_core::TaskHandle<
CoreTimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M, pi_async_task::LocalOutput,
>,
SpawnError<F, CoreTimedTaskNotifier<ThreadNotifier, T::Access>, M, pi_async_task::LocalOutput>,
>
where
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
self.inner.spawn_timing_local(local, future, delay, config)
}
}
impl<C, I, T> RuntimeHandle<C, LocalTaskSourceAccess, I, T, LocalTaskQueue>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
pub(crate) fn submit_same_task_queue(
&self,
local: &ExecutorLocal<T, LocalTaskQueue>,
task: UnsubmittedTask,
) -> pi_result::RawResult<(), SubmitError> {
self.inner.submit_same_task_queue(local, task)
}
}
impl<C, E, I, T, Q> Clone for RuntimeHandle<C, E, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
fn clone(&self) -> Self {
runtime_handle_clone_inner(self)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl<C, I, T, Q> RuntimeHandle<C, WeightedSerialParQueueHandle, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
pub fn spawn_with_queue<F>(
&self,
future: F,
queue: QueueSelection,
) -> pi_result::RawResult<TaskHandle<F::Output>, SpawnError<F, ThreadNotifier>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.inner.spawn_with_queue(future, queue)
}
}
impl<M, C, P, H, E, I, T, Q> Drop for ConfiguredRuntime<M, C, P, H, E, I, T, Q>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: RuntimeSourceBinding,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
fn drop(&mut self) {
runtime_drop_inner(self)
}
}
include!("instance_inner.rs");
include!("prepared_serial_runtime.rs");