| 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 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | pub unsafe trait ClockSource {
|
| 70 | |
| 71 | |
| 72 | |
| 73 | const ID: bindings::clockid_t;
|
| 74 |
|
| 75 | |
| 76 | |
| 77 | |
| 78 | fn ktime_get() -> bindings::ktime_t;
|
| 79 | }
|
| 80 |
|
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | pub struct Monotonic;
|
| 93 |
|
| 94 | |
| 95 | |
| 96 | unsafe impl ClockSource for Monotonic {
|
| 97 | const ID: bindings::clockid_t = bindings::CLOCK_MONOTONIC as bindings::clockid_t;
|
| 98 |
|
| 99 | fn ktime_get() -> bindings::ktime_t {
|
| 100 | |
| 101 | unsafe { bindings::ktime_get() }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | pub struct RealTime;
|
| 120 |
|
| 121 | |
| 122 | |
| 123 | unsafe impl ClockSource for RealTime {
|
| 124 | const ID: bindings::clockid_t = bindings::CLOCK_REALTIME as bindings::clockid_t;
|
| 125 |
|
| 126 | fn ktime_get() -> bindings::ktime_t {
|
| 127 | |
| 128 | unsafe { bindings::ktime_get_real() }
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | pub struct BootTime;
|
| 140 |
|
| 141 | |
| 142 | |
| 143 | unsafe impl ClockSource for BootTime {
|
| 144 | const ID: bindings::clockid_t = bindings::CLOCK_BOOTTIME as bindings::clockid_t;
|
| 145 |
|
| 146 | fn ktime_get() -> bindings::ktime_t {
|
| 147 | |
| 148 | unsafe { bindings::ktime_get_boottime() }
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | pub struct Tai;
|
| 164 |
|
| 165 | |
| 166 | |
| 167 | unsafe impl ClockSource for Tai {
|
| 168 | const ID: bindings::clockid_t = bindings::CLOCK_TAI as bindings::clockid_t;
|
| 169 |
|
| 170 | fn ktime_get() -> bindings::ktime_t {
|
| 171 | |
| 172 | unsafe { bindings::ktime_get_clocktai() }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | #[repr(transparent)]
|
| 182 | #[derive(PartialEq, PartialOrd, Eq, Ord)]
|
| 183 | pub struct Instant<C: ClockSource> {
|
| 184 | inner: bindings::ktime_t,
|
| 185 | _c: PhantomData<C>,
|
| 186 | }
|
| 187 |
|
| 188 | impl<C: ClockSource> Clone for Instant<C> {
|
| 189 | fn clone(&self) -> Self {
|
| 190 | *self
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | impl<C: ClockSource> Copy for Instant<C> {}
|
| 195 |
|
| 196 | impl<C: ClockSource> Instant<C> {
|
| 197 | |
| 198 | #[inline]
|
| 199 | pub fn now() -> Self {
|
| 200 | |
| 201 | |
| 202 | Self {
|
| 203 | inner: C::ktime_get(),
|
| 204 | _c: PhantomData,
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | |
| 209 | #[inline]
|
| 210 | pub fn elapsed(&self) -> Delta {
|
| 211 | Self::now() - *self
|
| 212 | }
|
| 213 |
|
| 214 | #[inline]
|
| 215 | pub(crate) fn as_nanos(&self) -> i64 {
|
| 216 | self.inner
|
| 217 | }
|
| 218 |
|
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | #[inline]
|
| 230 | pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
|
| 231 | debug_assert!(ktime >= 0);
|
| 232 |
|
| 233 | |
| 234 | |
| 235 | Self {
|
| 236 | inner: ktime,
|
| 237 | _c: PhantomData,
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | impl<C: ClockSource> ops::Sub for Instant<C> {
|
| 243 | type Output = Delta;
|
| 244 |
|
| 245 | |
| 246 | #[inline]
|
| 247 | fn sub(self, other: Instant<C>) -> Delta {
|
| 248 | Delta {
|
| 249 | value: self.inner - other.inner,
|
| 250 | }
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | impl<T: ClockSource> ops::Add<Delta> for Instant<T> {
|
| 255 | type Output = Self;
|
| 256 |
|
| 257 | #[inline]
|
| 258 | fn add(self, rhs: Delta) -> Self::Output {
|
| 259 | |
| 260 | |
| 261 | let res = self.inner + rhs.value;
|
| 262 |
|
| 263 | |
| 264 | #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
|
| 265 | assert!(res >= 0);
|
| 266 |
|
| 267 | Self {
|
| 268 | inner: res,
|
| 269 | _c: PhantomData,
|
| 270 | }
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | impl<T: ClockSource> ops::Sub<Delta> for Instant<T> {
|
| 275 | type Output = Self;
|
| 276 |
|
| 277 | #[inline]
|
| 278 | fn sub(self, rhs: Delta) -> Self::Output {
|
| 279 | |
| 280 | |
| 281 | let res = self.inner - rhs.value;
|
| 282 |
|
| 283 | |
| 284 | #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
|
| 285 | assert!(res >= 0);
|
| 286 |
|
| 287 | Self {
|
| 288 | inner: res,
|
| 289 | _c: PhantomData,
|
| 290 | }
|
| 291 | }
|
| 292 | }
|
| 293 |
|
| 294 | mod private {
|
| 295 | pub trait Sealed {}
|
| 296 |
|
| 297 | impl Sealed for super::Nsec {}
|
| 298 | impl Sealed for super::Jiffy {}
|
| 299 | }
|
| 300 |
|
| 301 | |
| 302 | pub trait TimeUnit: private::Sealed {
|
| 303 | |
| 304 | type Repr: Copy + Clone + PartialEq + PartialOrd + Eq + Ord + core::fmt::Debug;
|
| 305 | }
|
| 306 |
|
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
|
| 312 | pub enum Nsec {}
|
| 313 |
|
| 314 | impl TimeUnit for Nsec {
|
| 315 | type Repr = i64;
|
| 316 | }
|
| 317 |
|
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
|
| 323 | pub enum Jiffy {}
|
| 324 |
|
| 325 | impl TimeUnit for Jiffy {
|
| 326 | type Repr = isize;
|
| 327 | }
|
| 328 |
|
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
|
| 336 | pub struct Delta<U: TimeUnit = Nsec> {
|
| 337 | value: U::Repr,
|
| 338 | }
|
| 339 |
|
| 340 | impl Delta<Jiffy> {
|
| 341 | |
| 342 | #[inline]
|
| 343 | pub const fn from_jiffies(jiffies: isize) -> Self {
|
| 344 | Self { value: jiffies }
|
| 345 | }
|
| 346 |
|
| 347 | |
| 348 | #[inline]
|
| 349 | pub const fn as_jiffies(self) -> isize {
|
| 350 | self.value
|
| 351 | }
|
| 352 | }
|
| 353 |
|
| 354 | impl ops::Add for Delta {
|
| 355 | type Output = Self;
|
| 356 |
|
| 357 | #[inline]
|
| 358 | fn add(self, rhs: Self) -> Self {
|
| 359 | Self {
|
| 360 | value: self.value + rhs.value,
|
| 361 | }
|
| 362 | }
|
| 363 | }
|
| 364 |
|
| 365 | impl ops::AddAssign for Delta {
|
| 366 | #[inline]
|
| 367 | fn add_assign(&mut self, rhs: Self) {
|
| 368 | self.value += rhs.value;
|
| 369 | }
|
| 370 | }
|
| 371 |
|
| 372 | impl ops::Sub for Delta {
|
| 373 | type Output = Self;
|
| 374 |
|
| 375 | #[inline]
|
| 376 | fn sub(self, rhs: Self) -> Self::Output {
|
| 377 | Self {
|
| 378 | value: self.value - rhs.value,
|
| 379 | }
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | impl ops::SubAssign for Delta {
|
| 384 | #[inline]
|
| 385 | fn sub_assign(&mut self, rhs: Self) {
|
| 386 | self.value -= rhs.value;
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | impl ops::Mul<i64> for Delta {
|
| 391 | type Output = Self;
|
| 392 |
|
| 393 | #[inline]
|
| 394 | fn mul(self, rhs: i64) -> Self::Output {
|
| 395 | Self {
|
| 396 | value: self.value * rhs,
|
| 397 | }
|
| 398 | }
|
| 399 | }
|
| 400 |
|
| 401 | impl ops::MulAssign<i64> for Delta {
|
| 402 | #[inline]
|
| 403 | fn mul_assign(&mut self, rhs: i64) {
|
| 404 | self.value *= rhs;
|
| 405 | }
|
| 406 | }
|
| 407 |
|
| 408 | impl ops::Div for Delta {
|
| 409 | type Output = i64;
|
| 410 |
|
| 411 | #[inline]
|
| 412 | fn div(self, rhs: Self) -> Self::Output {
|
| 413 | #[cfg(CONFIG_64BIT)]
|
| 414 | {
|
| 415 | self.value / rhs.value
|
| 416 | }
|
| 417 |
|
| 418 | #[cfg(not(CONFIG_64BIT))]
|
| 419 | {
|
| 420 | |
| 421 | unsafe { bindings::div64_s64(self.value, rhs.value) }
|
| 422 | }
|
| 423 | }
|
| 424 | }
|
| 425 |
|
| 426 | impl Delta {
|
| 427 | |
| 428 | pub const ZERO: Self = Self { value: 0 };
|
| 429 |
|
| 430 | |
| 431 | #[inline]
|
| 432 | pub const fn from_nanos(nanos: i64) -> Self {
|
| 433 | Self { value: nanos }
|
| 434 | }
|
| 435 |
|
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | #[inline]
|
| 442 | pub const fn from_micros(micros: i64) -> Self {
|
| 443 | Self {
|
| 444 | value: micros.saturating_mul(NSEC_PER_USEC),
|
| 445 | }
|
| 446 | }
|
| 447 |
|
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | #[inline]
|
| 454 | pub const fn from_millis(millis: i64) -> Self {
|
| 455 | Self {
|
| 456 | value: millis.saturating_mul(NSEC_PER_MSEC),
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | #[inline]
|
| 466 | pub const fn from_secs(secs: i64) -> Self {
|
| 467 | Self {
|
| 468 | value: secs.saturating_mul(NSEC_PER_SEC),
|
| 469 | }
|
| 470 | }
|
| 471 |
|
| 472 | |
| 473 | #[inline]
|
| 474 | pub fn is_zero(self) -> bool {
|
| 475 | self.as_nanos() == 0
|
| 476 | }
|
| 477 |
|
| 478 | |
| 479 | #[inline]
|
| 480 | pub fn is_negative(self) -> bool {
|
| 481 | self.as_nanos() < 0
|
| 482 | }
|
| 483 |
|
| 484 | |
| 485 | #[inline]
|
| 486 | pub const fn as_nanos(self) -> i64 {
|
| 487 | self.value
|
| 488 | }
|
| 489 |
|
| 490 | |
| 491 | |
| 492 | #[inline]
|
| 493 | pub fn as_micros_ceil(self) -> i64 {
|
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | let n = self.as_nanos();
|
| 501 |
|
| 502 | let (n, add) = if n > 0 { (n - 1, 1) } else { (n, 0) };
|
| 503 |
|
| 504 | #[cfg(CONFIG_64BIT)]
|
| 505 | {
|
| 506 | n / NSEC_PER_USEC + add
|
| 507 | }
|
| 508 |
|
| 509 | #[cfg(not(CONFIG_64BIT))]
|
| 510 | |
| 511 | unsafe {
|
| 512 | bindings::ktime_to_us(n) + add
|
| 513 | }
|
| 514 | }
|
| 515 |
|
| 516 | |
| 517 | #[inline]
|
| 518 | pub fn as_millis(self) -> i64 {
|
| 519 | #[cfg(CONFIG_64BIT)]
|
| 520 | {
|
| 521 | self.as_nanos() / NSEC_PER_MSEC
|
| 522 | }
|
| 523 |
|
| 524 | #[cfg(not(CONFIG_64BIT))]
|
| 525 | |
| 526 | unsafe {
|
| 527 | bindings::ktime_to_ms(self.as_nanos())
|
| 528 | }
|
| 529 | }
|
| 530 |
|
| 531 | |
| 532 | |
| 533 | #[inline]
|
| 534 | pub fn as_millis_ceil(self) -> i64 {
|
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | let n = self.as_nanos();
|
| 542 |
|
| 543 | let (n, add) = if n > 0 { (n - 1, 1) } else { (n, 0) };
|
| 544 |
|
| 545 | #[cfg(CONFIG_64BIT)]
|
| 546 | {
|
| 547 | n / NSEC_PER_MSEC + add
|
| 548 | }
|
| 549 |
|
| 550 | #[cfg(not(CONFIG_64BIT))]
|
| 551 | |
| 552 | unsafe {
|
| 553 | bindings::ktime_to_ms(n) + add
|
| 554 | }
|
| 555 | }
|
| 556 |
|
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | #[inline]
|
| 562 | pub fn rem_nanos(self, dividend: i32) -> Self {
|
| 563 | #[cfg(CONFIG_64BIT)]
|
| 564 | {
|
| 565 | Self {
|
| 566 | value: self.as_nanos() % i64::from(dividend),
|
| 567 | }
|
| 568 | }
|
| 569 |
|
| 570 | #[cfg(not(CONFIG_64BIT))]
|
| 571 | {
|
| 572 | let mut rem = 0;
|
| 573 |
|
| 574 | |
| 575 | unsafe { bindings::div_s64_rem(self.as_nanos(), dividend, &mut rem) };
|
| 576 |
|
| 577 | Self {
|
| 578 | value: i64::from(rem),
|
| 579 | }
|
| 580 | }
|
| 581 | }
|
| 582 | }
|