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
use duration::*;
#[allow(unused_imports)]
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;

/// A measurement of a monotonically increasing clock. Opaque and useful only with `Duration`.
#[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;
}

#[cfg(any(all(feature = "sierra", target_os = "macos"), target_os = "freebsd"))]
#[allow(non_camel_case_types)]
type clockid_t = libc::c_int;

#[cfg(all(feature = "sierra", target_os = "macos"))]
const CLOCK_MONOTONIC_RAW_APPROX: clockid_t = 5;

#[cfg(all(feature = "sierra", target_os = "macos"))]
extern "system" {
    pub fn clock_gettime_nsec_np(clk_id: clockid_t) -> u64;
}

#[cfg(target_os = "freebsd")]
const CLOCK_MONOTONIC_FAST: clockid_t = 12;

impl Instant {
    /// Returns an instant corresponding to "now"
    ///
    /// This function also updates the stored instant.
    pub fn now() -> Instant {
        let now = Self::_now();
        Self::_update(now);
        Instant(now)
    }

    /// Returns an instant corresponding to the latest update
    pub fn recent() -> Instant {
        match Self::_recent() {
            0 => Instant::now(),
            recent => Instant(recent),
        }
    }

    /// Update the stored instant
    ///
    /// This function should be called frequently, for example in an event loop or using an
    /// `Updater` task.
    pub fn update() {
        let now = Self::_now();
        Self::_update(now);
    }

    /// Returns the amount of time elapsed from another instant to this one
    #[inline]
    pub fn duration_since(&self, earlier: Instant) -> Duration {
        *self - earlier
    }

    /// Returns the amount of time elapsed between the this instant was created and the latest
    /// update
    #[inline]
    pub fn elapsed_since_recent(&self) -> Duration {
        Self::recent() - *self
    }

    /// Returns the amount of time elapsed since this instant was created
    ///
    /// This function also updates the stored instant.
    #[inline]
    pub fn elapsed(&self) -> Duration {
        Self::now() - *self
    }

    #[doc(hidden)]
    #[inline]
    pub fn as_u64(&self) -> u64 {
        self.0
    }

    #[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 u32)
    }

    #[cfg(all(feature = "sierra", target_os = "macos"))]
    fn _now() -> u64 {
        let nsec = unsafe { clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW_APPROX) };
        _nsecs_to_u64(nsec)
    }

    #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
    fn _now() -> u64 {
        let mut tp: libc::timespec = unsafe { uninitialized() };
        unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC_FAST, &mut tp) };
        _timespec_to_u64(tp.tv_sec as u64, tp.tv_nsec as u32)
    }

    #[cfg(all(unix, not(any(all(feature = "sierra", target_os = "macos"),
                            target_os = "linux", target_os = "android",
                            target_os = "freebsd", target_os = "dragonfly"))))]
    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 u32)
    }

    #[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;

    #[cfg(all(unix, not(any(all(feature = "sierra", target_os = "macos"),
                            target_os = "linux", target_os = "android",
                            target_os = "freebsd", target_os = "dragonfly"))))]
    #[inline]
    fn sub(self, other: Instant) -> Duration {
        Duration::from_u64(self.0.saturating_sub(other.0))
    }

    #[cfg(not(all(unix, not(any(all(feature = "sierra", target_os = "macos"),
                            target_os = "linux", target_os = "android",
                            target_os = "freebsd", target_os = "dragonfly")))))]
    #[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;
    }
}