fn prepared_serial_runtime_new_inner(
clock: ManualClock,
options: CancelTimerOptions,
) -> pi_result::Result<PreparedSerialRuntime> {
let id = RuntimeIdReservation::reserve()?;
let parker = PreparedThreadParker::new();
let timer = PreparedDefaultTimer::prepare(id.id(), clock, parker.notifier(), options)?;
Ok(PreparedSerialRuntime { id, parker, timer })
}
fn prepared_serial_runtime_id_inner<T>(this: &PreparedSerialRuntime<T>) -> RuntimeId {
this.id.id()
}
fn prepared_serial_runtime_notifier_inner<T>(this: &PreparedSerialRuntime<T>) -> ThreadNotifier {
this.parker.notifier()
}
fn prepared_serial_runtime_timer_inner(this: &PreparedSerialRuntime) -> RuntimeTimerAccess {
this.timer.access()
}
fn prepared_serial_bind_inner<E, P, H, I>(
this: PreparedSerialRuntime,
external: E,
policy: P,
hooks: H,
options: RuntimeOptions,
timed_admission: impl Fn(pi_async_task::definitions::TaskId) -> Option<bool> + 'static,
make_idle: impl FnOnce(pi_async_task_queue::MpscQueueOptions) -> pi_result::Result<I>,
) -> pi_result::Result<(
ConfiguredRuntime<SerialMode, ManualClock, P, H, E, I>,
MemberTaskAccess<E::Handle, I::Handle>,
)>
where
E: ExternalQueueManager,
I: IdleBinding,
P: IdlePolicy,
H: ExecutorHooks,
{
let PreparedSerialRuntime { id, parker, timer } = this;
let clock = ManualClock::new()?;
let binding = RuntimeBinding::new();
let executor_options = options.executor;
let idle = make_idle(options.idle_queue)?;
let source = RuntimeExternalSource::new(external, Rc::clone(&binding));
let mut prepared_timer = Some(timer);
let mut may_admit = Some(timed_admission);
let (executor, executor_access) = DefaultExecutor::<
ManualClock,
RuntimeExternalSource<E>,
I,
Timer<ManualClock>,
RuntimeTag<ManualClock, Timer<ManualClock>>,
>::from_prepared(
clock,
id,
parker,
source,
idle,
executor_options,
move |runtime, clock, notifier| {
let timer = prepared_timer.take().expect("预备定时来源只接管一次");
let admit = may_admit.take().expect("定时接纳判据只移交一次");
if timer.access().runtime_id() != runtime {
return Err(pi_result::Error::new(pi_result::ErrorKind::InvalidInput)
.attach("预备定时来源(prepared timer source)与预备成员身份不一致"));
}
let now = clock.now()?;
timer.bind(¬ifier, now, admit)
},
)?;
Ok(prepared_serial_finish_bind(executor, executor_access, binding, policy, hooks))
}
fn prepared_serial_runtime_without_timer_inner() -> pi_result::Result<PreparedSerialRuntime<NoTimerQueue<GlobalManualInstant>>> {
let id = RuntimeIdReservation::reserve()?;
let parker = PreparedThreadParker::new();
Ok(PreparedSerialRuntime { id, parker, timer: NoTimerQueue::default() })
}
fn prepared_serial_bind_without_timer_inner<E, I>(
this: PreparedSerialRuntime<NoTimerQueue<GlobalManualInstant>>,
external: E,
idle_options: pi_async_task_queue::MpscQueueOptions,
make_idle: impl FnOnce(pi_async_task_queue::MpscQueueOptions) -> pi_result::Result<I>,
) -> pi_result::Result<(
RuntimeIdReservation, PreparedThreadParker, RuntimeExternalSource<E>, I, Rc<RuntimeBinding>,
)>
where
E: ExternalQueueManager,
I: IdleBinding,
{
let PreparedSerialRuntime { id, parker, timer: _ } = this;
let binding = RuntimeBinding::new();
let idle = make_idle(idle_options)?;
let source = RuntimeExternalSource::new(external, Rc::clone(&binding));
Ok((id, parker, source, idle, binding))
}
fn member_owner_id_inner<E, I, T>(
this: &MemberTaskAccess<E, I, T>,
) -> pi_async_task::definitions::TaskOwnerId
where
E: ExternalTaskQueue,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>, {
this.executor.owner_id()
}
fn member_return_runnable_inner<E, I, T>(
this: &MemberTaskAccess<E, I, T>,
task: Runnable,
) -> pi_result::RawResult<(), Runnable>
where
E: ExternalTaskQueue,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>, {
this.executor.return_runnable(task)
}
fn member_idle_submit_inner<E, I, T>(
this: &MemberTaskAccess<E, I, T>,
task: Runnable,
) -> pi_result::RawResult<(), IdleSubmitError>
where
E: ExternalTaskQueue,
I: IdleTaskQueue,
T: TimerBinding<ClockInstant = GlobalManualInstant>, {
this.executor.submit_idle(task)
}
fn member_create_with_notice_inner<E, I, T, N, F, M>(
this: &MemberTaskAccess<E, I, T>,
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
E: ExternalTaskQueue,
I: IdleAccess,
T: TimerBinding<ClockInstant = GlobalManualInstant>,
N: TaskNotifier,
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
let mut pending = Some((notifier, future, config));
let runtime_id = pi_async_runtime_core::RuntimeQuery::id(&this.handle);
let outcome = pi_async_runtime_core::context::record::try_with_local_for::<
ExecutorLocal<T>,
_,
>(runtime_id, |local| {
let (notifier, future, config) = pending.take().expect("原值与配置只取出一次");
this.executor
.create_local_with_notifier(local, notifier, future, config)
});
match outcome {
Ok(result) => result,
Err(error) => {
let (_notifier, future, config) = pending.take().expect("未取得本地权限时原值仍在本处");
Err(CreateError {
future,
config,
cause: pi_async_runtime_core::definitions::CreateErrorKind::LocalContext(error),
})
}
}
}
fn member_local_timed_inner<E, I, T, N, A, F, M>(
this: &MemberTaskAccess<E, I, T>,
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
E: ExternalTaskQueue,
I: IdleAccess,
T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
N: TaskNotifier,
A: TimerAccess,
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
let mut pending = Some((notifier, access, future, config));
let runtime_id = pi_async_runtime_core::RuntimeQuery::id(&this.handle);
let outcome = pi_async_runtime_core::context::record::try_with_local_for::<
ExecutorLocal<T>,
_,
>(runtime_id, |local| {
let (notifier, access, future, config) = pending.take().expect("原值与配置只取出一次");
this.executor
.spawn_timing_local_with_notifier(local, notifier, access, future, delay, config)
});
match outcome {
Ok(result) => result,
Err(error) => {
let (_notifier, _access, future, config) =
pending.take().expect("未取得本地权限时原值仍在本处");
Err(SpawnError::Create(CreateError {
future,
config,
cause: pi_async_runtime_core::definitions::CreateErrorKind::LocalContext(error),
}))
}
}
}