use pi_async_task_queue::InternalTaskQueue;
use pi_async_executor::{ExternalAccess, IdleAccess, IdleBinding, TimerBinding};
use pi_async_executor::ExecutorTimer;
use crate::lifecycle::RuntimeSourceBinding;
use crate::{instance::*, mode::RuntimeMode};
use pi_async_executor::{ExecutorClock, ExecutorHooks, IdlePolicy};
use pi_async_runtime_core::{
CloseRequestStatus, CloseState, IdleState, LocalRuntimeAccessError, RuntimeControl, RuntimeId,
RuntimeQuery, RuntimeStatus, RuntimeTime, RuntimeWait, Sleep, Timeout, TimeoutOn,
};
use pi_clock_manual::global::GlobalManualInstant;
use pi_result::RawResult;
use std::{future::Future, time::Duration};
fn local_timer_access<C, E, I, T, Q>() -> RawResult<T::Access, LocalRuntimeAccessError>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
Q: InternalTaskQueue + 'static,
{
crate::access::with_local::<C, E, I, T, Q, _>(|handle, _| handle.shared().inner.timer_access())
}
impl<C, E, I, T, Q> RuntimeTime for RuntimeHandle<C, E, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
Q: InternalTaskQueue + 'static,
{
type Timer = T::Access;
fn timeout(&self, duration: Duration) -> Timeout<Self::Timer> {
Timeout::new(self.shared().inner.timer_access(), duration)
}
fn timeout_on<F: Future>(&self, future: F, duration: Duration) -> TimeoutOn<Self::Timer, F> {
TimeoutOn::new(self.shared().inner.timer_access(), future, duration)
}
fn sleep(&self, duration: Duration) -> Sleep<Self::Timer> {
Sleep::new(self.shared().inner.timer_access(), duration)
}
fn timeout_local(duration: Duration) -> RawResult<Timeout<Self::Timer>, LocalRuntimeAccessError> {
Ok(Timeout::new_local(local_timer_access::<C, E, I, T, Q>()?, duration))
}
fn timeout_on_local<F: Future>(
future: F,
duration: Duration,
) -> RawResult<TimeoutOn<Self::Timer, F>, LocalRuntimeAccessError> {
Ok(TimeoutOn::new_local(local_timer_access::<C, E, I, T, Q>()?, future, duration))
}
fn sleep_local(duration: Duration) -> RawResult<Sleep<Self::Timer>, LocalRuntimeAccessError> {
Ok(Sleep::new_local(local_timer_access::<C, E, I, T, Q>()?, duration))
}
}
impl<C, E, I, T, Q> RuntimeWait for RuntimeHandle<C, E, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
Q: InternalTaskQueue + 'static,
{}
impl<C, E, I, T, Q> RuntimeQuery for RuntimeHandle<C, E, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
fn id(&self) -> RuntimeId {
self.shared().inner.id()
}
fn status(&self) -> RuntimeStatus {
self.shared().inner.status()
}
fn is_driving(&self) -> bool {
self.shared().inner.is_driving()
}
fn last_idle_state(&self) -> IdleState {
self.shared().inner.last_idle_state()
}
fn close_state(&self) -> CloseState {
self.shared().inner.close_state()
}
fn external_task_count(&self) -> usize {
self.shared().inner.external_task_count()
}
fn internal_task_count(&self) -> usize {
self.shared().inner.internal_task_count()
}
fn timer_registration_count(&self) -> usize {
self.shared().inner.timer_registration_count()
}
fn idle_task_count(&self) -> usize {
self.shared().inner.idle_task_count()
}
fn in_flight_task_count(&self) -> usize {
self.shared().inner.in_flight_task_count()
}
fn waiting_for_wake_count(&self) -> usize {
self.shared().inner.waiting_for_wake_count()
}
fn pending_cancel_task_count(&self) -> usize {
self.shared().inner.pending_cancel_task_count()
}
}
impl<C, E, I, T, Q> RuntimeControl for RuntimeHandle<C, E, I, T, Q>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
E: ExternalAccess,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
fn request_close(&self) -> CloseRequestStatus {
self.shared().inner.request_close()
}
}
impl<M, C, P, H, E, I, T, Q> RuntimeTime for ConfiguredRuntime<M, C, P, H, E, I, T, Q>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: RuntimeSourceBinding,
I: IdleBinding,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
Q: InternalTaskQueue + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
type Timer = T::Access;
fn timeout(&self, duration: Duration) -> Timeout<Self::Timer> {
Timeout::new(self.shared().inner.timer_access(), duration)
}
fn timeout_on<F: Future>(&self, future: F, duration: Duration) -> TimeoutOn<Self::Timer, F> {
TimeoutOn::new(self.shared().inner.timer_access(), future, duration)
}
fn sleep(&self, duration: Duration) -> Sleep<Self::Timer> {
Sleep::new(self.shared().inner.timer_access(), duration)
}
fn timeout_local(duration: Duration) -> RawResult<Timeout<Self::Timer>, LocalRuntimeAccessError> {
RuntimeHandle::<C, E::Handle, I::Handle, T, Q>::timeout_local(duration)
}
fn timeout_on_local<F: Future>(
future: F,
duration: Duration,
) -> RawResult<TimeoutOn<Self::Timer, F>, LocalRuntimeAccessError> {
RuntimeHandle::<C, E::Handle, I::Handle, T, Q>::timeout_on_local(future, duration)
}
fn sleep_local(duration: Duration) -> RawResult<Sleep<Self::Timer>, LocalRuntimeAccessError> {
RuntimeHandle::<C, E::Handle, I::Handle, T, Q>::sleep_local(duration)
}
}
impl<M, C, P, H, E, I, T, Q> RuntimeWait for ConfiguredRuntime<M, C, P, H, E, I, T, Q>
where
M: RuntimeMode,
C: ExecutorClock<Instant = GlobalManualInstant>,
E: RuntimeSourceBinding,
I: IdleBinding,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
Q: InternalTaskQueue + 'static,
P: IdlePolicy,
H: ExecutorHooks,
{
}
impl<M, C, P, H, E, I, T, Q> RuntimeQuery 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 id(&self) -> RuntimeId {
self.shared().inner.id()
}
fn status(&self) -> RuntimeStatus {
self.shared().inner.status()
}
fn is_driving(&self) -> bool {
self.shared().inner.is_driving()
}
fn last_idle_state(&self) -> IdleState {
self.shared().inner.last_idle_state()
}
fn close_state(&self) -> CloseState {
self.shared().inner.close_state()
}
fn external_task_count(&self) -> usize {
self.shared().inner.external_task_count()
}
fn internal_task_count(&self) -> usize {
self.shared().inner.internal_task_count()
}
fn timer_registration_count(&self) -> usize {
self.shared().inner.timer_registration_count()
}
fn idle_task_count(&self) -> usize {
self.shared().inner.idle_task_count()
}
fn in_flight_task_count(&self) -> usize {
self.shared().inner.in_flight_task_count()
}
fn waiting_for_wake_count(&self) -> usize {
self.shared().inner.waiting_for_wake_count()
}
fn pending_cancel_task_count(&self) -> usize {
self.shared().inner.pending_cancel_task_count()
}
}
impl<M, C, P, H, E, I, T, Q> RuntimeControl 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 request_close(&self) -> CloseRequestStatus {
self.shared().inner.request_close()
}
}