use super::{
protocol::LocalTimerSubmit,
retry::{RetryHandle, RetryOwner, RetryShared, RetryWork},
};
use crate::{clock::ExecutorClock, parking::ThreadNotifier};
use cordyceps::{mpsc_queue::Links, Linked};
use pi_async_runtime_core::{
definitions::{RuntimeId, TimerError},
time::{
access::{Confirmation, TimerCancelRequestOutcome},
resume_timer_cancel, TimerAccess, TimerEntry, TimerRegistration, TimerRegistrationKind,
},
};
use pi_async_task_queue::{
timer::{TimerQueueError, TimerQueueHandle, TimerSubmitError},
CancelTimerKey, CancelTimerQueueHandle,
};
use pi_clock_manual::global::GlobalManualInstant;
use std::{
cell::UnsafeCell,
marker::{PhantomData, PhantomPinned},
ptr::NonNull,
sync::{
atomic::{AtomicBool, AtomicU8},
Arc, Weak,
},
time::Duration,
};
pub type DefaultTimerItem<C, L> = TimerEntry<CancelTimerKey, DefaultTimerRegistration<C, L>>;
pub struct DefaultTimerAccess<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
pub(super) shared: Arc<TimerSourceShared<C, L>>,
}
pub struct DefaultTimerRegistration<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
pub(super) node: Arc<CancelRetryNode<C, L>>,
}
pub(super) struct TimerSourceShared<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
pub(super) runtime: RuntimeId,
pub(super) clock: C,
pub(super) backend: CancelTimerQueueHandle<DefaultTimerItem<C, L>, ThreadNotifier>,
pub(super) retries: Arc<RetryShared<CancelRetryNode<C, L>>>,
pub(super) owner_exiting: AtomicBool,
pub(super) local: PhantomData<fn() -> L>,
}
pub(super) struct CancelRetryNode<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
facts: TimerRegistration<CancelTimerKey>,
links: Links<Self>,
origin: Weak<TimerSourceShared<C, L>>,
retry: Weak<RetryShared<Self>>,
membership: AtomicU8,
error: UnsafeCell<Option<TimerQueueError>>,
stub: bool,
pin: PhantomPinned,
}
const NODE_IDLE: u8 = 0;
const NODE_PUBLISHING: u8 = 1;
const NODE_QUEUED: u8 = 2;
unsafe impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Sync
for CancelRetryNode<C, L>
{
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Clone
for DefaultTimerAccess<C, L>
{
fn clone(&self) -> Self {
default_access_clone_inner(self)
}
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Clone
for DefaultTimerRegistration<C, L>
{
fn clone(&self) -> Self {
default_registration_clone_inner(self)
}
}
impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static>
AsRef<TimerRegistration<CancelTimerKey>> for DefaultTimerRegistration<C, L>
{
fn as_ref(&self) -> &TimerRegistration<CancelTimerKey> {
&self.node.facts
}
}
impl<C, L> TimerAccess for DefaultTimerAccess<C, L>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
type Instant = GlobalManualInstant;
type Key = CancelTimerKey;
type Registration = DefaultTimerRegistration<C, L>;
fn runtime_id(&self) -> RuntimeId {
self.shared.runtime
}
fn deadline_after(
&self,
duration: Duration,
) -> pi_result::RawResult<Self::Instant, TimerError> {
default_deadline_inner(self, duration)
}
fn prepare_registration(
&self,
kind: TimerRegistrationKind,
) -> pi_result::RawResult<Self::Registration, TimerError> {
default_prepare_inner(self, kind)
}
fn try_register(
&self,
deadline: Self::Instant,
item: DefaultTimerItem<C, L>,
) -> pi_result::RawResult<Self::Key, TimerSubmitError<DefaultTimerItem<C, L>>> {
default_register_inner(self, deadline, item)
}
fn try_register_local(
&self,
deadline: Self::Instant,
item: DefaultTimerItem<C, L>,
) -> pi_result::RawResult<Self::Key, TimerSubmitError<DefaultTimerItem<C, L>>> {
default_register_local_inner(self, deadline, item)
}
fn request_cancel(
&self,
key: &Self::Key,
) -> pi_result::RawResult<TimerCancelRequestOutcome, TimerQueueError> {
default_cancel_inner(self, key)
}
fn defer_cancel(
&self,
registration: Self::Registration,
error: Option<TimerQueueError>,
confirmation: Confirmation<'_, Self::Key>,
) {
assert!(confirmation.is_for(registration.as_ref()), "取消接续须使用原登记");
default_defer_inner(self, registration, error, confirmation)
}
}
impl<C, L> RetryWork for CancelRetryNode<C, L>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
fn resume(this: &Arc<Self>) {
default_retry_resume_inner(this)
}
}
unsafe impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Linked<Links<Self>>
for CancelRetryNode<C, L>
{
type Handle = RetryHandle<Self>;
fn into_ptr(handle: Self::Handle) -> NonNull<Self> {
default_retry_into_ptr_inner(handle)
}
unsafe fn from_ptr(ptr: NonNull<Self>) -> Self::Handle {
default_retry_from_ptr_inner(ptr)
}
unsafe fn links(ptr: NonNull<Self>) -> NonNull<Links<Self>> {
default_retry_links_inner(ptr)
}
}
pub(super) fn release_retry_unit<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static>(
node: &CancelRetryNode<C, L>,
) {
default_release_unit_inner(node)
}
fn publish_retry<C, L>(
source: &TimerSourceShared<C, L>,
node: Arc<CancelRetryNode<C, L>>,
confirmation: Confirmation<'_, CancelTimerKey>,
) where
C: ExecutorClock<Instant = GlobalManualInstant>,
L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
source.retries.publish(node, release_retry_unit::<C, L>, |retained| {
assert!(
confirmation.deferred(&retained.facts).is_ok(),
"发布的原节点须与已核对的登记相同",
);
});
}
pub(super) fn new_retry_owner<C, L>(
notifier: ThreadNotifier,
batch: std::num::NonZeroUsize,
) -> RetryOwner<CancelRetryNode<C, L>>
where
C: ExecutorClock<Instant = GlobalManualInstant>,
L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
{
default_retry_owner_new_inner(notifier, batch)
}
include!("access_inner.rs");