use crate::instance::Timer;
use pi_async_executor::{
routing::LocalReadySubmit, DefaultExecutor, DefaultTimerDriver, ExecutorClock, ExecutorOptions,
ExecutorTimer, IdleBinding, InternalQueueContext, MakeTimer, ManualClock, NoIdleQueue,
NoTimerQueue, ThreadNotifier, TimerBinding,
};
use pi_async_runtime_core::RuntimeId;
use pi_async_task_queue::{
CancelTimerOptions, ExternalQueueManager, IdleQueueManager, InternalTaskQueue, MpscIdleTaskQueue,
MpscQueueOptions, MpscTaskQueue, TaskQueue,
};
use pi_clock_manual::global::GlobalManualInstant;
use std::marker::PhantomData;
pub(crate) enum QueueSelection<Q> {
Default(fn(MpscQueueOptions) -> pi_result::Result<Q>),
Specified(Q),
}
#[doc(hidden)]
pub struct MakeDefaultTimer<C>(PhantomData<fn() -> C>);
#[doc(hidden)]
pub struct DefaultInternalQueue;
pub struct LocalRuntimeQueues<I = NoIdleQueue, F = NoTimerQueue<GlobalManualInstant>> {
idle: I,
timer: F,
}
impl Default for LocalRuntimeQueues {
fn default() -> Self {
Self { idle: NoIdleQueue, timer: NoTimerQueue::default() }
}
}
impl<I, F> LocalRuntimeQueues<I, F> {
pub(crate) fn into_parts(self) -> (I, F) {
(self.idle, self.timer)
}
pub fn with_idle<Q: IdleQueueManager>(self, idle: Q) -> LocalRuntimeQueues<Q, F> {
LocalRuntimeQueues { idle, timer: self.timer }
}
pub fn with_default_timer(self) -> LocalRuntimeQueues<I, MakeDefaultTimer<ManualClock>> {
LocalRuntimeQueues { idle: self.idle, timer: MakeDefaultTimer(PhantomData) }
}
pub fn with_timer<T, G>(
self,
make: G,
) -> LocalRuntimeQueues<
I,
impl FnOnce(RuntimeId, &ManualClock, ThreadNotifier, CancelTimerOptions) -> pi_result::Result<T>,
>
where
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
G: FnOnce(RuntimeId, &ManualClock, ThreadNotifier) -> pi_result::Result<T>,
{
LocalRuntimeQueues {
idle: self.idle,
timer: move |runtime, clock: &ManualClock, notifier, _options| make(runtime, clock, notifier),
}
}
}
pub struct RuntimeQueues<
C: ExecutorClock<Instant = GlobalManualInstant> = ManualClock,
E = MpscTaskQueue,
I = MpscIdleTaskQueue,
F = MakeDefaultTimer<C>,
B = DefaultInternalQueue,
> {
pub(crate) external: QueueSelection<E>,
pub(crate) idle: QueueSelection<I>,
pub(crate) timer: F,
pub(crate) internal: B,
clock: PhantomData<fn() -> C>,
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>> Default for RuntimeQueues<C> {
fn default() -> Self {
Self {
external: QueueSelection::Default(MpscTaskQueue::new),
idle: QueueSelection::Default(MpscIdleTaskQueue::new),
timer: MakeDefaultTimer(PhantomData),
internal: DefaultInternalQueue,
clock: PhantomData,
}
}
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>, E, I, F, B> RuntimeQueues<C, E, I, F, B> {
pub fn with_external<Q: ExternalQueueManager>(self, queue: Q) -> RuntimeQueues<C, Q, I, F, B> {
RuntimeQueues {
external: QueueSelection::Specified(queue),
idle: self.idle,
timer: self.timer,
internal: self.internal,
clock: PhantomData,
}
}
pub fn with_idle<Q: IdleQueueManager>(self, queue: Q) -> RuntimeQueues<C, E, Q, F, B> {
RuntimeQueues {
external: self.external,
idle: QueueSelection::Specified(queue),
timer: self.timer,
internal: self.internal,
clock: PhantomData,
}
}
pub fn with_internal<Q>(
self,
queue: Q,
) -> RuntimeQueues<C, E, I, F, impl RuntimeInternalFactory<Q>>
where
Q: InternalTaskQueue + 'static,
{
self.with_internal_factory(move |_| Ok(queue))
}
pub fn with_internal_factory<Q, G>(
self,
make: G,
) -> RuntimeQueues<C, E, I, F, G>
where
Q: InternalTaskQueue + 'static,
G: for<'owner> FnOnce(InternalQueueContext<'owner>) -> pi_result::Result<Q>,
{
queues_with_internal_factory_inner::<C, E, I, F, B, Q, G>(self, make)
}
pub fn without_idle(self) -> RuntimeQueues<C, E, NoIdleQueue, F, B> {
queues_without_idle_inner(self)
}
pub fn without_timer(self) -> RuntimeQueues<C, E, I, NoTimerQueue<GlobalManualInstant>, B> {
queues_without_timer_inner(self)
}
pub fn with_timer<T, G>(
self,
make: G,
) -> RuntimeQueues<
C,
E,
I,
impl FnOnce(RuntimeId, &C, ThreadNotifier, CancelTimerOptions) -> pi_result::Result<T>,
B,
>
where
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
G: FnOnce(RuntimeId, &C, ThreadNotifier) -> pi_result::Result<T>,
{
RuntimeQueues {
external: self.external,
idle: self.idle,
timer: move |runtime, clock: &C, notifier, _options| make(runtime, clock, notifier),
internal: self.internal,
clock: PhantomData,
}
}
}
#[doc(hidden)]
pub trait RuntimeTimerFactory<C, T, Q: InternalTaskQueue + 'static>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
fn prepare(self, options: CancelTimerOptions) -> impl MakeTimer<C, T>;
}
impl<C, T, F, Q: InternalTaskQueue + 'static> RuntimeTimerFactory<C, T, Q> for F
where
C: ExecutorClock<Instant = GlobalManualInstant>,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
F: FnOnce(RuntimeId, &C, ThreadNotifier, CancelTimerOptions) -> pi_result::Result<T>,
{
fn prepare(self, options: CancelTimerOptions) -> impl MakeTimer<C, T> {
move |id, clock: &C, notifier| self(id, clock, notifier, options)
}
}
impl<C, Q> RuntimeTimerFactory<C, NoTimerQueue<GlobalManualInstant>, Q>
for NoTimerQueue<GlobalManualInstant>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
fn prepare(self, _: CancelTimerOptions) -> impl MakeTimer<C, NoTimerQueue<GlobalManualInstant>> {
self
}
}
impl<C, Q> RuntimeTimerFactory<C, Timer<C, Q>, Q> for MakeDefaultTimer<C>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
Q: InternalTaskQueue + 'static,
{
fn prepare(self, options: CancelTimerOptions) -> impl MakeTimer<C, Timer<C, Q>> {
move |runtime, clock: &C, notifier| DefaultTimerDriver::new(runtime, clock.clone(), notifier, options)
}
}
#[doc(hidden)]
pub trait RuntimeInternalFactory<Q: InternalTaskQueue + 'static> {
fn bind<C, E, I, T, L>(
self,
clock: C,
external: E,
idle: I,
options: ExecutorOptions,
timer: impl MakeTimer<C, T>,
) -> pi_result::Result<DefaultExecutor<C, E, I, T, L, Q>>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleBinding,
T: TimerBinding<ClockInstant = C::Instant>,
L: LocalReadySubmit;
}
impl RuntimeInternalFactory<TaskQueue> for DefaultInternalQueue {
fn bind<C, E, I, T, L>(
self,
clock: C,
external: E,
idle: I,
options: ExecutorOptions,
timer: impl MakeTimer<C, T>,
) -> pi_result::Result<DefaultExecutor<C, E, I, T, L, TaskQueue>>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleBinding,
T: TimerBinding<ClockInstant = C::Instant>,
L: LocalReadySubmit,
{
DefaultExecutor::with_queues(clock, external, idle, options, timer)
}
}
impl<Q, G> RuntimeInternalFactory<Q> for G
where
Q: InternalTaskQueue + 'static,
G: for<'owner> FnOnce(InternalQueueContext<'owner>) -> pi_result::Result<Q>,
{
fn bind<C, E, I, T, L>(
self,
clock: C,
external: E,
idle: I,
options: ExecutorOptions,
timer: impl MakeTimer<C, T>,
) -> pi_result::Result<DefaultExecutor<C, E, I, T, L, Q>>
where
C: ExecutorClock,
E: ExternalQueueManager,
I: IdleBinding,
T: TimerBinding<ClockInstant = C::Instant>,
L: LocalReadySubmit,
{
DefaultExecutor::with_queue_factories(clock, external, idle, self, options, timer)
}
}
include!("queues_inner.rs");