1use super::{
3 protocol::LocalTimerSubmit,
4 retry::{RetryHandle, RetryOwner, RetryShared, RetryWork},
5};
6use crate::{clock::ExecutorClock, parking::ThreadNotifier};
7use cordyceps::{mpsc_queue::Links, Linked};
8use pi_async_runtime_core::{
9 definitions::{RuntimeId, TimerError},
10 time::{
11 resume_timer_cancel, TimerAccess, TimerEntry, TimerRegistration, TimerRegistrationKind,
12 },
13};
14use pi_async_task_queue::{
15 timer::{TimerQueueError, TimerQueueHandle, TimerSubmitError},
16 CancelTimerKey, CancelTimerQueueHandle,
17};
18use pi_clock_manual::global::GlobalManualInstant;
19use std::{
20 cell::UnsafeCell,
21 marker::{PhantomData, PhantomPinned},
22 ptr::NonNull,
23 sync::{
24 atomic::{AtomicBool, AtomicU8},
25 Arc, Weak,
26 },
27 time::Duration,
28};
29
30pub type DefaultTimerItem<C, L> = TimerEntry<CancelTimerKey, DefaultTimerRegistration<C, L>>;
32
33pub struct DefaultTimerAccess<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
38 pub(super) shared: Arc<TimerSourceShared<C, L>>,
39}
40pub struct DefaultTimerRegistration<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
42 pub(super) node: Arc<CancelRetryNode<C, L>>,
43}
44pub(super) struct TimerSourceShared<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
45 pub(super) runtime: RuntimeId,
46 pub(super) clock: C,
47 pub(super) backend: CancelTimerQueueHandle<DefaultTimerItem<C, L>, ThreadNotifier>,
48 pub(super) retries: Arc<RetryShared<CancelRetryNode<C, L>>>,
49 pub(super) owner_exiting: AtomicBool,
50 pub(super) local: PhantomData<fn() -> L>,
51}
52pub(super) struct CancelRetryNode<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> {
59 facts: TimerRegistration<CancelTimerKey>,
60 links: Links<Self>,
61 origin: Weak<TimerSourceShared<C, L>>,
62 retry: Weak<RetryShared<Self>>,
63 membership: AtomicU8,
64 error: UnsafeCell<Option<TimerQueueError>>,
65 stub: bool,
66 pin: PhantomPinned,
67}
68const NODE_IDLE: u8 = 0;
69const NODE_PUBLISHING: u8 = 1;
70const NODE_QUEUED: u8 = 2;
71unsafe impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Sync
76 for CancelRetryNode<C, L>
77{
78}
79
80impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Clone
81 for DefaultTimerAccess<C, L>
82{
83 fn clone(&self) -> Self {
84 default_access_clone_inner(self)
85 }
86}
87impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Clone
88 for DefaultTimerRegistration<C, L>
89{
90 fn clone(&self) -> Self {
91 default_registration_clone_inner(self)
92 }
93}
94impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static>
95 AsRef<TimerRegistration<CancelTimerKey>> for DefaultTimerRegistration<C, L>
96{
97 fn as_ref(&self) -> &TimerRegistration<CancelTimerKey> {
98 &self.node.facts
99 }
100}
101impl<C, L> TimerAccess for DefaultTimerAccess<C, L>
102where
103 C: ExecutorClock<Instant = GlobalManualInstant>,
104 L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
105{
106 type Instant = GlobalManualInstant;
107 type Key = CancelTimerKey;
108 type Registration = DefaultTimerRegistration<C, L>;
109 fn runtime_id(&self) -> RuntimeId {
110 self.shared.runtime
111 }
112 fn deadline_after(
113 &self,
114 duration: Duration,
115 ) -> pi_result::RawResult<Self::Instant, TimerError> {
116 default_deadline_inner(self, duration)
117 }
118 fn prepare_registration(
119 &self,
120 kind: TimerRegistrationKind,
121 ) -> pi_result::RawResult<Self::Registration, TimerError> {
122 default_prepare_inner(self, kind)
123 }
124 fn try_register(
125 &self,
126 deadline: Self::Instant,
127 item: DefaultTimerItem<C, L>,
128 ) -> pi_result::RawResult<Self::Key, TimerSubmitError<DefaultTimerItem<C, L>>> {
129 default_register_inner(self, deadline, item)
130 }
131 fn try_register_local(
132 &self,
133 deadline: Self::Instant,
134 item: DefaultTimerItem<C, L>,
135 ) -> pi_result::RawResult<Self::Key, TimerSubmitError<DefaultTimerItem<C, L>>> {
136 default_register_local_inner(self, deadline, item)
137 }
138 fn request_cancel(&self, key: &Self::Key) -> pi_result::RawResult<(), TimerQueueError> {
139 default_cancel_inner(self, key)
140 }
141 fn defer_cancel(&self, registration: Self::Registration, error: Option<TimerQueueError>) {
142 default_defer_inner(self, registration, error)
143 }
144}
145impl<C, L> RetryWork for CancelRetryNode<C, L>
146where
147 C: ExecutorClock<Instant = GlobalManualInstant>,
148 L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
149{
150 fn resume(this: &Arc<Self>) {
151 default_retry_resume_inner(this)
152 }
153}
154unsafe impl<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static> Linked<Links<Self>>
158 for CancelRetryNode<C, L>
159{
160 type Handle = RetryHandle<Self>;
161 fn into_ptr(handle: Self::Handle) -> NonNull<Self> {
162 default_retry_into_ptr_inner(handle)
163 }
164 unsafe fn from_ptr(ptr: NonNull<Self>) -> Self::Handle {
165 default_retry_from_ptr_inner(ptr)
166 }
167 unsafe fn links(ptr: NonNull<Self>) -> NonNull<Links<Self>> {
168 default_retry_links_inner(ptr)
169 }
170}
171pub(super) fn release_retry_unit<C: ExecutorClock<Instant = GlobalManualInstant>, L: 'static>(
172 node: &CancelRetryNode<C, L>,
173) {
174 default_release_unit_inner(node)
175}
176pub(super) fn new_retry_owner<C, L>(
178 notifier: ThreadNotifier,
179 batch: std::num::NonZeroUsize,
180) -> RetryOwner<CancelRetryNode<C, L>>
181where
182 C: ExecutorClock<Instant = GlobalManualInstant>,
183 L: LocalTimerSubmit<DefaultTimerItem<C, L>>,
184{
185 default_retry_owner_new_inner(notifier, batch)
186}
187include!("access_inner.rs");