File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ // SPDX-License-Identifier: GPL-2.0
2
+
3
+ //! Delay functions for operations like sleeping.
4
+ //!
5
+ //! C header: [`include/linux/delay.h`](../../../../include/linux/delay.h)
6
+
7
+ use crate :: bindings;
8
+ use core:: { cmp:: min, time:: Duration } ;
9
+
10
+ const MILLIS_PER_SEC : u64 = 1_000 ;
11
+
12
+ /// Sleeps safely even with waitqueue interruptions.
13
+ ///
14
+ /// This function forwards the call to the C side `msleep` function. As a result,
15
+ /// `duration` will be rounded up to the nearest millisecond if granularity less
16
+ /// than a millisecond is provided. Any [`Duration`] that exceeds
17
+ /// [`c_uint::MAX`][core::ffi::c_uint::MAX] in milliseconds is saturated.
18
+ pub fn coarse_sleep ( duration : Duration ) {
19
+ let milli_as_nanos = Duration :: MILLISECOND . subsec_nanos ( ) ;
20
+
21
+ // Rounds the nanosecond component of `duration` up to the nearest millisecond.
22
+ let nanos_as_millis = duration. subsec_nanos ( ) . wrapping_add ( milli_as_nanos - 1 ) / milli_as_nanos;
23
+
24
+ // Saturates the second component of `duration` to `c_uint::MAX`.
25
+ let seconds_as_millis = min (
26
+ duration. as_secs ( ) . saturating_mul ( MILLIS_PER_SEC ) ,
27
+ u64:: from ( core:: ffi:: c_uint:: MAX ) ,
28
+ ) as core:: ffi:: c_uint ;
29
+
30
+ // SAFETY: msleep is safe for all values of an `unsigned int`.
31
+ unsafe { bindings:: msleep ( seconds_as_millis. saturating_add ( nanos_as_millis) ) }
32
+ }
Original file line number Diff line number Diff line change 27
27
#![ feature( coerce_unsized) ]
28
28
#![ feature( dispatch_from_dyn) ]
29
29
#![ feature( unsize) ]
30
+ #![ feature( duration_constants) ]
30
31
31
32
// Ensure conditional compilation based on the kernel configuration works;
32
33
// otherwise we may silently break things like initcall handling.
@@ -48,6 +49,7 @@ pub mod chrdev;
48
49
#[ cfg( CONFIG_COMMON_CLK ) ]
49
50
pub mod clk;
50
51
pub mod cred;
52
+ pub mod delay;
51
53
pub mod device;
52
54
pub mod driver;
53
55
pub mod error;
You can’t perform that action at this time.
0 commit comments