use crate::{
binding::RuntimeTag,
lifecycle::{RuntimeBinding, RuntimeExternalSource},
mode::{ConcurrentMode, RuntimeMode, SerialMode},
options::RuntimeOptions,
};
use pi_async_executor::{
timer::{DefaultTimerAccess, DefaultTimerDriver},
DefaultExecutor, DefaultIdlePolicy, ExecutorClock, ExecutorHandle, ExecutorHooks,
ExecutorLocal, IdlePolicy, ManualClock, NoHooks, ThreadNotifier,
};
use pi_async_task_queue::{
MpscIdleTaskQueue, MpscIdleTaskQueueHandle, MpscTaskQueue, MpscTaskQueueHandle,
};
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> =
DefaultExecutor<C, RuntimeExternalSource, MpscIdleTaskQueue, Timer<C>, RuntimeTag<C>>;
pub(crate) type Shared<C> =
ExecutorHandle<MpscTaskQueueHandle, MpscIdleTaskQueueHandle, Timer<C>, RuntimeTag<C>>;
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>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
{
pub(crate) shared: RuntimeHandle<C>,
pub(crate) data: RefCell<Option<RuntimeData<M, C, P, H>>>,
pub(crate) binding: Rc<RuntimeBinding>,
}
pub(crate) struct RuntimeData<M, C, P, H>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
{
pub(crate) executor: Executor<C>,
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>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
{
pub(crate) inner: Arc<Shared<C>>,
}
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())
}
pub fn with_options(options: RuntimeOptions) -> pi_result::Result<Self> {
runtime_new_inner(options)
}
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)
}
}
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)
}
pub fn handle(&self) -> RuntimeHandle<C> {
self.shared.clone()
}
pub(crate) fn shared(&self) -> &RuntimeHandle<C> {
&self.shared
}
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>> RuntimeHandle<C> {
pub(crate) fn shared(&self) -> &Self {
self
}
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>> Clone for RuntimeHandle<C> {
fn clone(&self) -> Self {
runtime_handle_clone_inner(self)
}
}
impl<M, C, P, H> Drop for ConfiguredRuntime<M, C, P, H>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
{
fn drop(&mut self) {
runtime_drop_inner(self)
}
}
include!("instance_inner.rs");