#[cfg(test)]
#[path = "task_support_inner_test.rs"]
mod task_support_inner_test;
fn spawn_query_inner<S, N, A, F, M, G>(
factory: &TaskFactory<S, N>,
access: A,
future: F,
delay: Duration,
config: TaskConfig<M>,
register: G,
) -> pi_result::RawResult<
TaskHandle<TimedTaskNotifier<N, A>, F::Output, M>,
SpawnError<F, TimedTaskNotifier<N, A>, M>,
>
where
S: Schedule + Clone,
N: TaskNotifier,
A: TimerAccess,
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
G: FnOnce(TaskQuery),
{
use crate::definitions::TimerError;
use crate::task::CreateError;
use pi_async_task_queue::timer::{TimerQueueError, TimerSubmitError};
if factory.runtime_id() != access.runtime_id() {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(TimerError::Queue(TimerQueueError::WrongQueue)),
}));
}
let min_positive = Duration::from_millis(1);
if !delay.is_zero() && delay < min_positive {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(TimerError::InvalidDuration {
value: delay,
min_positive,
max: None,
}),
}));
}
let deadline = match access.deadline_after(delay) {
Ok(deadline) => deadline,
Err(error) => {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(error),
}));
}
};
let registration = match access.prepare_registration(TimerRegistrationKind::Task) {
Ok(registration) => registration,
Err(error) => {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(error),
}));
}
};
let timed_notifier = TimedTaskNotifier::new(
factory.notifier().clone(),
access.clone(),
registration.clone(),
);
let timed_factory = factory.with_notifier(timed_notifier);
let (unsubmitted, handle) = timed_factory
.create(future, config)
.map_err(SpawnError::Create)?;
register(handle.query_handle());
let mut guard = QueryRegistrationGuard {
access: access.clone(),
registration: registration.clone(),
armed: false,
};
let submit_result = factory.submit(unsubmitted, SubmissionMode::Ordinary, |runnable| {
if !registration.as_ref().begin() {
registration.as_ref().finish(RegistrationEnd::DirectFailure);
return Err((runnable, SubmitErrorKind::Timer(TimerQueueError::Closed)));
}
guard.armed = true;
let entry = TimerEntry::task(runnable, registration.clone());
match access.try_register(deadline, entry) {
Ok(key) => {
let already_recorded = registration.as_ref().publish_key(key);
if already_recorded {
let _ = request_registration_cancel(
&access,
®istration,
CancelMode::KeyPublished,
);
}
Ok(())
}
Err(TimerSubmitError { item, cause }) => {
let runnable = item
.recover()
.expect("刚构造的任务定时入口(TimerEntry::task)必须保有原Runnable");
registration.as_ref().finish(RegistrationEnd::DirectFailure);
guard.armed = false;
Err((runnable, SubmitErrorKind::Timer(cause)))
}
}
});
match submit_result {
Ok(()) => {
guard.armed = false;
Ok(handle)
}
Err(error) => {
guard.armed = false;
Err(SpawnError::Submit { error, handle })
}
}
}
fn spawn_local_query_inner<S, N, A, F, M, G>(
factory: &TaskFactory<S, N>,
owner: &TaskThread,
access: A,
future: F,
delay: Duration,
config: TaskConfig<M>,
register: G,
) -> pi_result::RawResult<
TaskHandle<TimedTaskNotifier<N, A>, F::Output, M, LocalOutput>,
SpawnError<F, TimedTaskNotifier<N, A>, M, LocalOutput>,
>
where
S: Schedule + Clone,
N: TaskNotifier,
A: TimerAccess,
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
G: FnOnce(TaskQuery),
{
use crate::definitions::TimerError;
use crate::task::CreateError;
use pi_async_task_queue::timer::{TimerQueueError, TimerSubmitError};
if factory.runtime_id() != access.runtime_id() {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(TimerError::Queue(TimerQueueError::WrongQueue)),
}));
}
let min_positive = Duration::from_millis(1);
if !delay.is_zero() && delay < min_positive {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(TimerError::InvalidDuration {
value: delay,
min_positive,
max: None,
}),
}));
}
let deadline = match access.deadline_after(delay) {
Ok(deadline) => deadline,
Err(error) => {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(error),
}));
}
};
let registration = match access.prepare_registration(TimerRegistrationKind::Task) {
Ok(registration) => registration,
Err(error) => {
return Err(SpawnError::Create(CreateError {
future,
config,
cause: CreateErrorKind::Timer(error),
}));
}
};
let timed_notifier = TimedTaskNotifier::new(
factory.notifier().clone(),
access.clone(),
registration.clone(),
);
let timed_factory = factory.with_notifier(timed_notifier);
let (unsubmitted, handle) = timed_factory
.create_local(owner, future, config)
.map_err(SpawnError::Create)?;
register(handle.query_handle());
let mut guard = QueryRegistrationGuard {
access: access.clone(),
registration: registration.clone(),
armed: false,
};
let submit_result = factory.submit(unsubmitted, SubmissionMode::Local(owner.id()), |runnable| {
if !registration.as_ref().begin() {
registration.as_ref().finish(RegistrationEnd::DirectFailure);
return Err((runnable, SubmitErrorKind::Timer(TimerQueueError::Closed)));
}
guard.armed = true;
let entry = TimerEntry::task(runnable, registration.clone());
match access.try_register_local(deadline, entry) {
Ok(key) => {
let already_recorded = registration.as_ref().publish_key(key);
if already_recorded {
let _ = request_registration_cancel(
&access,
®istration,
CancelMode::KeyPublished,
);
}
Ok(())
}
Err(TimerSubmitError { item, cause }) => {
let runnable = item
.recover()
.expect("刚构造的本地任务定时入口(TimerEntry::task)必须保有原Runnable");
registration.as_ref().finish(RegistrationEnd::DirectFailure);
guard.armed = false;
Err((runnable, SubmitErrorKind::Timer(cause)))
}
}
});
match submit_result {
Ok(()) => {
guard.armed = false;
Ok(handle)
}
Err(error) => {
guard.armed = false;
Err(SpawnError::Submit { error, handle })
}
}
}
#[allow(unused_variables)]
fn query_registration_drop_inner<A: TimerAccess>(this: &mut QueryRegistrationGuard<A>) {
if !this.armed {
return;
}
this.armed = false;
let _ = request_registration_cancel(&this.access, &this.registration, CancelMode::Detached);
}