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
use duration::*;
use helpers::*;
use libc;
#[allow(unused_imports)]
use std::mem::uninitialized;
use std::ops::*;
#[allow(unused_imports)]
use std::ptr::*;
#[cfg(feature = "nightly")]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(feature = "nightly"))]
use std::sync::Mutex;
#[derive(Copy, Clone, Debug, Hash, Ord, Eq, PartialOrd, PartialEq)]
pub struct Instant(u64);
#[cfg(feature = "nightly")]
type Recent = AtomicU64;
#[cfg(feature = "nightly")]
static mut RECENT: Recent = AtomicU64::new(0);
#[cfg(not(feature = "nightly"))]
type Recent = Mutex<u64>;
#[cfg(not(feature = "nightly"))]
lazy_static! {
static ref RECENT: Recent = Mutex::new(0);
}
#[cfg(windows)]
extern "system" {
pub fn GetTickCount() -> libc::c_ulong;
}
impl Instant {
pub fn now() -> Instant {
let now = Self::_now();
Self::_update(now);
Instant(now)
}
pub fn recent() -> Instant {
match Self::_recent() {
0 => Instant::now(),
recent => Instant(recent),
}
}
pub fn update() {
let now = Self::_now();
Self::_update(now);
}
#[inline]
pub fn duration_since(&self, earlier: Instant) -> Duration {
*self - earlier
}
#[inline]
pub fn elapsed_since_recent(&self) -> Duration {
Self::recent() - *self
}
#[inline]
pub fn elapsed(&self) -> Duration {
Self::now() - *self
}
#[cfg(any(target_os = "linux", target_os = "android"))]
fn _now() -> u64 {
let mut tp: libc::timespec = unsafe { uninitialized() };
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC_COARSE, &mut tp) };
_timespec_to_u64(tp.tv_sec as u64, tp.tv_nsec as u64)
}
#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
fn _now() -> u64 {
let mut tv: libc::timeval = unsafe { uninitialized() };
unsafe { libc::gettimeofday(&mut tv, null_mut()) };
_timeval_to_u64(tv.tv_sec as u64, tv.tv_usec as u64)
}
#[cfg(windows)]
fn _now() -> u64 {
let tc = unsafe { GetTickCount() } as u64;
_millis_to_u64(tc)
}
#[cfg(feature = "nightly")]
#[inline]
fn _update(now: u64) {
unsafe { RECENT.store(now, Ordering::Relaxed) };
}
#[cfg(not(feature = "nightly"))]
#[inline]
fn _update(now: u64) {
*RECENT.lock().unwrap() = now;
}
#[cfg(feature = "nightly")]
#[inline]
fn _recent() -> u64 {
unsafe { RECENT.load(Ordering::Relaxed) }
}
#[cfg(not(feature = "nightly"))]
#[inline]
fn _recent() -> u64 {
*RECENT.lock().unwrap()
}
}
impl Default for Instant {
fn default() -> Instant {
Self::now()
}
}
impl Sub<Instant> for Instant {
type Output = Duration;
#[inline]
fn sub(self, other: Instant) -> Duration {
Duration::from_u64(self.0 - other.0)
}
}
impl Sub<Duration> for Instant {
type Output = Instant;
#[inline]
fn sub(self, rhs: Duration) -> Instant {
Instant(self.0 - rhs.as_u64())
}
}
impl SubAssign<Duration> for Instant {
#[inline]
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}
impl Add<Duration> for Instant {
type Output = Instant;
#[inline]
fn add(self, rhs: Duration) -> Instant {
Instant(self.0 + rhs.as_u64())
}
}
impl AddAssign<Duration> for Instant {
#[inline]
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}