| 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 | use core::marker::PhantomData;
|
| 28 | use core::ops;
|
| 29 |
|
| 30 | pub mod delay;
|
| 31 | pub mod hrtimer;
|
| 32 |
|
| 33 | |
| 34 | pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64;
|
| 35 |
|
| 36 | |
| 37 | pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
|
| 38 |
|
| 39 | |
| 40 | pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64;
|
| 41 |
|
| 42 | |
| 43 | pub type Jiffies = crate::ffi::c_ulong;
|
| 44 |
|
| 45 | |
| 46 | pub type Msecs = crate::ffi::c_uint;
|
| 47 |
|
| 48 | |
| 49 | #[inline]
|
| 50 | pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
|
| 51 | |
| 52 | |
| 53 | unsafe { bindings::__msecs_to_jiffies(msecs) }
|
| 54 | }
|
| 55 |
|
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | pub trait ClockSource {
|
| 64 | |
| 65 | |
| 66 | |
| 67 | const ID: bindings::clockid_t;
|
| 68 |
|
| 69 | |
| 70 | |
| 71 | |
| 72 | fn ktime_get() -> bindings::ktime_t;
|
| 73 | }
|
| 74 |
|
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | pub struct Monotonic;
|
| 87 |
|
| 88 | impl ClockSource for Monotonic {
|
| 89 | const ID: bindings::clockid_t = bindings::CLOCK_MONOTONIC as bindings::clockid_t;
|
| 90 |
|
| 91 | fn ktime_get() -> bindings::ktime_t {
|
| 92 | |
| 93 | unsafe { bindings::ktime_get() }
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | pub struct RealTime;
|
| 112 |
|
| 113 | impl ClockSource for RealTime {
|
| 114 | const ID: bindings::clockid_t = bindings::CLOCK_REALTIME as bindings::clockid_t;
|
| 115 |
|
| 116 | fn ktime_get() -> bindings::ktime_t {
|
| 117 | |
| 118 | unsafe { bindings::ktime_get_real() }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | pub struct BootTime;
|
| 130 |
|
| 131 | impl ClockSource for BootTime {
|
| 132 | const ID: bindings::clockid_t = bindings::CLOCK_BOOTTIME as bindings::clockid_t;
|
| 133 |
|
| 134 | fn ktime_get() -> bindings::ktime_t {
|
| 135 | |
| 136 | unsafe { bindings::ktime_get_boottime() }
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | pub struct Tai;
|
| 152 |
|
| 153 | impl ClockSource for Tai {
|
| 154 | const ID: bindings::clockid_t = bindings::CLOCK_TAI as bindings::clockid_t;
|
| 155 |
|
| 156 | fn ktime_get() -> bindings::ktime_t {
|
| 157 | |
| 158 | unsafe { bindings::ktime_get_clocktai() }
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | #[repr(transparent)]
|
| 168 | #[derive(PartialEq, PartialOrd, Eq, Ord)]
|
| 169 | pub struct Instant<C: ClockSource> {
|
| 170 | inner: bindings::ktime_t,
|
| 171 | _c: PhantomData<C>,
|
| 172 | }
|
| 173 |
|
| 174 | impl<C: ClockSource> Clone for Instant<C> {
|
| 175 | fn clone(&self) -> Self {
|
| 176 | *self
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | impl<C: ClockSource> Copy for Instant<C> {}
|
| 181 |
|
| 182 | impl<C: ClockSource> Instant<C> {
|
| 183 | |
| 184 | #[inline]
|
| 185 | pub fn now() -> Self {
|
| 186 | |
| 187 | |
| 188 | Self {
|
| 189 | inner: C::ktime_get(),
|
| 190 | _c: PhantomData,
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | |
| 195 | #[inline]
|
| 196 | pub fn elapsed(&self) -> Delta {
|
| 197 | Self::now() - *self
|
| 198 | }
|
| 199 |
|
| 200 | #[inline]
|
| 201 | pub(crate) fn as_nanos(&self) -> i64 {
|
| 202 | self.inner
|
| 203 | }
|
| 204 |
|
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | #[inline]
|
| 216 | pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
|
| 217 | debug_assert!(ktime >= 0);
|
| 218 |
|
| 219 | |
| 220 | |
| 221 | Self {
|
| 222 | inner: ktime,
|
| 223 | _c: PhantomData,
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | impl<C: ClockSource> ops::Sub for Instant<C> {
|
| 229 | type Output = Delta;
|
| 230 |
|
| 231 | |
| 232 | #[inline]
|
| 233 | fn sub(self, other: Instant<C>) -> Delta {
|
| 234 | Delta {
|
| 235 | nanos: self.inner - other.inner,
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | impl<T: ClockSource> ops::Add<Delta> for Instant<T> {
|
| 241 | type Output = Self;
|
| 242 |
|
| 243 | #[inline]
|
| 244 | fn add(self, rhs: Delta) -> Self::Output {
|
| 245 | |
| 246 | |
| 247 | let res = self.inner + rhs.nanos;
|
| 248 |
|
| 249 | |
| 250 | #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
|
| 251 | assert!(res >= 0);
|
| 252 |
|
| 253 | Self {
|
| 254 | inner: res,
|
| 255 | _c: PhantomData,
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | impl<T: ClockSource> ops::Sub<Delta> for Instant<T> {
|
| 261 | type Output = Self;
|
| 262 |
|
| 263 | #[inline]
|
| 264 | fn sub(self, rhs: Delta) -> Self::Output {
|
| 265 | |
| 266 | |
| 267 | let res = self.inner - rhs.nanos;
|
| 268 |
|
| 269 | |
| 270 | #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
|
| 271 | assert!(res >= 0);
|
| 272 |
|
| 273 | Self {
|
| 274 | inner: res,
|
| 275 | _c: PhantomData,
|
| 276 | }
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
|
| 286 | pub struct Delta {
|
| 287 | nanos: i64,
|
| 288 | }
|
| 289 |
|
| 290 | impl ops::Add for Delta {
|
| 291 | type Output = Self;
|
| 292 |
|
| 293 | #[inline]
|
| 294 | fn add(self, rhs: Self) -> Self {
|
| 295 | Self {
|
| 296 | nanos: self.nanos + rhs.nanos,
|
| 297 | }
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | impl ops::AddAssign for Delta {
|
| 302 | #[inline]
|
| 303 | fn add_assign(&mut self, rhs: Self) {
|
| 304 | self.nanos += rhs.nanos;
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | impl ops::Sub for Delta {
|
| 309 | type Output = Self;
|
| 310 |
|
| 311 | #[inline]
|
| 312 | fn sub(self, rhs: Self) -> Self::Output {
|
| 313 | Self {
|
| 314 | nanos: self.nanos - rhs.nanos,
|
| 315 | }
|
| 316 | }
|
| 317 | }
|
| 318 |
|
| 319 | impl ops::SubAssign for Delta {
|
| 320 | #[inline]
|
| 321 | fn sub_assign(&mut self, rhs: Self) {
|
| 322 | self.nanos -= rhs.nanos;
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | impl ops::Mul<i64> for Delta {
|
| 327 | type Output = Self;
|
| 328 |
|
| 329 | #[inline]
|
| 330 | fn mul(self, rhs: i64) -> Self::Output {
|
| 331 | Self {
|
| 332 | nanos: self.nanos * rhs,
|
| 333 | }
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | impl ops::MulAssign<i64> for Delta {
|
| 338 | #[inline]
|
| 339 | fn mul_assign(&mut self, rhs: i64) {
|
| 340 | self.nanos *= rhs;
|
| 341 | }
|
| 342 | }
|
| 343 |
|
| 344 | impl ops::Div for Delta {
|
| 345 | type Output = i64;
|
| 346 |
|
| 347 | #[inline]
|
| 348 | fn div(self, rhs: Self) -> Self::Output {
|
| 349 | #[cfg(CONFIG_64BIT)]
|
| 350 | {
|
| 351 | self.nanos / rhs.nanos
|
| 352 | }
|
| 353 |
|
| 354 | #[cfg(not(CONFIG_64BIT))]
|
| 355 | {
|
| 356 | |
| 357 | unsafe { bindings::div64_s64(self.nanos, rhs.nanos) }
|
| 358 | }
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | impl Delta {
|
| 363 | |
| 364 | pub const ZERO: Self = Self { nanos: 0 };
|
| 365 |
|
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | #[inline]
|
| 372 | pub const fn from_micros(micros: i64) -> Self {
|
| 373 | Self {
|
| 374 | nanos: micros.saturating_mul(NSEC_PER_USEC),
|
| 375 | }
|
| 376 | }
|
| 377 |
|
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | #[inline]
|
| 384 | pub const fn from_millis(millis: i64) -> Self {
|
| 385 | Self {
|
| 386 | nanos: millis.saturating_mul(NSEC_PER_MSEC),
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | #[inline]
|
| 396 | pub const fn from_secs(secs: i64) -> Self {
|
| 397 | Self {
|
| 398 | nanos: secs.saturating_mul(NSEC_PER_SEC),
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | |
| 403 | #[inline]
|
| 404 | pub fn is_zero(self) -> bool {
|
| 405 | self.as_nanos() == 0
|
| 406 | }
|
| 407 |
|
| 408 | |
| 409 | #[inline]
|
| 410 | pub fn is_negative(self) -> bool {
|
| 411 | self.as_nanos() < 0
|
| 412 | }
|
| 413 |
|
| 414 | |
| 415 | #[inline]
|
| 416 | pub const fn as_nanos(self) -> i64 {
|
| 417 | self.nanos
|
| 418 | }
|
| 419 |
|
| 420 | |
| 421 | |
| 422 | #[inline]
|
| 423 | pub fn as_micros_ceil(self) -> i64 {
|
| 424 | #[cfg(CONFIG_64BIT)]
|
| 425 | {
|
| 426 | self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
|
| 427 | }
|
| 428 |
|
| 429 | #[cfg(not(CONFIG_64BIT))]
|
| 430 | |
| 431 | unsafe {
|
| 432 | bindings::ktime_to_us(self.as_nanos().saturating_add(NSEC_PER_USEC - 1))
|
| 433 | }
|
| 434 | }
|
| 435 |
|
| 436 | |
| 437 | #[inline]
|
| 438 | pub fn as_millis(self) -> i64 {
|
| 439 | #[cfg(CONFIG_64BIT)]
|
| 440 | {
|
| 441 | self.as_nanos() / NSEC_PER_MSEC
|
| 442 | }
|
| 443 |
|
| 444 | #[cfg(not(CONFIG_64BIT))]
|
| 445 | |
| 446 | unsafe {
|
| 447 | bindings::ktime_to_ms(self.as_nanos())
|
| 448 | }
|
| 449 | }
|
| 450 |
|
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | #[inline]
|
| 456 | pub fn rem_nanos(self, dividend: i32) -> Self {
|
| 457 | #[cfg(CONFIG_64BIT)]
|
| 458 | {
|
| 459 | Self {
|
| 460 | nanos: self.as_nanos() % i64::from(dividend),
|
| 461 | }
|
| 462 | }
|
| 463 |
|
| 464 | #[cfg(not(CONFIG_64BIT))]
|
| 465 | {
|
| 466 | let mut rem = 0;
|
| 467 |
|
| 468 | |
| 469 | unsafe { bindings::div_s64_rem(self.as_nanos(), dividend, &mut rem) };
|
| 470 |
|
| 471 | Self {
|
| 472 | nanos: i64::from(rem),
|
| 473 | }
|
| 474 | }
|
| 475 | }
|
| 476 | }
|