1 // SPDX-License-Identifier: GPL-2.0 2 3 use super::HasHrTimer; 4 use super::HrTimer; 5 use super::HrTimerCallback; 6 use super::HrTimerHandle; 7 use super::HrTimerMode; 8 use super::HrTimerPointer; 9 use super::RawHrTimerCallback; 10 use crate::sync::Arc; 11 use crate::sync::ArcBorrow; 12 13 /// A handle for an `Arc<HasHrTimer<T>>` returned by a call to 14 /// [`HrTimerPointer::start`]. 15 pub struct ArcHrTimerHandle<T> 16 where 17 T: HasHrTimer<T>, 18 { 19 pub(crate) inner: Arc<T>, 20 } 21 22 // SAFETY: We implement drop below, and we cancel the timer in the drop 23 // implementation. 24 unsafe impl<T> HrTimerHandle for ArcHrTimerHandle<T> 25 where 26 T: HasHrTimer<T>, 27 { cancel(&mut self) -> bool28 fn cancel(&mut self) -> bool { 29 let self_ptr = Arc::as_ptr(&self.inner); 30 31 // SAFETY: As we obtained `self_ptr` from a valid reference above, it 32 // must point to a valid `T`. 33 let timer_ptr = unsafe { <T as HasHrTimer<T>>::raw_get_timer(self_ptr) }; 34 35 // SAFETY: As `timer_ptr` points into `T` and `T` is valid, `timer_ptr` 36 // must point to a valid `HrTimer` instance. 37 unsafe { HrTimer::<T>::raw_cancel(timer_ptr) } 38 } 39 } 40 41 impl<T> Drop for ArcHrTimerHandle<T> 42 where 43 T: HasHrTimer<T>, 44 { drop(&mut self)45 fn drop(&mut self) { 46 self.cancel(); 47 } 48 } 49 50 impl<T> HrTimerPointer for Arc<T> 51 where 52 T: 'static, 53 T: Send + Sync, 54 T: HasHrTimer<T>, 55 T: for<'a> HrTimerCallback<Pointer<'a> = Self>, 56 { 57 type TimerMode = <T as HasHrTimer<T>>::TimerMode; 58 type TimerHandle = ArcHrTimerHandle<T>; 59 start( self, expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires, ) -> ArcHrTimerHandle<T>60 fn start( 61 self, 62 expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires, 63 ) -> ArcHrTimerHandle<T> { 64 // SAFETY: 65 // - We keep `self` alive by wrapping it in a handle below. 66 // - Since we generate the pointer passed to `start` from a valid 67 // reference, it is a valid pointer. 68 unsafe { T::start(Arc::as_ptr(&self), expires) }; 69 ArcHrTimerHandle { inner: self } 70 } 71 } 72 73 impl<T> RawHrTimerCallback for Arc<T> 74 where 75 T: 'static, 76 T: HasHrTimer<T>, 77 T: for<'a> HrTimerCallback<Pointer<'a> = Self>, 78 { 79 type CallbackTarget<'a> = ArcBorrow<'a, T>; 80 run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart81 unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart { 82 // `HrTimer` is `repr(C)` 83 let timer_ptr = ptr.cast::<super::HrTimer<T>>(); 84 85 // SAFETY: By C API contract `ptr` is the pointer we passed when 86 // queuing the timer, so it is a `HrTimer<T>` embedded in a `T`. 87 let data_ptr = unsafe { T::timer_container_of(timer_ptr) }; 88 89 // SAFETY: 90 // - `data_ptr` is derived form the pointer to the `T` that was used to 91 // queue the timer. 92 // - As per the safety requirements of the trait `HrTimerHandle`, the 93 // `ArcHrTimerHandle` associated with this timer is guaranteed to 94 // be alive until this method returns. That handle borrows the `T` 95 // behind `data_ptr` thus guaranteeing the validity of 96 // the `ArcBorrow` created below. 97 // - We own one refcount in the `ArcTimerHandle` associated with this 98 // timer, so it is not possible to get a `UniqueArc` to this 99 // allocation from other `Arc` clones. 100 let receiver = unsafe { ArcBorrow::from_raw(data_ptr) }; 101 102 T::run(receiver).into_c() 103 } 104 } 105