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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
|
// SPDX-License-Identifier: GPL-2.0
//! Intrusive high resolution timers.
//!
//! Allows running timer callbacks without doing allocations at the time of
//! starting the timer. For now, only one timer per type is allowed.
//!
//! # Vocabulary
//!
//! States:
//!
//! - Stopped: initialized but not started, or cancelled, or not restarted.
//! - Started: initialized and started or restarted.
//! - Running: executing the callback.
//!
//! Operations:
//!
//! * Start
//! * Cancel
//! * Restart
//!
//! Events:
//!
//! * Expire
//!
//! ## State Diagram
//!
//! ```text
//! Return NoRestart
//! +---------------------------------------------------------------------+
//! | |
//! | |
//! | |
//! | Return Restart |
//! | +------------------------+ |
//! | | | |
//! | | | |
//! v v | |
//! +-----------------+ Start +------------------+ +--------+-----+--+
//! | +---------------->| | | |
//! Init | | | | Expire | |
//! --------->| Stopped | | Started +---------->| Running |
//! | | Cancel | | | |
//! | |<----------------+ | | |
//! +-----------------+ +---------------+--+ +-----------------+
//! ^ |
//! | |
//! +---------+
//! Restart
//! ```
//!
//!
//! A timer is initialized in the **stopped** state. A stopped timer can be
//! **started** by the `start` operation, with an **expiry** time. After the
//! `start` operation, the timer is in the **started** state. When the timer
//! **expires**, the timer enters the **running** state and the handler is
//! executed. After the handler has returned, the timer may enter the
//! **started* or **stopped** state, depending on the return value of the
//! handler. A timer in the **started** or **running** state may be **canceled**
//! by the `cancel` operation. A timer that is cancelled enters the **stopped**
//! state.
//!
//! A `cancel` or `restart` operation on a timer in the **running** state takes
//! effect after the handler has returned and the timer has transitioned
//! out of the **running** state.
//!
//! A `restart` operation on a timer in the **stopped** state is equivalent to a
//! `start` operation.
//!
//! When a type implements both `HrTimerPointer` and `Clone`, it is possible to
//! issue the `start` operation while the timer is in the **started** state. In
//! this case the `start` operation is equivalent to the `restart` operation.
//!
//! # Examples
//!
//! ## Using an intrusive timer living in a [`Box`]
//!
//! ```
//! # use kernel::{
//! # alloc::flags,
//! # impl_has_hr_timer,
//! # prelude::*,
//! # sync::{
//! # atomic::{ordering, Atomic},
//! # completion::Completion,
//! # Arc,
//! # },
//! # time::{
//! # hrtimer::{
//! # RelativeMode, HrTimer, HrTimerCallback, HrTimerPointer,
//! # HrTimerRestart, HrTimerCallbackContext
//! # },
//! # Delta, Monotonic,
//! # },
//! # };
//!
//! #[pin_data]
//! struct Shared {
//! #[pin]
//! flag: Atomic<u64>,
//! #[pin]
//! cond: Completion,
//! }
//!
//! impl Shared {
//! fn new() -> impl PinInit<Self> {
//! pin_init!(Self {
//! flag <- Atomic::new(0),
//! cond <- Completion::new(),
//! })
//! }
//! }
//!
//! #[pin_data]
//! struct BoxIntrusiveHrTimer {
//! #[pin]
//! timer: HrTimer<Self>,
//! shared: Arc<Shared>,
//! }
//!
//! impl BoxIntrusiveHrTimer {
//! fn new() -> impl PinInit<Self, kernel::error::Error> {
//! try_pin_init!(Self {
//! timer <- HrTimer::new(),
//! shared: Arc::pin_init(Shared::new(), flags::GFP_KERNEL)?,
//! })
//! }
//! }
//!
//! impl HrTimerCallback for BoxIntrusiveHrTimer {
//! type Pointer<'a> = Pin<KBox<Self>>;
//!
//! fn run(this: Pin<&mut Self>, _ctx: HrTimerCallbackContext<'_, Self>) -> HrTimerRestart {
//! pr_info!("Timer called\n");
//!
//! let flag = this.shared.flag.fetch_add(1, ordering::Full);
//! this.shared.cond.complete_all();
//!
//! if flag == 4 {
//! HrTimerRestart::NoRestart
//! } else {
//! HrTimerRestart::Restart
//! }
//! }
//! }
//!
//! impl_has_hr_timer! {
//! impl HasHrTimer<Self> for BoxIntrusiveHrTimer {
//! mode: RelativeMode<Monotonic>, field: self.timer
//! }
//! }
//!
//! let has_timer = Box::pin_init(BoxIntrusiveHrTimer::new(), GFP_KERNEL)?;
//! let shared = has_timer.shared.clone();
//! let _handle = has_timer.start(Delta::from_micros(200));
//!
//! while shared.flag.load(ordering::Relaxed) != 5 {
//! shared.cond.wait_for_completion();
//! }
//!
//! pr_info!("Counted to 5\n");
//! # Ok::<(), kernel::error::Error>(())
//! ```
//!
//! ## Using an intrusive timer in an [`Arc`]
//!
//! ```
//! # use kernel::{
//! # alloc::flags,
//! # impl_has_hr_timer,
//! # prelude::*,
//! # sync::{
//! # atomic::{ordering, Atomic},
//! # completion::Completion,
//! # Arc, ArcBorrow,
//! # },
//! # time::{
//! # hrtimer::{
//! # RelativeMode, HrTimer, HrTimerCallback, HrTimerPointer, HrTimerRestart,
//! # HasHrTimer, HrTimerCallbackContext
//! # },
//! # Delta, Monotonic,
//! # },
//! # };
//!
//! #[pin_data]
//! struct ArcIntrusiveHrTimer {
//! #[pin]
//! timer: HrTimer<Self>,
//! #[pin]
//! flag: Atomic<u64>,
//! #[pin]
//! cond: Completion,
//! }
//!
//! impl ArcIntrusiveHrTimer {
//! fn new() -> impl PinInit<Self> {
//! pin_init!(Self {
//! timer <- HrTimer::new(),
//! flag <- Atomic::new(0),
//! cond <- Completion::new(),
//! })
//! }
//! }
//!
//! impl HrTimerCallback for ArcIntrusiveHrTimer {
//! type Pointer<'a> = Arc<Self>;
//!
//! fn run(
//! this: ArcBorrow<'_, Self>,
//! _ctx: HrTimerCallbackContext<'_, Self>,
//! ) -> HrTimerRestart {
//! pr_info!("Timer called\n");
//!
//! let flag = this.flag.fetch_add(1, ordering::Full);
//! this.cond.complete_all();
//!
//! if flag == 4 {
//! HrTimerRestart::NoRestart
//! } else {
//! HrTimerRestart::Restart
//! }
//! }
//! }
//!
//! impl_has_hr_timer! {
//! impl HasHrTimer<Self> for ArcIntrusiveHrTimer {
//! mode: RelativeMode<Monotonic>, field: self.timer
//! }
//! }
//!
//! let has_timer = Arc::pin_init(ArcIntrusiveHrTimer::new(), GFP_KERNEL)?;
//! let _handle = has_timer.clone().start(Delta::from_micros(200));
//!
//! while has_timer.flag.load(ordering::Relaxed) != 5 {
//! has_timer.cond.wait_for_completion();
//! }
//!
//! pr_info!("Counted to 5\n");
//! # Ok::<(), kernel::error::Error>(())
//! ```
//!
//! ## Using a stack-based timer
//!
//! ```
//! # use kernel::{
//! # impl_has_hr_timer,
//! # prelude::*,
//! # sync::{
//! # atomic::{ordering, Atomic},
//! # completion::Completion,
//! # },
//! # time::{
//! # hrtimer::{
//! # ScopedHrTimerPointer, HrTimer, HrTimerCallback, HrTimerPointer, HrTimerRestart,
//! # HasHrTimer, RelativeMode, HrTimerCallbackContext
//! # },
//! # Delta, Monotonic,
//! # },
//! # };
//! # use pin_init::stack_pin_init;
//!
//! #[pin_data]
//! struct IntrusiveHrTimer {
//! #[pin]
//! timer: HrTimer<Self>,
//! #[pin]
//! flag: Atomic<u64>,
//! #[pin]
//! cond: Completion,
//! }
//!
//! impl IntrusiveHrTimer {
//! fn new() -> impl PinInit<Self> {
//! pin_init!(Self {
//! timer <- HrTimer::new(),
//! flag <- Atomic::new(0),
//! cond <- Completion::new(),
//! })
//! }
//! }
//!
//! impl HrTimerCallback for IntrusiveHrTimer {
//! type Pointer<'a> = Pin<&'a Self>;
//!
//! fn run(this: Pin<&Self>, _ctx: HrTimerCallbackContext<'_, Self>) -> HrTimerRestart {
//! pr_info!("Timer called\n");
//!
//! this.flag.store(1, ordering::Release);
//! this.cond.complete_all();
//!
//! HrTimerRestart::NoRestart
//! }
//! }
//!
//! impl_has_hr_timer! {
//! impl HasHrTimer<Self> for IntrusiveHrTimer {
//! mode: RelativeMode<Monotonic>, field: self.timer
//! }
//! }
//!
//! stack_pin_init!( let has_timer = IntrusiveHrTimer::new() );
//! has_timer.as_ref().start_scoped(Delta::from_micros(200), || {
//! while has_timer.flag.load(ordering::Relaxed) != 1 {
//! has_timer.cond.wait_for_completion();
//! }
//! });
//!
//! pr_info!("Flag raised\n");
//! # Ok::<(), kernel::error::Error>(())
//! ```
//!
//! ## Using a mutable stack-based timer
//!
//! ```
//! # use kernel::{
//! # alloc::flags,
//! # impl_has_hr_timer,
//! # prelude::*,
//! # sync::{
//! # atomic::{ordering, Atomic},
//! # completion::Completion,
//! # Arc,
//! # },
//! # time::{
//! # hrtimer::{
//! # ScopedHrTimerPointer, HrTimer, HrTimerCallback, HrTimerPointer, HrTimerRestart,
//! # HasHrTimer, RelativeMode, HrTimerCallbackContext
//! # },
//! # Delta, Monotonic,
//! # },
//! # };
//! # use pin_init::stack_try_pin_init;
//!
//! #[pin_data]
//! struct Shared {
//! #[pin]
//! flag: Atomic<u64>,
//! #[pin]
//! cond: Completion,
//! }
//!
//! impl Shared {
//! fn new() -> impl PinInit<Self> {
//! pin_init!(Self {
//! flag <- Atomic::new(0),
//! cond <- Completion::new(),
//! })
//! }
//! }
//!
//! #[pin_data]
//! struct IntrusiveHrTimer {
//! #[pin]
//! timer: HrTimer<Self>,
//! shared: Arc<Shared>,
//! }
//!
//! impl IntrusiveHrTimer {
//! fn new() -> impl PinInit<Self, kernel::error::Error> {
//! try_pin_init!(Self {
//! timer <- HrTimer::new(),
//! shared: Arc::pin_init(Shared::new(), flags::GFP_KERNEL)?,
//! })
//! }
//! }
//!
//! impl HrTimerCallback for IntrusiveHrTimer {
//! type Pointer<'a> = Pin<&'a mut Self>;
//!
//! fn run(this: Pin<&mut Self>, _ctx: HrTimerCallbackContext<'_, Self>) -> HrTimerRestart {
//! pr_info!("Timer called\n");
//!
//! let flag = this.shared.flag.fetch_add(1, ordering::Full);
//! this.shared.cond.complete_all();
//!
//! if flag == 4 {
//! HrTimerRestart::NoRestart
//! } else {
//! HrTimerRestart::Restart
//! }
//! }
//! }
//!
//! impl_has_hr_timer! {
//! impl HasHrTimer<Self> for IntrusiveHrTimer {
//! mode: RelativeMode<Monotonic>, field: self.timer
//! }
//! }
//!
//! stack_try_pin_init!( let has_timer =? IntrusiveHrTimer::new() );
//! let shared = has_timer.shared.clone();
//!
//! has_timer.as_mut().start_scoped(Delta::from_micros(200), || {
//! while shared.flag.load(ordering::Relaxed) != 5 {
//! shared.cond.wait_for_completion();
//! }
//! });
//!
//! pr_info!("Counted to 5\n");
//! # Ok::<(), kernel::error::Error>(())
//! ```
//!
//! [`Arc`]: kernel::sync::Arc
use super::{ClockSource, Delta, Instant};
use crate::{prelude::*, types::Opaque};
use core::{marker::PhantomData, ptr::NonNull};
use pin_init::PinInit;
/// A type-alias to refer to the [`Instant<C>`] for a given `T` from [`HrTimer<T>`].
///
/// Where `C` is the [`ClockSource`] of the [`HrTimer`].
pub type HrTimerInstant<T> = Instant<<<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Clock>;
/// A timer backed by a C `struct hrtimer`.
///
/// # Invariants
///
/// * `self.timer` is initialized by `bindings::hrtimer_setup`.
#[pin_data]
#[repr(C)]
pub struct HrTimer<T> {
#[pin]
timer: Opaque<bindings::hrtimer>,
_t: PhantomData<T>,
}
// SAFETY: Ownership of an `HrTimer` can be moved to other threads and
// used/dropped from there.
unsafe impl<T> Send for HrTimer<T> {}
// SAFETY: Timer operations are locked on the C side, so it is safe to operate
// on a timer from multiple threads.
unsafe impl<T> Sync for HrTimer<T> {}
impl<T> HrTimer<T> {
/// Return an initializer for a new timer instance.
pub fn new() -> impl PinInit<Self>
where
T: HrTimerCallback,
T: HasHrTimer<T>,
{
pin_init!(Self {
// INVARIANT: We initialize `timer` with `hrtimer_setup` below.
timer <- Opaque::ffi_init(move |place: *mut bindings::hrtimer| {
// SAFETY: By design of `pin_init!`, `place` is a pointer to a
// live allocation. hrtimer_setup will initialize `place` and
// does not require `place` to be initialized prior to the call.
unsafe {
bindings::hrtimer_setup(
place,
Some(T::Pointer::run),
<<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Clock::ID,
<T as HasHrTimer<T>>::TimerMode::C_MODE,
);
}
}),
_t: PhantomData,
})
}
/// Get a pointer to the contained `bindings::hrtimer`.
///
/// This function is useful to get access to the value without creating
/// intermediate references.
///
/// # Safety
///
/// `this` must point to a live allocation of at least the size of `Self`.
unsafe fn raw_get(this: *const Self) -> *mut bindings::hrtimer {
// SAFETY: The field projection to `timer` does not go out of bounds,
// because the caller of this function promises that `this` points to an
// allocation of at least the size of `Self`.
unsafe { Opaque::cast_into(core::ptr::addr_of!((*this).timer)) }
}
/// Cancel an initialized and potentially running timer.
///
/// If the timer handler is running, this function will block until the
/// handler returns.
///
/// Note that the timer might be started by a concurrent start operation. If
/// so, the timer might not be in the **stopped** state when this function
/// returns.
///
/// Users of the `HrTimer` API would not usually call this method directly.
/// Instead they would use the safe [`HrTimerHandle::cancel`] on the handle
/// returned when the timer was started.
///
/// This function is useful to get access to the value without creating
/// intermediate references.
///
/// # Safety
///
/// `this` must point to a valid `Self`.
pub(crate) unsafe fn raw_cancel(this: *const Self) -> bool {
// SAFETY: `this` points to an allocation of at least `HrTimer` size.
let c_timer_ptr = unsafe { HrTimer::raw_get(this) };
// If the handler is running, this will wait for the handler to return
// before returning.
// SAFETY: `c_timer_ptr` is initialized and valid. Synchronization is
// handled on the C side.
unsafe { bindings::hrtimer_cancel(c_timer_ptr) != 0 }
}
/// Forward the timer expiry for a given timer pointer.
///
/// # Safety
///
/// - `self_ptr` must point to a valid `Self`.
/// - The caller must either have exclusive access to the data pointed at by `self_ptr`, or be
/// within the context of the timer callback.
#[inline]
unsafe fn raw_forward(self_ptr: *mut Self, now: HrTimerInstant<T>, interval: Delta) -> u64
where
T: HasHrTimer<T>,
{
// SAFETY:
// * The C API requirements for this function are fulfilled by our safety contract.
// * `self_ptr` is guaranteed to point to a valid `Self` via our safety contract
unsafe {
bindings::hrtimer_forward(Self::raw_get(self_ptr), now.as_nanos(), interval.as_nanos())
}
}
/// Conditionally forward the timer.
///
/// If the timer expires after `now`, this function does nothing and returns 0. If the timer
/// expired at or before `now`, this function forwards the timer by `interval` until the timer
/// expires after `now` and then returns the number of times the timer was forwarded by
/// `interval`.
///
/// This function is mainly useful for timer types which can provide exclusive access to the
/// timer when the timer is not running. For forwarding the timer from within the timer callback
/// context, see [`HrTimerCallbackContext::forward()`].
///
/// Returns the number of overruns that occurred as a result of the timer expiry change.
pub fn forward(self: Pin<&mut Self>, now: HrTimerInstant<T>, interval: Delta) -> u64
where
T: HasHrTimer<T>,
{
// SAFETY: `raw_forward` does not move `Self`
let this = unsafe { self.get_unchecked_mut() };
// SAFETY: By existence of `Pin<&mut Self>`, the pointer passed to `raw_forward` points to a
// valid `Self` that we have exclusive access to.
unsafe { Self::raw_forward(this, now, interval) }
}
/// Conditionally forward the timer.
///
/// This is a variant of [`forward()`](Self::forward) that uses an interval after the current
/// time of the base clock for the [`HrTimer`].
pub fn forward_now(self: Pin<&mut Self>, interval: Delta) -> u64
where
T: HasHrTimer<T>,
{
self.forward(HrTimerInstant::<T>::now(), interval)
}
/// Return the time expiry for this [`HrTimer`].
///
/// This value should only be used as a snapshot, as the actual expiry time could change after
/// this function is called.
pub fn expires(&self) -> HrTimerInstant<T>
where
T: HasHrTimer<T>,
{
// SAFETY: `self` is an immutable reference and thus always points to a valid `HrTimer`.
let c_timer_ptr = unsafe { HrTimer::raw_get(self) };
// SAFETY:
// - Timers cannot have negative ktime_t values as their expiration time.
// - There's no actual locking here, a racy read is fine and expected
unsafe {
Instant::from_ktime(
// This `read_volatile` is intended to correspond to a READ_ONCE call.
// FIXME(read_once): Replace with `read_once` when available on the Rust side.
core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
)
}
}
}
/// Implemented by pointer types that point to structs that contain a [`HrTimer`].
///
/// `Self` must be [`Sync`] because it is passed to timer callbacks in another
/// thread of execution (hard or soft interrupt context).
///
/// Starting a timer returns a [`HrTimerHandle`] that can be used to manipulate
/// the timer. Note that it is OK to call the start function repeatedly, and
/// that more than one [`HrTimerHandle`] associated with a [`HrTimerPointer`] may
/// exist. A timer can be manipulated through any of the handles, and a handle
/// may represent a cancelled timer.
pub trait HrTimerPointer: Sync + Sized {
/// The operational mode associated with this timer.
///
/// This defines how the expiration value is interpreted.
type TimerMode: HrTimerMode;
/// A handle representing a started or restarted timer.
///
/// If the timer is running or if the timer callback is executing when the
/// handle is dropped, the drop method of [`HrTimerHandle`] should not return
/// until the timer is stopped and the callback has completed.
///
/// Note: When implementing this trait, consider that it is not unsafe to
/// leak the handle.
type TimerHandle: HrTimerHandle;
/// Start the timer with expiry after `expires` time units. If the timer was
/// already running, it is restarted with the new expiry time.
fn start(self, expires: <Self::TimerMode as HrTimerMode>::Expires) -> Self::TimerHandle;
}
/// Unsafe version of [`HrTimerPointer`] for situations where leaking the
/// [`HrTimerHandle`] returned by `start` would be unsound. This is the case for
/// stack allocated timers.
///
/// Typical implementers are pinned references such as [`Pin<&T>`].
///
/// # Safety
///
/// Implementers of this trait must ensure that instances of types implementing
/// [`UnsafeHrTimerPointer`] outlives any associated [`HrTimerPointer::TimerHandle`]
/// instances.
pub unsafe trait UnsafeHrTimerPointer: Sync + Sized {
/// The operational mode associated with this timer.
///
/// This defines how the expiration value is interpreted.
type TimerMode: HrTimerMode;
/// A handle representing a running timer.
///
/// # Safety
///
/// If the timer is running, or if the timer callback is executing when the
/// handle is dropped, the drop method of [`Self::TimerHandle`] must not return
/// until the timer is stopped and the callback has completed.
type TimerHandle: HrTim
|