use crate::{
binding::RuntimeTag,
lifecycle::{RuntimeBinding, RuntimeExternalSource},
mode::{ConcurrentMode, RuntimeMode, SerialMode},
options::RuntimeOptions,
queues::RuntimeQueues,
};
use pi_async_executor::{
timer::{DefaultTimerAccess, DefaultTimerDriver},
DefaultExecutor, DefaultIdlePolicy, ExecutorClock, ExecutorHandle, ExecutorHooks,
ExecutorLocal, ExecutorTimer, IdlePolicy, ManualClock, NoHooks, ThreadNotifier,
};
use pi_async_task_queue::{
CancelTimerOptions, ExternalQueueManager, ExternalTaskQueue, IdleQueueManager, IdleTaskQueue,
MpscIdleTaskQueue, MpscIdleTaskQueueHandle, MpscTaskQueue, MpscTaskQueueHandle,
};
use pi_async_runtime_core::RuntimeId;
use pi_clock_manual::global::GlobalManualInstant;
use std::{cell::RefCell, rc::Rc, sync::Arc};
pub(crate) type Timer<C> = DefaultTimerDriver<C, RuntimeTag<C>>;
pub(crate) type Executor<C, E, I, T> = DefaultExecutor<C, RuntimeExternalSource<E>, I, T, RuntimeTag<C, T>>;
pub(crate) type Shared<C, E, I, T> = ExecutorHandle<E, I, T, RuntimeTag<C, T>>;
pub type RuntimeTimerAccess<C = ManualClock> = DefaultTimerAccess<C, RuntimeTag<C>>;
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> = pi_async_runtime_core::task::TaskHandle<
pi_async_runtime_core::time::TimedTaskNotifier<ThreadNotifier, RuntimeTimerAccess<C>>,
T,
V,
>;
pub type TimedTaskNotifier<C = ManualClock> =
pi_async_runtime_core::time::TimedTaskNotifier<ThreadNotifier, RuntimeTimerAccess<C>>;
pub struct ConfiguredRuntime<M, C, P, H, E = MpscTaskQueue, I = MpscIdleTaskQueue, T = Timer<C>>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
pub(crate) shared: RuntimeHandle<C, E::Handle, I::Handle, T>,
pub(crate) data: RefCell<Option<RuntimeData<M, C, P, H, E, I, T>>>,
pub(crate) binding: Rc<RuntimeBinding>,
}
pub(crate) struct RuntimeData<M, C, P, H, E, I, T>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
pub(crate) executor: Executor<C, E, I, T>,
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>>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalTaskQueue,
I: IdleTaskQueue,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
pub(crate) inner: Arc<Shared<C, E, I, T>>,
}
pub type SerialRuntime = ConfiguredRuntime<SerialMode, ManualClock, DefaultIdlePolicy, NoHooks>;
pub type ConcurrentRuntime =
ConfiguredRuntime<ConcurrentMode, ManualClock, 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(clock, policy, hooks, options, RuntimeQueues::default())
}
pub fn with_queues<E, I, F, T>(
options: RuntimeOptions,
queues: RuntimeQueues<ManualClock, E, I, F>,
) -> pi_result::Result<ConfiguredRuntime<M, ManualClock, DefaultIdlePolicy, NoHooks, E, I, T>>
where
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
F: FnOnce(RuntimeId, &ManualClock, ThreadNotifier, CancelTimerOptions) -> pi_result::Result<T>,
{
runtime_new_inner(options, queues)
}
pub fn with_components_and_queues<C, P, H, E, I, F, T>(
clock: C,
policy: P,
hooks: H,
options: RuntimeOptions,
queues: RuntimeQueues<C, E, I, F>,
) -> pi_result::Result<ConfiguredRuntime<M, C, P, H, E, I, T>>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
F: FnOnce(RuntimeId, &C, ThreadNotifier, CancelTimerOptions) -> pi_result::Result<T>,
{
runtime_components_inner(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(clock, policy, hooks, options, RuntimeQueues::default())
}
}
impl<M, C, P, H, E, I, T> ConfiguredRuntime<M, C, P, H, E, I, T>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
pub fn handle(&self) -> RuntimeHandle<C, E::Handle, I::Handle, T> {
self.shared.clone()
}
pub(crate) fn shared(&self) -> &RuntimeHandle<C, E::Handle, I::Handle, T> {
&self.shared
}
}
impl<C, E, I, T> RuntimeHandle<C, E, I, T>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalTaskQueue,
I: IdleTaskQueue,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
pub(crate) fn shared(&self) -> &Self {
self
}
}
impl<C, E, I, T> Clone for RuntimeHandle<C, E, I, T>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalTaskQueue,
I: IdleTaskQueue,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
fn clone(&self) -> Self {
runtime_handle_clone_inner(self)
}
}
impl<M, C, P, H, E, I, T> Drop for ConfiguredRuntime<M, C, P, H, E, I, T>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalQueueManager,
I: IdleQueueManager,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
fn drop(&mut self) {
runtime_drop_inner(self)
}
}
include!("instance_inner.rs");
include!("prepared_serial_runtime.rs");