-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep.rs
83 lines (70 loc) · 2.14 KB
/
sleep.rs
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
use std::thread;
use std::time;
/// Puts the current thread to sleep for at least the specified amount of time.
///
/// The thread may sleep longer than the duration specified due to scheduling
/// specifics or platform-dependent functionality. It will never sleep less.
///
/// This function is blocking, and should not be used in `async` functions.
///
/// # Platform-specific behavior
///
/// On Unix platforms, the underlying syscall may be interrupted by a
/// spurious wakeup or signal handler. To ensure the sleep occurs for at least
/// the specified duration, this function may invoke that system call multiple
/// times.
/// Platforms which do not support nanosecond precision for sleeping will
/// have `dur` rounded up to the nearest granularity of time they can sleep for.
///
/// Currently, specifying a zero duration on Unix platforms returns immediately
/// without invoking the underlying [`nanosleep`] syscall, whereas on Windows
/// platforms the underlying [`Sleep`] syscall is always invoked.
/// If the intention is to yield the current time-slice you may want to use
/// [`yield_now`] instead.
///
/// [`nanosleep`]: https://linux.die.net/man/2/nanosleep
/// [`Sleep`]: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
///
/// # Examples
///
/// ```no_run
/// use std::time;
/// use jkcutils_rs::time::sleep;
///
/// let now = time::Instant::now();
///
/// sleep(1);
///
/// assert!(now.elapsed() >= time::Duration::from_secs(1));
///
/// ```
#[inline]
pub fn sleep(secs: u64) {
thread::sleep(time::Duration::from_secs(secs))
}
#[inline]
pub fn sleep_s(secs: u64) {
thread::sleep(time::Duration::from_secs(secs))
}
#[inline]
pub fn sleep_ms(millis: u64) {
thread::sleep(time::Duration::from_millis(millis))
}
#[inline]
pub fn sleep_us(micros: u64) {
thread::sleep(time::Duration::from_micros(micros))
}
#[inline]
pub fn sleep_ns(nanos: u64) {
thread::sleep(time::Duration::from_nanos(nanos))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sleep() {
let now = time::Instant::now();
sleep(1);
assert!(now.elapsed() >= time::Duration::from_secs(1));
}
}