~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/time.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 //! Time related primitives.
  4 //!
  5 //! This module contains the kernel APIs related to time and timers that
  6 //! have been ported or wrapped for usage by Rust code in the kernel.
  7 //!
  8 //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
  9 //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
 10 
 11 /// The number of nanoseconds per millisecond.
 12 pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
 13 
 14 /// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
 15 pub type Jiffies = core::ffi::c_ulong;
 16 
 17 /// The millisecond time unit.
 18 pub type Msecs = core::ffi::c_uint;
 19 
 20 /// Converts milliseconds to jiffies.
 21 #[inline]
 22 pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
 23     // SAFETY: The `__msecs_to_jiffies` function is always safe to call no
 24     // matter what the argument is.
 25     unsafe { bindings::__msecs_to_jiffies(msecs) }
 26 }
 27 
 28 /// A Rust wrapper around a `ktime_t`.
 29 #[repr(transparent)]
 30 #[derive(Copy, Clone)]
 31 pub struct Ktime {
 32     inner: bindings::ktime_t,
 33 }
 34 
 35 impl Ktime {
 36     /// Create a `Ktime` from a raw `ktime_t`.
 37     #[inline]
 38     pub fn from_raw(inner: bindings::ktime_t) -> Self {
 39         Self { inner }
 40     }
 41 
 42     /// Get the current time using `CLOCK_MONOTONIC`.
 43     #[inline]
 44     pub fn ktime_get() -> Self {
 45         // SAFETY: It is always safe to call `ktime_get` outside of NMI context.
 46         Self::from_raw(unsafe { bindings::ktime_get() })
 47     }
 48 
 49     /// Divide the number of nanoseconds by a compile-time constant.
 50     #[inline]
 51     fn divns_constant<const DIV: i64>(self) -> i64 {
 52         self.to_ns() / DIV
 53     }
 54 
 55     /// Returns the number of nanoseconds.
 56     #[inline]
 57     pub fn to_ns(self) -> i64 {
 58         self.inner
 59     }
 60 
 61     /// Returns the number of milliseconds.
 62     #[inline]
 63     pub fn to_ms(self) -> i64 {
 64         self.divns_constant::<NSEC_PER_MSEC>()
 65     }
 66 }
 67 
 68 /// Returns the number of milliseconds between two ktimes.
 69 #[inline]
 70 pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
 71     (later - earlier).to_ms()
 72 }
 73 
 74 impl core::ops::Sub for Ktime {
 75     type Output = Ktime;
 76 
 77     #[inline]
 78     fn sub(self, other: Ktime) -> Ktime {
 79         Self {
 80             inner: self.inner - other.inner,
 81         }
 82     }
 83 }

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php