zx/
time.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Type-safe bindings for Zircon timer objects.

use crate::{ok, sys, AsHandleRef, Handle, HandleBased, HandleRef, Status};
use std::cmp::{Eq, Ord, PartialEq, PartialOrd};
use std::hash::{Hash, Hasher};
use std::{ops, time as stdtime};

/// A timestamp from the monontonic clock. Does not advance while the system is suspended.
pub type MonotonicInstant = Instant<MonotonicTimeline, NsUnit>;

/// A timestamp from a user-defined clock with arbitrary behavior.
pub type SyntheticInstant = Instant<SyntheticTimeline, NsUnit>;

/// A timestamp from the boot clock. Advances while the system is suspended.
pub type BootInstant = Instant<BootTimeline>;

/// A timestamp from system ticks. Has an arbitrary unit that can be measured with
/// `Ticks::per_second()`.
pub type Ticks<T> = Instant<T, TicksUnit>;

/// A timestamp from system ticks on the monotonic timeline. Does not advance while the system is
/// suspended.
pub type MonotonicTicks = Instant<MonotonicTimeline, TicksUnit>;

/// A timestamp from system ticks on the boot timeline. Advances while the system is suspended.
pub type BootTicks = Instant<BootTimeline, TicksUnit>;

/// A duration on the monotonic timeline.
pub type MonotonicDuration = Duration<MonotonicTimeline>;

/// A duration on the boot timeline.
pub type BootDuration = Duration<BootTimeline>;

/// A duration from a user-defined clock with arbitrary behavior.
pub type SyntheticDuration = Duration<SyntheticTimeline, NsUnit>;

/// A duration between two system ticks monotonic timestamps.
pub type MonotonicDurationTicks = Duration<MonotonicTimeline, TicksUnit>;

/// A duration between two system ticks boot timestamps.
pub type BootDurationTicks = Duration<BootTimeline, TicksUnit>;

/// A timestamp from the kernel. Generic over both the timeline and the units it is measured in.
#[repr(transparent)]
pub struct Instant<T, U = NsUnit>(sys::zx_time_t, std::marker::PhantomData<(T, U)>);

impl<T, U> Clone for Instant<T, U> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T, U> Copy for Instant<T, U> {}

impl<T, U> Default for Instant<T, U> {
    fn default() -> Self {
        Instant(0, std::marker::PhantomData)
    }
}

impl<T, U> Hash for Instant<T, U> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl<T, U> PartialEq for Instant<T, U> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}
impl<T, U> Eq for Instant<T, U> {}

impl<T, U> PartialOrd for Instant<T, U> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}
impl<T, U> Ord for Instant<T, U> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl<T, U> std::fmt::Debug for Instant<T, U> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Avoid line noise from the marker type but do include the timeline in the output.
        let timeline_name = std::any::type_name::<T>();
        let short_timeline_name =
            timeline_name.rsplit_once("::").map(|(_, n)| n).unwrap_or(timeline_name);
        let units_name = std::any::type_name::<U>();
        let short_units_name = units_name.rsplit_once("::").map(|(_, n)| n).unwrap_or(units_name);
        f.debug_tuple(&format!("Instant<{short_timeline_name}, {short_units_name}>"))
            .field(&self.0)
            .finish()
    }
}

impl MonotonicInstant {
    /// Get the current monotonic time which does not advance during system suspend.
    ///
    /// Wraps the
    /// [zx_clock_get_monotonic](https://fuchsia.dev/fuchsia-src/reference/syscalls/clock_get_monotonic.md)
    /// syscall.
    pub fn get() -> Self {
        unsafe { Self::from_nanos(sys::zx_clock_get_monotonic()) }
    }

    /// Compute a deadline for the time in the future that is the given `Duration` away.
    ///
    /// Wraps the
    /// [zx_deadline_after](https://fuchsia.dev/fuchsia-src/reference/syscalls/deadline_after.md)
    /// syscall.
    pub fn after(duration: MonotonicDuration) -> Self {
        unsafe { Self::from_nanos(sys::zx_deadline_after(duration.0)) }
    }

    /// Sleep until the given time.
    ///
    /// Wraps the
    /// [zx_nanosleep](https://fuchsia.dev/fuchsia-src/reference/syscalls/nanosleep.md)
    /// syscall.
    pub fn sleep(self) {
        unsafe {
            sys::zx_nanosleep(self.0);
        }
    }
}

impl BootInstant {
    /// Get the current boot time which advances during system suspend.
    pub fn get() -> Self {
        // SAFETY: FFI call that is always sound to call.
        unsafe { Self::from_nanos(sys::zx_clock_get_boot()) }
    }

    /// Compute a deadline for the time in the future that is the given `Duration` away.
    pub fn after(duration: BootDuration) -> Self {
        Self::from_nanos(Self::get().into_nanos().saturating_add(duration.0))
    }
}

impl<T: Timeline, U: TimeUnit> Instant<T, U> {
    pub const ZERO: Instant<T, U> = Instant(0, std::marker::PhantomData);
}

impl<T: Timeline> Instant<T> {
    pub const INFINITE: Instant<T, NsUnit> =
        Instant(sys::ZX_TIME_INFINITE, std::marker::PhantomData);
    pub const INFINITE_PAST: Instant<T, NsUnit> =
        Instant(sys::ZX_TIME_INFINITE_PAST, std::marker::PhantomData);

    /// Returns the number of nanoseconds since the epoch contained by this `Time`.
    pub const fn into_nanos(self) -> i64 {
        self.0
    }

    /// Return a strongly-typed `Time` from a raw number of nanoseconds.
    pub const fn from_nanos(nanos: i64) -> Self {
        Instant(nanos, std::marker::PhantomData)
    }
}

impl MonotonicTicks {
    /// Read the number of high-precision timer ticks on the monotonic timeline. These ticks may be
    /// processor cycles, high speed timer, profiling timer, etc. They do not advance while the
    /// system is suspended.
    ///
    /// Wraps the
    /// [zx_ticks_get](https://fuchsia.dev/fuchsia-src/reference/syscalls/ticks_get.md)
    /// syscall.
    pub fn get() -> Self {
        // SAFETY: FFI call that is always sound to call.
        Self(unsafe { sys::zx_ticks_get() }, std::marker::PhantomData)
    }
}

impl BootTicks {
    /// Read the number of high-precision timer ticks on the boot timeline. These ticks may be
    /// processor cycles, high speed timer, profiling timer, etc. They advance while the
    /// system is suspended.
    pub fn get() -> Self {
        // SAFETY: FFI call that is always sound to call.
        Self(unsafe { sys::zx_ticks_get_boot() }, std::marker::PhantomData)
    }
}

impl<T: Timeline> Ticks<T> {
    /// Return the number of ticks contained by this `Ticks`.
    pub const fn into_raw(self) -> i64 {
        self.0
    }

    /// Return a strongly-typed `Ticks` from a raw number of system ticks.
    pub const fn from_raw(raw: i64) -> Self {
        Self(raw, std::marker::PhantomData)
    }

    /// Return the number of high-precision timer ticks in a second.
    ///
    /// Wraps the
    /// [zx_ticks_per_second](https://fuchsia.dev/fuchsia-src/reference/syscalls/ticks_per_second.md)
    /// syscall.
    pub fn per_second() -> i64 {
        // SAFETY: FFI call that is always sound to call.
        unsafe { sys::zx_ticks_per_second() }
    }
}

impl<T: Timeline, U: TimeUnit> ops::Add<Duration<T, U>> for Instant<T, U> {
    type Output = Instant<T, U>;
    fn add(self, dur: Duration<T, U>) -> Self::Output {
        Self(self.0.saturating_add(dur.0), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::Sub<Duration<T, U>> for Instant<T, U> {
    type Output = Instant<T, U>;
    fn sub(self, dur: Duration<T, U>) -> Self::Output {
        Self(self.0.saturating_sub(dur.0), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::Sub<Instant<T, U>> for Instant<T, U> {
    type Output = Duration<T, U>;
    fn sub(self, rhs: Instant<T, U>) -> Self::Output {
        Duration(self.0.saturating_sub(rhs.0), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::AddAssign<Duration<T, U>> for Instant<T, U> {
    fn add_assign(&mut self, dur: Duration<T, U>) {
        self.0 = self.0.saturating_add(dur.0);
    }
}

impl<T: Timeline, U: TimeUnit> ops::SubAssign<Duration<T, U>> for Instant<T, U> {
    fn sub_assign(&mut self, dur: Duration<T, U>) {
        self.0 = self.0.saturating_sub(dur.0);
    }
}

/// A marker trait for times to prevent accidental comparison between different timelines.
pub trait Timeline {}

/// A marker type for the system's monotonic timeline which pauses during suspend.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct MonotonicTimeline;
impl Timeline for MonotonicTimeline {}

/// A marker type for the system's boot timeline which continues running during suspend.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct BootTimeline;
impl Timeline for BootTimeline {}

/// A marker type representing a synthetic timeline defined by a kernel clock object.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct SyntheticTimeline;
impl Timeline for SyntheticTimeline {}

/// A marker trait for times and durations to prevent accidental comparison between different units.
pub trait TimeUnit {}

/// A marker type representing nanoseconds.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NsUnit;
impl TimeUnit for NsUnit {}

/// A marker type representing system ticks.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TicksUnit;
impl TimeUnit for TicksUnit {}

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Duration<T, U = NsUnit>(sys::zx_duration_t, std::marker::PhantomData<(T, U)>);

impl<T: Timeline> From<stdtime::Duration> for Duration<T, NsUnit> {
    fn from(dur: stdtime::Duration) -> Self {
        Duration::from_seconds(dur.as_secs() as i64)
            + Duration::from_nanos(dur.subsec_nanos() as i64)
    }
}

impl<T: Timeline, U: TimeUnit> ops::Add<Instant<T, U>> for Duration<T, U> {
    type Output = Instant<T, U>;
    fn add(self, time: Instant<T, U>) -> Self::Output {
        Instant(self.0.saturating_add(time.0), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::Add for Duration<T, U> {
    type Output = Duration<T, U>;
    fn add(self, rhs: Duration<T, U>) -> Self::Output {
        Self(self.0.saturating_add(rhs.0), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::Sub for Duration<T, U> {
    type Output = Duration<T, U>;
    fn sub(self, rhs: Duration<T, U>) -> Duration<T, U> {
        Self(self.0.saturating_sub(rhs.0), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::AddAssign for Duration<T, U> {
    fn add_assign(&mut self, rhs: Duration<T, U>) {
        self.0 = self.0.saturating_add(rhs.0);
    }
}

impl<T: Timeline, U: TimeUnit> ops::SubAssign for Duration<T, U> {
    fn sub_assign(&mut self, rhs: Duration<T, U>) {
        self.0 = self.0.saturating_sub(rhs.0);
    }
}

impl<T: Timeline, S: Into<i64>, U: TimeUnit> ops::Mul<S> for Duration<T, U> {
    type Output = Self;
    fn mul(self, mul: S) -> Self {
        Self(self.0.saturating_mul(mul.into()), std::marker::PhantomData)
    }
}

impl<S: Into<i64>, T: Timeline, U: TimeUnit> ops::Div<S> for Duration<T, U> {
    type Output = Self;
    fn div(self, div: S) -> Self {
        Self(self.0.saturating_div(div.into()), std::marker::PhantomData)
    }
}

impl<T: Timeline, U: TimeUnit> ops::Neg for Duration<T, U> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self(self.0.saturating_neg(), std::marker::PhantomData)
    }
}

impl<T: Timeline> Duration<T, NsUnit> {
    pub const INFINITE: Duration<T> = Duration(sys::zx_duration_t::MAX, std::marker::PhantomData);
    pub const INFINITE_PAST: Duration<T> =
        Duration(sys::zx_duration_t::MIN, std::marker::PhantomData);
    pub const ZERO: Duration<T> = Duration(0, std::marker::PhantomData);

    /// Returns the number of nanoseconds contained by this `Duration`.
    pub const fn into_nanos(self) -> i64 {
        self.0
    }

    /// Returns the total number of whole microseconds contained by this `Duration`.
    pub const fn into_micros(self) -> i64 {
        self.0 / 1_000
    }

    /// Returns the total number of whole milliseconds contained by this `Duration`.
    pub const fn into_millis(self) -> i64 {
        self.into_micros() / 1_000
    }

    /// Returns the total number of whole seconds contained by this `Duration`.
    pub const fn into_seconds(self) -> i64 {
        self.into_millis() / 1_000
    }

    /// Returns the duration as a floating-point value in seconds.
    pub fn into_seconds_f64(self) -> f64 {
        self.into_nanos() as f64 / 1_000_000_000f64
    }

    /// Returns the total number of whole minutes contained by this `Duration`.
    pub const fn into_minutes(self) -> i64 {
        self.into_seconds() / 60
    }

    /// Returns the total number of whole hours contained by this `Duration`.
    pub const fn into_hours(self) -> i64 {
        self.into_minutes() / 60
    }

    pub const fn from_nanos(nanos: i64) -> Self {
        Duration(nanos, std::marker::PhantomData)
    }

    pub const fn from_micros(micros: i64) -> Self {
        Duration(micros.saturating_mul(1_000), std::marker::PhantomData)
    }

    pub const fn from_millis(millis: i64) -> Self {
        Duration::from_micros(millis.saturating_mul(1_000))
    }

    pub const fn from_seconds(secs: i64) -> Self {
        Duration::from_millis(secs.saturating_mul(1_000))
    }

    pub const fn from_minutes(min: i64) -> Self {
        Duration::from_seconds(min.saturating_mul(60))
    }

    pub const fn from_hours(hours: i64) -> Self {
        Duration::from_minutes(hours.saturating_mul(60))
    }
}

impl<T: Timeline> Duration<T, TicksUnit> {
    /// Return the raw number of ticks represented by this `Duration`.
    pub const fn into_raw(self) -> i64 {
        self.0
    }

    /// Return a typed wrapper around the provided number of ticks.
    pub const fn from_raw(raw: i64) -> Self {
        Self(raw, std::marker::PhantomData)
    }
}

impl MonotonicDuration {
    /// Sleep for the given amount of time.
    pub fn sleep(self) {
        MonotonicInstant::after(self).sleep()
    }
}

/// An object representing a Zircon timer, such as the one returned by
/// [zx_timer_create](https://fuchsia.dev/fuchsia-src/reference/syscalls/timer_create.md).
///
/// As essentially a subtype of `Handle`, it can be freely interconverted.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
// TODO(https://fxbug.dev/361661898) remove default type when FIDL understands mono vs. boot timers
pub struct Timer<T = MonotonicTimeline>(Handle, std::marker::PhantomData<T>);

/// A timer that measures its deadlines against the monotonic clock.
pub type MonotonicTimer = Timer<MonotonicTimeline>;

/// A timer that measures its deadlines against the boot clock.
pub type BootTimer = Timer<BootTimeline>;

impl Timer<MonotonicTimeline> {
    /// Create a timer, an object that can signal when a specified point on the monotonic clock has
    /// been reached. Wraps the
    /// [zx_timer_create](https://fuchsia.dev/fuchsia-src/reference/syscalls/timer_create.md)
    /// syscall.
    ///
    /// # Panics
    ///
    /// If the kernel reports no memory available to create a timer or the process' job policy
    /// denies timer creation.
    pub fn create() -> Self {
        let mut out = 0;
        let opts = 0;
        let status = unsafe {
            sys::zx_timer_create(opts, 0 /*ZX_CLOCK_MONOTONIC*/, &mut out)
        };
        ok(status)
            .expect("timer creation always succeeds except with OOM or when job policy denies it");
        unsafe { Self::from(Handle::from_raw(out)) }
    }
}

impl Timer<BootTimeline> {
    /// Create a timer, an object that can signal when a specified point on the boot clock has been
    /// reached. Wraps the
    /// [zx_timer_create](https://fuchsia.dev/fuchsia-src/reference/syscalls/timer_create.md)
    /// syscall.
    ///
    /// If the timer elapses while the system is suspended it will not wake the system.
    ///
    /// # Panics
    ///
    /// If the kernel reports no memory available to create a timer or the process' job policy
    /// denies timer creation.
    pub fn create() -> Self {
        // TODO(https://fxbug.dev/328306129) change the clock ID to the boot clock
        Self(MonotonicTimer::create().0, std::marker::PhantomData)
    }
}

impl<T: Timeline> Timer<T> {
    /// Start a one-shot timer that will fire when `deadline` passes. Wraps the
    /// [zx_timer_set](https://fuchsia.dev/fuchsia-src/reference/syscalls/timer_set.md)
    /// syscall.
    pub fn set(&self, deadline: Instant<T>, slack: Duration<T, NsUnit>) -> Result<(), Status> {
        let status = unsafe {
            sys::zx_timer_set(self.raw_handle(), deadline.into_nanos(), slack.into_nanos())
        };
        ok(status)
    }

    /// Cancels a pending timer that was started with set(). Wraps the
    /// [zx_timer_cancel](https://fuchsia.dev/fuchsia-src/reference/syscalls/timer_cancel.md)
    /// syscall.
    pub fn cancel(&self) -> Result<(), Status> {
        let status = unsafe { sys::zx_timer_cancel(self.raw_handle()) };
        ok(status)
    }
}

impl<T: Timeline> AsHandleRef for Timer<T> {
    fn as_handle_ref(&self) -> HandleRef<'_> {
        self.0.as_handle_ref()
    }
}

impl<T: Timeline> From<Handle> for Timer<T> {
    fn from(handle: Handle) -> Self {
        Timer(handle, std::marker::PhantomData)
    }
}

impl<T: Timeline> From<Timer<T>> for Handle {
    fn from(x: Timer<T>) -> Handle {
        x.0
    }
}

impl<T: Timeline> HandleBased for Timer<T> {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Signals;

    #[test]
    fn time_debug_repr_is_short() {
        assert_eq!(
            format!("{:?}", MonotonicInstant::from_nanos(0)),
            "Instant<MonotonicTimeline, NsUnit>(0)"
        );
        assert_eq!(
            format!("{:?}", SyntheticInstant::from_nanos(0)),
            "Instant<SyntheticTimeline, NsUnit>(0)"
        );
    }

    #[test]
    fn monotonic_time_increases() {
        let time1 = MonotonicInstant::get();
        Duration::from_nanos(1_000).sleep();
        let time2 = MonotonicInstant::get();
        assert!(time2 > time1);
    }

    #[test]
    fn ticks_increases() {
        let ticks1 = MonotonicTicks::get();
        Duration::from_nanos(1_000).sleep();
        let ticks2 = MonotonicTicks::get();
        assert!(ticks2 > ticks1);
    }

    #[test]
    fn boot_time_increases() {
        let time1 = BootInstant::get();
        Duration::from_nanos(1_000).sleep();
        let time2 = BootInstant::get();
        assert!(time2 > time1);
    }

    #[test]
    fn boot_ticks_increases() {
        let ticks1 = BootTicks::get();
        Duration::from_nanos(1_000).sleep();
        let ticks2 = BootTicks::get();
        assert!(ticks2 > ticks1);
    }

    #[test]
    fn tick_length() {
        let sleep_time = Duration::from_millis(1);
        let ticks1 = MonotonicTicks::get();
        sleep_time.sleep();
        let ticks2 = MonotonicTicks::get();

        // The number of ticks should have increased by at least 1 ms worth
        let sleep_ticks = MonotonicDurationTicks::from_raw(
            sleep_time.into_millis() * (MonotonicTicks::per_second() / 1000),
        );
        assert!(ticks2 >= (ticks1 + sleep_ticks));
    }

    #[test]
    fn sleep() {
        let sleep_ns = Duration::from_millis(1);
        let time1 = MonotonicInstant::get();
        sleep_ns.sleep();
        let time2 = MonotonicInstant::get();
        assert!(time2 > time1 + sleep_ns);
    }

    #[test]
    fn from_std() {
        let std_dur = stdtime::Duration::new(25, 25);
        let dur = MonotonicDuration::from(std_dur);
        let std_dur_nanos = (1_000_000_000 * std_dur.as_secs()) + std_dur.subsec_nanos() as u64;
        assert_eq!(std_dur_nanos as i64, dur.into_nanos());
    }

    #[test]
    fn i64_conversions() {
        let nanos_in_one_hour = 3_600_000_000_000;
        let dur_from_nanos = MonotonicDuration::from_nanos(nanos_in_one_hour);
        let dur_from_hours = MonotonicDuration::from_hours(1);
        assert_eq!(dur_from_nanos, dur_from_hours);
        assert_eq!(dur_from_nanos.into_nanos(), dur_from_hours.into_nanos());
        assert_eq!(dur_from_nanos.into_nanos(), nanos_in_one_hour);
        assert_eq!(dur_from_nanos.into_hours(), 1);
    }

    #[test]
    fn timer_basic() {
        let slack = Duration::from_millis(0);
        let ten_ms = Duration::from_millis(10);
        let five_secs = Duration::from_seconds(5);

        // Create a timer
        let timer = MonotonicTimer::create();

        // Should not signal yet.
        assert_eq!(
            timer.wait_handle(Signals::TIMER_SIGNALED, MonotonicInstant::after(ten_ms)),
            Err(Status::TIMED_OUT)
        );

        // Set it, and soon it should signal.
        assert_eq!(timer.set(MonotonicInstant::after(five_secs), slack), Ok(()));
        assert_eq!(
            timer.wait_handle(Signals::TIMER_SIGNALED, Instant::INFINITE),
            Ok(Signals::TIMER_SIGNALED)
        );

        // Cancel it, and it should stop signalling.
        assert_eq!(timer.cancel(), Ok(()));
        assert_eq!(
            timer.wait_handle(Signals::TIMER_SIGNALED, MonotonicInstant::after(ten_ms)),
            Err(Status::TIMED_OUT)
        );
    }

    #[test]
    fn boot_timer_basic() {
        let slack = Duration::from_millis(0);
        let ten_ms = Duration::from_millis(10);
        let five_secs = Duration::from_seconds(5);

        // Create a timer
        let timer = BootTimer::create();

        // Should not signal yet.
        assert_eq!(
            timer.wait_handle(Signals::TIMER_SIGNALED, MonotonicInstant::after(ten_ms)),
            Err(Status::TIMED_OUT)
        );

        // Set it, and soon it should signal.
        assert_eq!(timer.set(BootInstant::get() + five_secs, slack), Ok(()));
        assert_eq!(
            timer.wait_handle(Signals::TIMER_SIGNALED, Instant::INFINITE),
            Ok(Signals::TIMER_SIGNALED)
        );

        // Cancel it, and it should stop signalling.
        assert_eq!(timer.cancel(), Ok(()));
        assert_eq!(
            timer.wait_handle(Signals::TIMER_SIGNALED, MonotonicInstant::after(ten_ms)),
            Err(Status::TIMED_OUT)
        );
    }

    #[test]
    fn time_minus_time() {
        let lhs = MonotonicInstant::from_nanos(10);
        let rhs = MonotonicInstant::from_nanos(30);
        assert_eq!(lhs - rhs, Duration::from_nanos(-20));
    }

    #[test]
    fn time_saturation() {
        // Addition
        assert_eq!(
            MonotonicInstant::from_nanos(10) + Duration::from_nanos(30),
            MonotonicInstant::from_nanos(40)
        );
        assert_eq!(
            MonotonicInstant::from_nanos(10) + Duration::INFINITE,
            MonotonicInstant::INFINITE
        );
        assert_eq!(
            MonotonicInstant::from_nanos(-10) + Duration::INFINITE_PAST,
            MonotonicInstant::INFINITE_PAST
        );

        // Subtraction
        assert_eq!(
            MonotonicInstant::from_nanos(10) - Duration::from_nanos(30),
            MonotonicInstant::from_nanos(-20)
        );
        assert_eq!(
            MonotonicInstant::from_nanos(-10) - Duration::INFINITE,
            MonotonicInstant::INFINITE_PAST
        );
        assert_eq!(
            MonotonicInstant::from_nanos(10) - Duration::INFINITE_PAST,
            MonotonicInstant::INFINITE
        );

        // Assigning addition
        {
            let mut t = MonotonicInstant::from_nanos(10);
            t += Duration::from_nanos(30);
            assert_eq!(t, MonotonicInstant::from_nanos(40));
        }
        {
            let mut t = MonotonicInstant::from_nanos(10);
            t += Duration::INFINITE;
            assert_eq!(t, MonotonicInstant::INFINITE);
        }
        {
            let mut t = MonotonicInstant::from_nanos(-10);
            t += Duration::INFINITE_PAST;
            assert_eq!(t, MonotonicInstant::INFINITE_PAST);
        }

        // Assigning subtraction
        {
            let mut t = MonotonicInstant::from_nanos(10);
            t -= Duration::from_nanos(30);
            assert_eq!(t, MonotonicInstant::from_nanos(-20));
        }
        {
            let mut t = MonotonicInstant::from_nanos(-10);
            t -= Duration::INFINITE;
            assert_eq!(t, MonotonicInstant::INFINITE_PAST);
        }
        {
            let mut t = MonotonicInstant::from_nanos(10);
            t -= Duration::INFINITE_PAST;
            assert_eq!(t, MonotonicInstant::INFINITE);
        }
    }

    #[test]
    fn duration_saturation() {
        // Addition
        assert_eq!(
            MonotonicDuration::from_nanos(10) + Duration::from_nanos(30),
            Duration::from_nanos(40)
        );
        assert_eq!(MonotonicDuration::from_nanos(10) + Duration::INFINITE, Duration::INFINITE);
        assert_eq!(
            MonotonicDuration::from_nanos(-10) + Duration::INFINITE_PAST,
            Duration::INFINITE_PAST
        );

        // Subtraction
        assert_eq!(
            MonotonicDuration::from_nanos(10) - Duration::from_nanos(30),
            Duration::from_nanos(-20)
        );
        assert_eq!(
            MonotonicDuration::from_nanos(-10) - Duration::INFINITE,
            Duration::INFINITE_PAST
        );
        assert_eq!(MonotonicDuration::from_nanos(10) - Duration::INFINITE_PAST, Duration::INFINITE);

        // Multiplication
        assert_eq!(MonotonicDuration::from_nanos(10) * 3, Duration::from_nanos(30));
        assert_eq!(MonotonicDuration::from_nanos(10) * i64::MAX, Duration::INFINITE);
        assert_eq!(MonotonicDuration::from_nanos(10) * i64::MIN, Duration::INFINITE_PAST);

        // Division
        assert_eq!(MonotonicDuration::from_nanos(30) / 3, Duration::from_nanos(10));
        assert_eq!(MonotonicDuration::INFINITE_PAST / -1, Duration::INFINITE);

        // Negation
        assert_eq!(-MonotonicDuration::from_nanos(30), Duration::from_nanos(-30));
        assert_eq!(-MonotonicDuration::INFINITE_PAST, Duration::INFINITE);

        // Assigning addition
        {
            let mut t = MonotonicDuration::from_nanos(10);
            t += Duration::from_nanos(30);
            assert_eq!(t, Duration::from_nanos(40));
        }
        {
            let mut t = MonotonicDuration::from_nanos(10);
            t += Duration::INFINITE;
            assert_eq!(t, Duration::INFINITE);
        }
        {
            let mut t = MonotonicDuration::from_nanos(-10);
            t += Duration::INFINITE_PAST;
            assert_eq!(t, Duration::INFINITE_PAST);
        }

        // Assigning subtraction
        {
            let mut t = MonotonicDuration::from_nanos(10);
            t -= Duration::from_nanos(30);
            assert_eq!(t, Duration::from_nanos(-20));
        }
        {
            let mut t = MonotonicDuration::from_nanos(-10);
            t -= Duration::INFINITE;
            assert_eq!(t, Duration::INFINITE_PAST);
        }
        {
            let mut t = MonotonicDuration::from_nanos(10);
            t -= Duration::INFINITE_PAST;
            assert_eq!(t, Duration::INFINITE);
        }
    }

    #[test]
    fn time_minus_time_saturates() {
        assert_eq!(
            MonotonicInstant::INFINITE - MonotonicInstant::INFINITE_PAST,
            Duration::INFINITE
        );
    }

    #[test]
    fn time_and_duration_defaults() {
        assert_eq!(MonotonicInstant::default(), MonotonicInstant::from_nanos(0));
        assert_eq!(Duration::default(), MonotonicDuration::from_nanos(0));
    }
}