Skip to main content

pi_async_runtime_core/time/
registration.rs

1//! 单次登记的共享事实;取消转交资格与只增事实分开。
2use atomic_waker::AtomicWaker;
3use std::{cell::UnsafeCell, mem::MaybeUninit, sync::atomic::AtomicU8};
4
5/// 仅控制登记存储是否需要等待结果信号,不授予任务(Task)执行权限。
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum TimerRegistrationKind {
8    Task,
9    Wait,
10}
11
12/// 实现者共同嵌入的登记事实;键(key)只初始化一次,不要求Clone/Copy。
13// @trace 1
14pub struct TimerRegistration<K: Send + Sync + 'static> {
15    // 单调事实:STARTED/KEY_WRITER/KEY_READY/CANCEL_REQUESTED/
16    // CANCEL_FORWARDED/RELEASED/INTEREST_CLOSED;无SeqCst依赖。
17    facts: AtomicU8,
18    key: UnsafeCell<MaybeUninit<K>>,
19    completion: TimerCompletion,
20    // Idle/Publishing/Deferred/Finished;不混入只增事实字。
21    forwarding: AtomicU8,
22}
23enum TimerCompletion {
24    Task,
25    Wait {
26        outcome: AtomicU8,
27        waker: AtomicWaker,
28    },
29}
30// SAFETY: 键(key)通过独占写入资格初始化一次,Release发布后才允许Acquire读取;
31// 其它共享成员均为原子或AtomicWaker,最终独占析构仅释放已初始化键。
32unsafe impl<K: Send + Sync + 'static> Send for TimerRegistration<K> {}
33// SAFETY: 不暴露可写键槽,借用K要求K:Sync,发布与读取协议同上。
34unsafe impl<K: Send + Sync + 'static> Sync for TimerRegistration<K> {}
35impl<K: Send + Sync + 'static> TimerRegistration<K> {
36    const STARTED: u8 = 1;
37    const KEY_WRITER: u8 = 2;
38    const KEY_READY: u8 = 4;
39    const CANCEL_REQUESTED: u8 = 8;
40    const CANCEL_FORWARDED: u8 = 16;
41    const RELEASED: u8 = 32;
42    const INTEREST_CLOSED: u8 = 64;
43    /// 准备未登记的基础事实;由实现者与预备重试节点共同持有,不读取时钟。
44    pub fn new(kind: TimerRegistrationKind) -> Self {
45        registration_new_inner(kind)
46    }
47    // 内部清理记录请求,不受等待兴趣结束阻挡,不确认实际移除。
48    pub(crate) fn request_cancel(&self) -> bool {
49        registration_cancel_inner(self)
50    }
51    // 用户控制必须在同一事实字CAS内同时核兴趣,不能先查询再无条件请求。
52    pub(crate) fn request_wait_cancel(&self) -> bool {
53        registration_wait_cancel_inner(self)
54    }
55    // 返回发布时已经存在的取消请求,不能用发布键本身制造取消。
56    pub(crate) fn publish_key(&self, key: K) -> bool {
57        registration_key_inner(self, key)
58    }
59    pub(crate) fn key(&self) -> Option<&K> {
60        registration_key_ref_inner(self)
61    }
62    pub(crate) fn begin(&self) -> bool {
63        registration_begin_inner(self)
64    }
65    pub(crate) fn finish(&self, reason: RegistrationEnd) {
66        registration_finish_inner(self, reason)
67    }
68    pub(crate) fn close_interest(&self) {
69        registration_interest_inner(self)
70    }
71    pub(crate) fn poll(&self, cx: &mut std::task::Context<'_>) -> std::task::Poll<RegistrationEnd> {
72        registration_poll_inner(self, cx)
73    }
74    pub(crate) fn claim_forwarding(&self, deferred: bool) -> bool {
75        registration_claim_inner(self, deferred)
76    }
77    pub(crate) fn defer_forwarding(&self) {
78        registration_defer_inner(self)
79    }
80    pub(crate) fn finish_forwarding(&self, forwarded: bool) {
81        registration_forwarded_inner(self, forwarded)
82    }
83}
84impl<K: Send + Sync + 'static> Drop for TimerRegistration<K> {
85    fn drop(&mut self) {
86        registration_drop_inner(self)
87    }
88}
89#[derive(Clone, Copy)]
90pub(crate) enum RegistrationEnd {
91    Elapsed,
92    Cancelled,
93    Removed,
94    DirectFailure,
95}
96
97#[repr(u8)]
98enum ForwardState {
99    Idle = 0,
100    Publishing = 1,
101    Deferred = 2,
102    Finished = 3,
103}
104#[repr(u8)]
105enum WaitOutcome {
106    Pending = 0,
107    Elapsed = 1,
108    Cancelled = 2,
109    Removed = 3,
110    DirectFailure = 4,
111}
112include!("registration_inner.rs");