1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::sync::atomic::{AtomicBool, Ordering};

pub trait Contains {
    type T;
    fn contains(&self, val: Self::T) -> bool;
}

pub(crate) struct InitOnce {
    lock: AtomicBool,
    is_init: AtomicBool,
}

impl InitOnce {
    pub const fn new() -> Self {
        InitOnce {
            lock: AtomicBool::new(false),
            is_init: AtomicBool::new(false),
        }
    }

    pub fn init<F, R>(&self, f: F, default: R) -> R
    where
        F: Fn() -> R,
    {
        while !self.is_init.load(Ordering::Relaxed) {
            if !self.lock.load(Ordering::Relaxed)
                && self
                    .lock
                    .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
                    .is_ok()
            {
                let result = f();
                self.is_init.store(true, Ordering::Release);
                return result;
            }
        }

        default
    }
}

#[cfg(feature = "statistics")]
pub(crate) mod statistics {
    use serde::Serialize;
    use std::time::Duration;

    #[derive(Serialize, Debug)]
    pub struct SerializableTimeStat {
        pub min: Option<Duration>,
        pub max: Option<Duration>,
        pub data: Vec<f64>,
    }

    #[derive(Debug)]
    pub struct TimeStatistics<const N: usize> {
        min: Option<Duration>,
        max: Option<Duration>,
        data: Box<[Option<Duration>; N]>,
        idx: usize,
        mask: usize,
    }

    impl<const N: usize> TimeStatistics<N> {
        pub fn new() -> Self {
            assert_ne!(N, 0);

            // N must be 2^m where m >= 0.
            let mask = (1 << ((0_usize).leading_zeros() - N.leading_zeros() - 1)) - 1;
            assert_eq!(N & mask, 0);

            Self {
                min: None,
                max: None,
                data: Box::new([None; N]),
                idx: 0,
                mask,
            }
        }

        pub fn add(&mut self, dur: Duration) {
            if let Some(min) = self.min {
                if dur < min {
                    self.min = Some(dur);
                }
            } else {
                self.min = Some(dur);
            }

            if let Some(max) = self.max {
                if dur > max {
                    self.max = Some(dur);
                }
            } else {
                self.max = Some(dur);
            }

            self.data[self.idx] = Some(dur);
            self.idx += 1;
            self.idx &= self.mask;
        }

        pub fn to_serializable(&self) -> SerializableTimeStat {
            let mut data = Vec::new();
            for dur in self.data.iter().flatten() {
                data.push(dur.as_secs_f64());
            }

            SerializableTimeStat {
                min: self.min,
                max: self.max,
                data,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::helper::InitOnce;

    static INITIALIZER: InitOnce = InitOnce::new();
    static mut N: usize = 0;

    #[test]
    fn test_init_once() {
        fn init_n() {
            unsafe {
                N += 1;
            }
        }

        let th = std::thread::spawn(|| INITIALIZER.init(init_n, ()));
        INITIALIZER.init(init_n, ());
        INITIALIZER.init(init_n, ());
        INITIALIZER.init(init_n, ());
        th.join().unwrap();

        assert_eq!(unsafe { N }, 1);
    }
}