#[allow(unused_variables)]
fn handle_clone_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
>(
this: &ExecutorHandle<E, I, T, L>,
) -> ExecutorHandle<E, I, T, L> {
ExecutorHandle {
factory: this.factory.clone(),
external: this.external.clone(),
idle: this.idle.clone(),
timer: this.timer.clone(),
timer_query: this.timer_query.clone(),
state: Arc::clone(&this.state),
registry: this.registry.clone(),
notifier: this.notifier.clone(),
owner: this.owner,
types: PhantomData,
}
}
fn handle_create_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
F,
M,
>(
this: &ExecutorHandle<E, I, T, L>,
future: F,
config: TaskConfig<M>,
) -> pi_result::RawResult<
(UnsubmittedTask, TaskHandle<ThreadNotifier, F::Output, M>),
CreateError<F, M>,
>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
let (task, handle) = this.factory.create(future, config)?;
this.registry.register(handle.control_handle().query_handle());
Ok((task, handle))
}
fn handle_submit_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
>(
this: &ExecutorHandle<E, I, T, L>,
task: UnsubmittedTask,
idle: bool,
) -> pi_result::RawResult<(), SubmitError> {
let status = this.state.status();
let closed = matches!(status, RuntimeStatus::Closing | RuntimeStatus::Closed);
let external = this.external.clone();
let idle_queue = this.idle.clone();
let notifier = this.notifier.clone();
this.factory.submit(
task,
if idle { SubmissionMode::Idle } else { SubmissionMode::Ordinary },
|runnable| {
if closed {
let cause = if idle {
SubmitErrorKind::Idle(pi_async_task_queue::idle::IdleQueueError::Closed)
} else {
SubmitErrorKind::External(pi_async_task_queue::external::ExternalPushErrorKind::Closed)
};
return Err((runnable, cause));
}
let mut guard = SourceNotifyGuard { notifier: ¬ifier, armed: true };
let result = if idle {
idle_queue.try_push(runnable).map_err(|error| {
(error.task, SubmitErrorKind::Idle(error.cause))
})
} else {
external.try_push(runnable).map_err(|error| {
(error.task, SubmitErrorKind::External(error.cause))
})
};
guard.armed = false;
result
},
)
}
fn handle_create_local_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
F,
M,
>(
this: &ExecutorHandle<E, I, T, L>,
local: &ExecutorLocal<T>,
future: F,
config: TaskConfig<M>,
) -> pi_result::RawResult<
(
UnsubmittedTask,
TaskHandle<ThreadNotifier, F::Output, M, LocalOutput>,
),
CreateError<F, M>,
>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
if local.runtime_id() != this.factory.runtime_id() {
return Err(CreateError {
future,
config,
cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::TypeMismatch),
});
}
let mut pending = Some((future, config));
let registry = this.registry.clone();
let created = local.with_owner(|owner| {
let (future, config) = pending.take().expect("原值与配置只在此处取出一次");
this.factory.create_local(owner, future, config)
});
match created {
Ok(result) => {
let (task, handle) = result?;
registry.register(handle.control_handle().query_handle());
Ok((task, handle))
}
Err(_) => {
let (future, config) = pending.take().expect("未取得所属域时原值仍在本处");
Err(CreateError {
future,
config,
cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::ContextUnavailable),
})
}
}
}
fn handle_submit_local_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
>(
this: &ExecutorHandle<E, I, T, L>,
local: &ExecutorLocal<T>,
task: UnsubmittedTask,
front: bool,
) -> pi_result::RawResult<(), SubmitError> {
if local.runtime_id() != this.factory.runtime_id() {
return Err(SubmitError {
task,
cause: SubmitErrorKind::LocalContext(LocalRuntimeAccessError::TypeMismatch),
});
}
let mut pending = Some(task);
let submitted = local.with_owner(|owner| {
let task = pending.take().expect("原待提交值只在此处取出一次");
this.factory.submit(task, SubmissionMode::Local(owner.id()), |runnable| {
local
.try_push(runnable, front)
.map_err(|error| (error.task, SubmitErrorKind::LocalQueue(error.cause)))
})
});
match submitted {
Ok(result) => result,
Err(_) => Err(SubmitError {
task: pending.take().expect("未取得所属域时原任务仍在本处"),
cause: SubmitErrorKind::LocalContext(LocalRuntimeAccessError::ContextUnavailable),
}),
}
}
fn handle_timed_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
F,
M,
>(
this: &ExecutorHandle<E, I, T, L>,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> pi_result::RawResult<
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,
{
let registry = this.registry.clone();
pi_async_runtime_core::time::task_support::spawn_task_with_query(
&this.factory,
this.timer.clone(),
future,
delay,
config,
move |query| registry.register(query),
)
}
fn handle_timed_local_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
F,
M,
>(
this: &ExecutorHandle<E, I, T, L>,
local: &ExecutorLocal<T>,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> pi_result::RawResult<
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,
{
let mut pending = Some((future, config));
let registry = this.registry.clone();
let outcome = local.with_owner(|owner| {
let (future, config) = pending.take().expect("原值与配置只在此处取出一次");
pi_async_runtime_core::time::task_support::spawn_local_task_with_query(
&this.factory,
owner,
this.timer.clone(),
future,
delay,
config,
move |query| registry.register(query),
)
});
match outcome {
Ok(result) => result,
Err(_) => {
let (future, config) = pending.take().expect("未取得所属域时原值仍在本处");
Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::LocalContext(LocalRuntimeAccessError::ContextUnavailable),
}))
}
}
}
fn handle_internal_count_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
>(
this: &ExecutorHandle<E, I, T, L>,
) -> usize {
this.state.internal.load(std::sync::atomic::Ordering::Acquire)
}
#[allow(unused_variables)]
fn handle_in_flight_inner<
E: ExternalTaskQueue + Clone,
I: IdleTaskQueue + Clone,
T: ExecutorTimer + 'static,
L: LocalReadySubmit,
>(
this: &ExecutorHandle<E, I, T, L>,
) -> usize {
this.state.in_flight.load(std::sync::atomic::Ordering::Acquire)
}
#[allow(unused_variables)]
fn source_notify_drop_inner(guard: &mut SourceNotifyGuard<'_>) {
if !guard.armed {
return;
}
guard.armed = false;
pi_async_task_queue::notify::QueueNotifier::notify(guard.notifier);
}
#[cfg(test)]
#[path = "handle_inner_test.rs"]
mod handle_inner_test;