use crate::{
dispatch,
instance::LocalService,
lifecycle::RuntimeBinding,
queues::RuntimeTimerFactory,
CloseRequestStatus, CloseState, CreateError, IdleDispatch, IdleState, LocalRuntimeAccessError,
LocalRuntimeQueues, LocalTaskDispatch, LocalTaskHandle, RuntimeControl, RuntimeId,
RuntimeQuery, RuntimeStatus, RuntimeTime, RuntimeWait, Sleep, SpawnError, SubmitError,
TaskConfig, TaskDispatch, TaskHandle, TimedTaskDispatch, Timeout, TimeoutOn, TurnResult,
UnsubmittedTask,
};
use pi_async_executor::{
DefaultExecutor, ExecutorLocal, ExecutorOptions, ExecutorTimer, IdleBinding, LocalTaskQueue,
LocalTaskSourceAccess, ManualClock, NoIdleQueue, NoTimerQueue, ThreadNotifier, TimerBinding,
};
use pi_async_runtime_core::time::TimedTaskNotifier;
use pi_async_task::LocalOutput;
use pi_async_task_queue::IdleQueueManager;
use pi_clock_manual::global::GlobalManualInstant;
use pi_result::RawResult;
use std::{future::Future, rc::Rc, time::Duration};
type Ready = LocalTaskQueue;
type NoTimer = NoTimerQueue<GlobalManualInstant>;
pub type LocalRuntime = ConfiguredLocalRuntime<NoIdleQueue, NoTimer>;
pub struct ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
service: LocalService<I, T>,
local: Rc<ExecutorLocal<T, Ready>>,
}
impl LocalRuntime {
pub fn new() -> pi_result::Result<Self> {
Self::with_options(ExecutorOptions::default())
}
pub fn with_options(options: ExecutorOptions) -> pi_result::Result<Self> {
local_runtime_new_inner(options)
}
pub fn with_queues<I, F, T>(
options: ExecutorOptions,
queues: LocalRuntimeQueues<I, F>,
) -> pi_result::Result<ConfiguredLocalRuntime<I, T>>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
F: RuntimeTimerFactory<ManualClock, T, Ready>,
{
local_runtime_with_queues_inner(options, queues)
}
}
impl<I, T> ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
pub fn run_once(&mut self) -> pi_result::Result<TurnResult> {
self.service.run_once()
}
#[cfg(not(target_family = "wasm"))]
pub fn run(&mut self) -> pi_result::Result<()> {
self.service.run()
}
}
impl<I, T> TaskDispatch for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
type Notifier = ThreadNotifier;
fn create_with_config<F, M>(
&self,
future: F,
config: TaskConfig<M>,
) -> RawResult<(UnsubmittedTask, TaskHandle<F::Output, M>), CreateError<F, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
self.service.shared().create_task(future, config)
}
fn submit(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError> {
self.service
.shared()
.submit_same_task_queue(&self.local, task)
}
}
impl<I, T> LocalTaskDispatch for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
fn create_local_with_config<F, M>(
future: F,
config: TaskConfig<M>,
) -> RawResult<(UnsubmittedTask, LocalTaskHandle<F::Output, M>), CreateError<F, M>>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
dispatch::create_local::<
ManualClock,
LocalTaskSourceAccess,
I::Handle,
T,
Ready,
F,
M,
>(future, config)
}
fn submit_local(task: UnsubmittedTask) -> RawResult<(), SubmitError> {
dispatch::submit_local::<ManualClock, LocalTaskSourceAccess, I::Handle, T, Ready>(
task, false,
)
}
}
#[cfg(not(target_family = "wasm"))]
impl<I, T> crate::RuntimeBlockOn for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
fn block_on<F: Future>(&self, future: F) -> F::Output {
self.service.block_on(future)
}
}
impl<I, T> RuntimeQuery for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
fn id(&self) -> RuntimeId {
self.service.id()
}
fn status(&self) -> RuntimeStatus {
self.service.status()
}
fn is_driving(&self) -> bool {
self.service.is_driving()
}
fn last_idle_state(&self) -> IdleState {
self.service.last_idle_state()
}
fn close_state(&self) -> CloseState {
self.service.close_state()
}
fn external_task_count(&self) -> usize {
self.service.external_task_count()
}
fn internal_task_count(&self) -> usize {
self.service.internal_task_count()
}
fn timer_registration_count(&self) -> usize {
self.service.timer_registration_count()
}
fn idle_task_count(&self) -> usize {
self.service.idle_task_count()
}
fn in_flight_task_count(&self) -> usize {
self.service.in_flight_task_count()
}
fn waiting_for_wake_count(&self) -> usize {
self.service.waiting_for_wake_count()
}
fn pending_cancel_task_count(&self) -> usize {
self.service.pending_cancel_task_count()
}
}
impl<I, T> RuntimeControl for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
fn request_close(&self) -> CloseRequestStatus {
self.service.request_close()
}
}
impl<I, T> RuntimeTime for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
type Timer = T::Access;
fn timeout(&self, duration: Duration) -> Timeout<Self::Timer> {
self.service.timeout(duration)
}
fn timeout_on<F: Future>(&self, future: F, duration: Duration) -> TimeoutOn<Self::Timer, F> {
self.service.timeout_on(future, duration)
}
fn sleep(&self, duration: Duration) -> Sleep<Self::Timer> {
self.service.sleep(duration)
}
fn timeout_local(duration: Duration) -> RawResult<Timeout<Self::Timer>, LocalRuntimeAccessError> {
LocalService::<I, T>::timeout_local(duration)
}
fn timeout_on_local<F: Future>(
future: F,
duration: Duration,
) -> RawResult<TimeoutOn<Self::Timer, F>, LocalRuntimeAccessError> {
LocalService::<I, T>::timeout_on_local(future, duration)
}
fn sleep_local(duration: Duration) -> RawResult<Sleep<Self::Timer>, LocalRuntimeAccessError> {
LocalService::<I, T>::sleep_local(duration)
}
}
impl<I, T> RuntimeWait for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
}
impl<I, T> IdleDispatch for ConfiguredLocalRuntime<I, T>
where
I: IdleQueueManager,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
{
fn submit_idle(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError> {
self.service.shared().submit_idle_task(task)
}
fn idle_local_with_config<F, M>(
future: F,
config: TaskConfig<M>,
) -> RawResult<TaskHandle<F::Output, M>, SpawnError<F, ThreadNotifier, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
dispatch::idle_local::<ManualClock, LocalTaskSourceAccess, I::Handle, T, Ready, F, M>(
future, config,
)
}
}
impl<I, T> TimedTaskDispatch for ConfiguredLocalRuntime<I, T>
where
I: IdleBinding,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
fn spawn_timing_with_config<F, M>(
&self,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> RawResult<
pi_async_runtime_core::TaskHandle<TimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M>,
SpawnError<F, TimedTaskNotifier<ThreadNotifier, T::Access>, M>,
>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
self.service.shared().spawn_timing_task(future, delay, config)
}
fn spawn_timing_local_with_config<F, M>(
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> RawResult<
pi_async_runtime_core::TaskHandle<TimedTaskNotifier<ThreadNotifier, T::Access>, F::Output, M, LocalOutput>,
SpawnError<F, TimedTaskNotifier<ThreadNotifier, T::Access>, M, LocalOutput>,
>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
dispatch::timing_local::<ManualClock, LocalTaskSourceAccess, I::Handle, T, Ready, F, M>(
future, delay, config,
)
}
}
include!("local_runtime_inner.rs");