use pi_async_executor::{
parking::PreparedThreadParker, timer::PreparedDefaultTimer, ExecutorTimer, NoIdleQueue,
NoTimerQueue,
};
use pi_async_task_queue::IdleTaskQueue;
use pi_async_executor::handle::ExecutorMemberAccess;
use pi_async_runtime_core::task::{RuntimeIdReservation, TaskNotifier};
use pi_async_runtime_core::{CreateError, SpawnError, TaskConfig, UnsubmittedTask};
use pi_async_runtime_core::time::{TimedTaskNotifier as CoreTimedTaskNotifier, TimerAccess};
use pi_async_task::{LocalOutput, Runnable};
use pi_async_task_queue::idle::IdleSubmitError;
use std::{future::Future, time::Duration};
#[doc(hidden)]
pub struct PreparedSerialRuntime<
T = PreparedDefaultTimer<ManualClock, RuntimeTag<ManualClock>>,
> {
id: RuntimeIdReservation,
parker: PreparedThreadParker,
timer: T,
}
#[doc(hidden)]
pub struct MemberTaskAccess<E, I = MpscIdleTaskQueueHandle, T = Timer<ManualClock>>
where
E: ExternalTaskQueue,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
handle: RuntimeHandle<ManualClock, E, I, T>,
executor: ExecutorMemberAccess<E, I, T, RuntimeTag<ManualClock, T>>,
}
impl PreparedSerialRuntime {
pub fn prepare(clock: ManualClock, options: CancelTimerOptions) -> pi_result::Result<Self> {
prepared_serial_runtime_new_inner(clock, options)
}
pub fn timer_access(&self) -> RuntimeTimerAccess {
prepared_serial_runtime_timer_inner(self)
}
pub fn bind<E, P, H>(self, external: E, policy: P, hooks: H, options: RuntimeOptions,
timed_admission: impl Fn(pi_async_task::definitions::TaskId) -> Option<bool> + 'static)
-> pi_result::Result<(ConfiguredRuntime<SerialMode, ManualClock, P, H, E>, MemberTaskAccess<E::Handle>)>
where E: ExternalQueueManager, P: IdlePolicy, H: ExecutorHooks, {
prepared_serial_bind_inner(self, external, policy, hooks, options, timed_admission, MpscIdleTaskQueue::new)
}
pub fn bind_without_idle<E, P, H>(
self,
external: E,
policy: P,
hooks: H,
options: RuntimeOptions,
timed_admission: impl Fn(pi_async_task::definitions::TaskId) -> Option<bool> + 'static,
) -> pi_result::Result<(
ConfiguredRuntime<SerialMode, ManualClock, P, H, E, NoIdleQueue>,
MemberTaskAccess<E::Handle, NoIdleQueue>,
)>
where
E: ExternalQueueManager, P: IdlePolicy, H: ExecutorHooks,
{
prepared_serial_bind_inner(self, external, policy, hooks, options, timed_admission, |_| Ok(NoIdleQueue))
}
}
impl<T> PreparedSerialRuntime<T> {
pub fn id(&self) -> RuntimeId {
prepared_serial_runtime_id_inner(self)
}
pub fn notifier(&self) -> ThreadNotifier {
prepared_serial_runtime_notifier_inner(self)
}
}
impl PreparedSerialRuntime<NoTimerQueue<GlobalManualInstant>> {
pub fn prepare_without_timer() -> pi_result::Result<Self> {
prepared_serial_runtime_without_timer_inner()
}
pub fn bind_without_timer<E, P, H>(
self, external: E, policy: P, hooks: H, options: RuntimeOptions,
) -> pi_result::Result<(
ConfiguredRuntime<SerialMode, ManualClock, P, H, E, MpscIdleTaskQueue, NoTimerQueue<GlobalManualInstant>>,
MemberTaskAccess<E::Handle, MpscIdleTaskQueueHandle, NoTimerQueue<GlobalManualInstant>>,
)>
where
E: ExternalQueueManager, P: IdlePolicy, H: ExecutorHooks,
{
bind_prepared_member_without_timer(self, external, policy, hooks, options, MpscIdleTaskQueue::new)
}
pub fn bind_without_timer_and_idle<E, P, H>(
self, external: E, policy: P, hooks: H, options: RuntimeOptions,
) -> pi_result::Result<(
ConfiguredRuntime<SerialMode, ManualClock, P, H, E, NoIdleQueue, NoTimerQueue<GlobalManualInstant>>,
MemberTaskAccess<E::Handle, NoIdleQueue, NoTimerQueue<GlobalManualInstant>>,
)>
where
E: ExternalQueueManager, P: IdlePolicy, H: ExecutorHooks,
{
bind_prepared_member_without_timer(self, external, policy, hooks, options, |_| Ok(NoIdleQueue))
}
}
impl<E, I, T> MemberTaskAccess<E, I, T>
where
E: ExternalTaskQueue,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
pub fn owner_id(&self) -> pi_async_task::definitions::TaskOwnerId {
member_owner_id_inner(self)
}
pub fn return_runnable(&self, task: Runnable) -> pi_result::RawResult<(), Runnable> {
member_return_runnable_inner(self, task)
}
pub fn submit_idle(&self, task: Runnable) -> pi_result::RawResult<(), IdleSubmitError>
where I: IdleTaskQueue,
{
member_idle_submit_inner(self, task)
}
pub fn create_local_with_notifier<N, F, M>(&self, notifier: N, future: F, config: TaskConfig<M>)
-> pi_result::RawResult<(UnsubmittedTask, pi_async_runtime_core::TaskHandle<N, F::Output, M, LocalOutput>), CreateError<F, M>>
where N: TaskNotifier, F: Future + 'static, F::Output: 'static, M: Send + Sync + 'static, {
member_create_with_notice_inner(self, notifier, future, config)
}
pub fn spawn_timing_local<N, A, F, M>(&self, notifier: N, access: A, future: F, delay: Duration, config: TaskConfig<M>)
-> pi_result::RawResult<
pi_async_runtime_core::TaskHandle<CoreTimedTaskNotifier<N, A>, F::Output, M, LocalOutput>,
SpawnError<F, CoreTimedTaskNotifier<N, A>, M, LocalOutput>,
>
where T: ExecutorTimer<Instant = GlobalManualInstant>, N: TaskNotifier, A: TimerAccess, F: Future + 'static, F::Output: 'static, M: Send + Sync + 'static, {
member_local_timed_inner(self, notifier, access, future, delay, config)
}
}
fn bind_prepared_member_without_timer<E, P, H, I>(
this: PreparedSerialRuntime<NoTimerQueue<GlobalManualInstant>>,
external: E,
policy: P,
hooks: H,
options: RuntimeOptions,
make_idle: impl FnOnce(pi_async_task_queue::MpscQueueOptions) -> pi_result::Result<I>,
) -> pi_result::Result<(
ConfiguredRuntime<SerialMode, ManualClock, P, H, E, I, NoTimerQueue<GlobalManualInstant>>,
MemberTaskAccess<E::Handle, I::Handle, NoTimerQueue<GlobalManualInstant>>,
)>
where
E: ExternalQueueManager,
I: IdleBinding,
P: IdlePolicy,
H: ExecutorHooks,
{
let (id, parker, source, idle, binding) =
prepared_serial_bind_without_timer_inner(this, external, options.idle_queue, make_idle)?;
let (executor, access) = DefaultExecutor::from_prepared_without_timer(
ManualClock, id, parker, source, idle, options.executor,
)?;
Ok(prepared_serial_finish_bind(executor, access, binding, policy, hooks))
}
fn prepared_serial_finish_bind<E, P, H, I, T>(
executor: Executor<ManualClock, E, I, T>,
executor_access: ExecutorMemberAccess<E::Handle, I::Handle, T, RuntimeTag<ManualClock, T>>,
binding: Rc<RuntimeBinding>,
policy: P,
hooks: H,
) -> (ConfiguredRuntime<SerialMode, ManualClock, P, H, E, I, T>, MemberTaskAccess<E::Handle, I::Handle, T>)
where
E: ExternalQueueManager,
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
P: IdlePolicy,
H: ExecutorHooks,
{
let shared = RuntimeHandle { inner: Arc::new(executor.handle()) };
let access = MemberTaskAccess { handle: shared.clone(), executor: executor_access };
let data = RuntimeData { executor, mode: SerialMode, policy, hooks };
(
ConfiguredRuntime { shared, data: RefCell::new(Some(data)), binding },
access,
)
}
include!("prepared_serial_runtime_inner.rs");