1#![cfg_attr(docsrs, feature(doc_cfg))]
3use std::{
4 sync::atomic::{AtomicU64, Ordering},
5 time::{Duration, SystemTime, UNIX_EPOCH},
6};
7
8pub fn timestamp() -> u64 {
10 SystemTime::now()
11 .duration_since(UNIX_EPOCH)
12 .expect("create timestamp in timing")
13 .as_millis() as u64
14}
15
16pub const SECONDS_PER_YEAR: f64 = 365.242_199 * 24.0 * 60.0 * 60.0;
17
18pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64) -> f64 {
20 years *
22 SECONDS_PER_YEAR
24 * (1_000_000_000.0 / tick_duration.as_nanos() as f64)
26 / ticks_per_slot as f64
28}
29
30pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
32 let slot_in_ns = if slots_per_year != 0.0 {
34 (SECONDS_PER_YEAR * 1_000_000_000.0) / slots_per_year
35 } else {
36 0.0
37 };
38 Duration::from_nanos(slot_in_ns as u64)
39}
40
41#[derive(Debug, Default)]
42pub struct AtomicInterval {
43 last_update: AtomicU64,
44}
45
46impl AtomicInterval {
47 #[inline(always)]
49 pub fn should_update(&self, interval_time_ms: u64) -> bool {
50 self.should_update_ext(interval_time_ms, true)
51 }
52
53 #[inline(always)]
57 pub fn should_update_ext(&self, interval_time_ms: u64, skip_first: bool) -> bool {
58 let now = timestamp();
59 let last = self.last_update.load(Ordering::Relaxed);
60 now.saturating_sub(last) > interval_time_ms
61 && self
62 .last_update
63 .compare_exchange(last, now, Ordering::Relaxed, Ordering::Relaxed)
64 == Ok(last)
65 && !(skip_first && last == 0)
66 }
67
68 pub fn elapsed_ms(&self) -> u64 {
70 let now = timestamp();
71 let last = self.last_update.load(Ordering::Relaxed);
72 now.saturating_sub(last) }
74
75 pub fn remaining_until_next_interval(&self, interval_time: u64) -> u64 {
77 interval_time.saturating_sub(self.elapsed_ms())
78 }
79}
80
81#[cfg(test)]
82mod test {
83 use super::*;
84
85 #[test]
86 fn test_interval_update() {
87 let i = AtomicInterval::default();
88 assert!(!i.should_update(1000));
89
90 let i = AtomicInterval::default();
91 assert!(i.should_update_ext(1000, false));
92
93 std::thread::sleep(Duration::from_millis(10));
94 assert!(i.elapsed_ms() > 9 && i.elapsed_ms() < 1000);
95 assert!(
96 i.remaining_until_next_interval(1000) > 9
97 && i.remaining_until_next_interval(1000) < 991
98 );
99 assert!(i.should_update(9));
100 assert!(!i.should_update(100));
101 }
102
103 #[test]
104 fn test_years_as_slots() {
105 let tick_duration = Duration::from_micros(1000 * 1000 / 160);
106
107 assert_eq!(years_as_slots(0.0, &tick_duration, 4) as u64, 0);
109 assert_eq!(
110 years_as_slots(1.0 / 12f64, &tick_duration, 4) as u64,
111 105_189_753
112 );
113 assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_262_277_039);
114
115 let tick_duration = Duration::from_micros(1000 * 1000);
116 assert_eq!(
118 years_as_slots(1.0 / SECONDS_PER_YEAR, &tick_duration, 1),
119 1.0
120 );
121 }
122
123 #[test]
124 fn test_slot_duration_from_slots_per_year() {
125 let slots_per_year = 1_262_277_039.0;
126 let ticks_per_slot = 4;
127
128 assert_eq!(
129 slot_duration_from_slots_per_year(slots_per_year),
130 Duration::from_micros(1000 * 1000 / 160) * ticks_per_slot
131 );
132 assert_eq!(
133 slot_duration_from_slots_per_year(0.0),
134 Duration::from_micros(0) * ticks_per_slot
135 );
136
137 let slots_per_year = SECONDS_PER_YEAR;
138 let ticks_per_slot = 1;
139 assert_eq!(
140 slot_duration_from_slots_per_year(slots_per_year),
141 Duration::from_millis(1000) * ticks_per_slot
142 );
143 }
144}