pi_async_runtime_core/time/
registration.rs1use atomic_waker::AtomicWaker;
3use std::{cell::UnsafeCell, mem::MaybeUninit, sync::atomic::AtomicU8};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum TimerRegistrationKind {
8 Task,
9 Wait,
10}
11
12pub struct TimerRegistration<K: Send + Sync + 'static> {
15 facts: AtomicU8,
18 key: UnsafeCell<MaybeUninit<K>>,
19 completion: TimerCompletion,
20 forwarding: AtomicU8,
22}
23enum TimerCompletion {
24 Task,
25 Wait {
26 outcome: AtomicU8,
27 waker: AtomicWaker,
28 },
29}
30unsafe impl<K: Send + Sync + 'static> Send for TimerRegistration<K> {}
33unsafe 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 pub fn new(kind: TimerRegistrationKind) -> Self {
45 registration_new_inner(kind)
46 }
47 pub(crate) fn request_cancel(&self) -> bool {
49 registration_cancel_inner(self)
50 }
51 pub(crate) fn request_wait_cancel(&self) -> bool {
53 registration_wait_cancel_inner(self)
54 }
55 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");