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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::constants::PoolState;
use crate::coroutine::suspender::SimpleDelaySuspender;
use crate::scheduler::SchedulableSuspender;
use std::fmt::Debug;
use std::io::{Error, ErrorKind};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

#[allow(clippy::pedantic, missing_docs)]
pub fn page_size() -> usize {
    static PAGE_SIZE: AtomicUsize = AtomicUsize::new(0);
    let mut ret = PAGE_SIZE.load(Ordering::Relaxed);
    if ret == 0 {
        unsafe {
            cfg_if::cfg_if! {
                if #[cfg(windows)] {
                    let mut info = std::mem::zeroed();
                    windows_sys::Win32::System::SystemInformation::GetSystemInfo(&mut info);
                    ret = info.dwPageSize as usize
                } else {
                    ret = libc::sysconf(libc::_SC_PAGESIZE) as usize;
                }
            }
        }
        PAGE_SIZE.store(ret, Ordering::Relaxed);
    }
    ret
}

/// Give the object a name.
pub trait Named {
    /// Get the name of this object.
    fn get_name(&self) -> &str;
}

/// A trait implemented for which needs `current()`.
pub trait Current<'c> {
    /// Init the current.
    fn init_current(current: &Self)
    where
        Self: Sized;

    /// Get the current if has.
    fn current() -> Option<&'c Self>
    where
        Self: Sized;

    /// clean the current.
    fn clean_current()
    where
        Self: Sized;
}

/// A trait for blocking current thread.
pub trait Blocker: Debug + Named {
    /// Block current thread for a while.
    fn block(&self, dur: Duration);
}

#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct CondvarBlocker(std::sync::Mutex<()>, std::sync::Condvar);

/// const `CONDVAR_BLOCKER_NAME`.
pub const CONDVAR_BLOCKER_NAME: &str = "CondvarBlocker";

impl Named for CondvarBlocker {
    fn get_name(&self) -> &str {
        CONDVAR_BLOCKER_NAME
    }
}

impl Blocker for CondvarBlocker {
    fn block(&self, dur: Duration) {
        _ = self.1.wait_timeout(self.0.lock().unwrap(), dur);
    }
}

#[allow(missing_docs)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub struct DelayBlocker {}

/// const `DELAY_BLOCKER_NAME`.
pub const DELAY_BLOCKER_NAME: &str = "DelayBlocker";

impl Named for DelayBlocker {
    fn get_name(&self) -> &str {
        DELAY_BLOCKER_NAME
    }
}

impl Blocker for DelayBlocker {
    fn block(&self, dur: Duration) {
        if let Some(suspender) = SchedulableSuspender::current() {
            suspender.delay(dur);
        }
    }
}

/// Join abstraction.
pub trait JoinHandle<T> {
    /// create `JoinHandle` instance.
    #[must_use]
    fn err() -> Self
    where
        Self: Sized,
    {
        Self::new(std::ptr::null(), "")
    }

    /// create `JoinHandle` instance.
    fn new(t: *const T, name: &str) -> Self;

    /// get the task name.
    ///
    /// # Errors
    /// if the task name is invalid.
    fn get_name(&self) -> std::io::Result<&str>;

    /// join with `Duration`.
    ///
    /// # Errors
    /// see `timeout_at_join`.
    fn timeout_join(&self, dur: Duration) -> std::io::Result<Result<Option<usize>, &str>> {
        self.timeout_at_join(open_coroutine_timer::get_timeout_time(dur))
    }

    /// join.
    ///
    /// # Errors
    /// see `timeout_at_join`.
    fn join(&self) -> std::io::Result<Result<Option<usize>, &str>> {
        self.timeout_at_join(u64::MAX)
    }

    /// join with timeout.
    ///
    /// # Errors
    /// if join failed.
    fn timeout_at_join(&self, timeout_time: u64) -> std::io::Result<Result<Option<usize>, &str>>;
}

/// The `Pool` abstraction.
pub trait Pool: Debug {
    /// Set the minimum number in this pool (the meaning of this number
    /// depends on the specific implementation).
    fn set_min_size(&self, min_size: usize);

    /// Get the minimum number in this pool (the meaning of this number
    /// depends on the specific implementation).
    fn get_min_size(&self) -> usize;

    /// Gets the number currently running in this pool.
    fn get_running_size(&self) -> usize;

    /// Set the maximum number in this pool (the meaning of this number
    /// depends on the specific implementation).
    fn set_max_size(&self, max_size: usize);

    /// Get the maximum number in this pool (the meaning of this number
    /// depends on the specific implementation).
    fn get_max_size(&self) -> usize;

    /// Set the maximum idle time running in this pool.
    /// `keep_alive_time` has `ns` units.
    fn set_keep_alive_time(&self, keep_alive_time: u64);

    /// Get the maximum idle time running in this pool.
    /// Returns in `ns` units.
    fn get_keep_alive_time(&self) -> u64;
}

/// The `StatePool` abstraction.
pub trait StatePool: Pool + Named {
    /// Get the state of this pool.
    fn state(&self) -> PoolState;

    /// Change the state of this pool.
    fn change_state(&self, state: PoolState) -> PoolState;

    /// created -> running
    ///
    /// # Errors
    /// if change state fails.
    fn running(&self, sync: bool) -> std::io::Result<()> {
        let current = self.state();
        match current {
            PoolState::Created => {
                let state = PoolState::Running(sync);
                _ = self.change_state(state);
                crate::info!("{} {:?}->{:?}", self.get_name(), current, state);
                return Ok(());
            }
            PoolState::Running(pre) => {
                if pre != sync {
                    let state = PoolState::Running(sync);
                    _ = self.change_state(state);
                    crate::info!("{} {:?}->{:?}", self.get_name(), current, state);
                }
                return Ok(());
            }
            _ => {}
        }
        Err(Error::new(
            ErrorKind::Other,
            format!(
                "{} unexpected {current}->{:?}",
                self.get_name(),
                PoolState::Running(sync)
            ),
        ))
    }

    /// running -> stopping
    /// stopping -> stopped
    ///
    /// # Errors
    /// if change state fails.
    fn end(&self) -> std::io::Result<()> {
        let current = self.state();
        match current {
            PoolState::Running(sync) => {
                let state = PoolState::Stopping(sync);
                _ = self.change_state(state);
                crate::info!("{} {:?}->{:?}", self.get_name(), current, state);
                return Ok(());
            }
            PoolState::Stopping(_) => {
                let state = PoolState::Stopped;
                _ = self.change_state(state);
                crate::info!("{} {:?}->{:?}", self.get_name(), current, state);
                return Ok(());
            }
            PoolState::Stopped => return Ok(()),
            PoolState::Created => {}
        }
        Err(Error::new(
            ErrorKind::Other,
            format!(
                "{} unexpected {current}->{:?}",
                self.get_name(),
                PoolState::Stopped
            ),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cmp::Ordering;

    #[test]
    fn condvar_blocker() {
        let blocker = CondvarBlocker::default();
        let time = open_coroutine_timer::now();
        blocker.block(Duration::from_secs(1));
        let cost = Duration::from_nanos(open_coroutine_timer::now().saturating_sub(time));
        if Ordering::Less == cost.cmp(&Duration::from_secs(1)) {
            crate::error!("condvar_blocker cost {cost:?}");
        }
    }
}