kernel/time/hrtimer/
tbox.rs1use super::HasHrTimer;
4use super::HrTimer;
5use super::HrTimerCallback;
6use super::HrTimerHandle;
7use super::HrTimerMode;
8use super::HrTimerPointer;
9use super::RawHrTimerCallback;
10use crate::prelude::*;
11use core::ptr::NonNull;
12
13pub struct BoxHrTimerHandle<T, A>
20where
21 T: HasHrTimer<T>,
22 A: crate::alloc::Allocator,
23{
24 pub(crate) inner: NonNull<T>,
25 _p: core::marker::PhantomData<A>,
26}
27
28unsafe impl<T, A> HrTimerHandle for BoxHrTimerHandle<T, A>
31where
32 T: HasHrTimer<T>,
33 A: crate::alloc::Allocator,
34{
35 fn cancel(&mut self) -> bool {
36 let timer_ptr = unsafe { <T as HasHrTimer<T>>::raw_get_timer(self.inner.as_ptr()) };
39
40 unsafe { HrTimer::<T>::raw_cancel(timer_ptr) }
43 }
44}
45
46impl<T, A> Drop for BoxHrTimerHandle<T, A>
47where
48 T: HasHrTimer<T>,
49 A: crate::alloc::Allocator,
50{
51 fn drop(&mut self) {
52 self.cancel();
53 drop(unsafe { Box::<T, A>::from_raw(self.inner.as_ptr()) })
56 }
57}
58
59impl<T, A> HrTimerPointer for Pin<Box<T, A>>
60where
61 T: 'static,
62 T: Send + Sync,
63 T: HasHrTimer<T>,
64 T: for<'a> HrTimerCallback<Pointer<'a> = Pin<Box<T, A>>>,
65 A: crate::alloc::Allocator,
66{
67 type TimerMode = <T as HasHrTimer<T>>::TimerMode;
68 type TimerHandle = BoxHrTimerHandle<T, A>;
69
70 fn start(
71 self,
72 expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires,
73 ) -> Self::TimerHandle {
74 let inner =
79 unsafe { NonNull::new_unchecked(Box::into_raw(Pin::into_inner_unchecked(self))) };
80
81 unsafe { T::start(inner.as_ptr(), expires) };
86
87 BoxHrTimerHandle {
89 inner,
90 _p: core::marker::PhantomData,
91 }
92 }
93}
94
95impl<T, A> RawHrTimerCallback for Pin<Box<T, A>>
96where
97 T: 'static,
98 T: HasHrTimer<T>,
99 T: for<'a> HrTimerCallback<Pointer<'a> = Pin<Box<T, A>>>,
100 A: crate::alloc::Allocator,
101{
102 type CallbackTarget<'a> = Pin<&'a mut T>;
103
104 unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
105 let timer_ptr = ptr.cast::<super::HrTimer<T>>();
107
108 let data_ptr = unsafe { T::timer_container_of(timer_ptr) };
111
112 let data_mut_ref = unsafe { Pin::new_unchecked(&mut *data_ptr) };
121
122 T::run(data_mut_ref).into_c()
123 }
124}