clock.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Timekeeping module.
  19. */
  20. #ifndef KERN_CLOCK_H
  21. #define KERN_CLOCK_H
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <kern/atomic.h>
  25. #include <kern/clock_i.h>
  26. #include <kern/init.h>
  27. #include <kern/macros.h>
  28. /*
  29. * Clock frequency.
  30. */
  31. #define CLOCK_FREQ CONFIG_CLOCK_FREQ
  32. #if (CLOCK_FREQ < 100) || (CLOCK_FREQ > 1000) || (1000 % CLOCK_FREQ) != 0
  33. #error "invalid clock frequency"
  34. #endif /* (1000 % CLOCK_FREQ) != 0 */
  35. /*
  36. * Arbitrary value used to determine if a time is in the past or the future.
  37. *
  38. * Time is represented as 64-bits unsigned integers counting ticks. The
  39. * global time currently starts from 0 but this isn't a strong assumption
  40. * users should rely on. Instead, all time checks involve a time reference
  41. * against which to compare. The result of that comparison, done by
  42. * substraction, is either in the future, i.e. the difference is less
  43. * than the expire threshold, or in the past, i.e. the difference is
  44. * greater (keep in mind the result is unsigned). The threshold must be
  45. * large enough to allow both a wide range of possible times in the future,
  46. * but also enough time in the past for reliable timeout detection. Note
  47. * that using signed integers would be equivalent to dividing the range
  48. * in two (almost) equal past and future halves.
  49. */
  50. #define CLOCK_EXPIRE_THRESHOLD (-(1ULL << 60))
  51. static inline uint64_t
  52. clock_get_time(void)
  53. {
  54. extern union clock_global_time clock_global_time;
  55. #ifdef ATOMIC_HAVE_64B_OPS
  56. /*
  57. * Don't enforce a stronger memory order, since :
  58. * 1/ it's useless as long as the reader remains on the same processor
  59. * 2/ thread migration enforces sequential consistency
  60. */
  61. return atomic_load(&clock_global_time.ticks, ATOMIC_RELAXED);
  62. #else /* ATOMIC_HAVE_64B_OPS */
  63. uint32_t high1, low, high2;
  64. /*
  65. * For machines with no 64-bits atomic accessors, this implementation uses
  66. * a variant of the two-digit monotonic-clock algorithm, described in the
  67. * paper "Concurrent Reading and Writing of Clocks" by Leslie Lamport.
  68. */
  69. do {
  70. high1 = atomic_load(&clock_global_time.high1, ATOMIC_ACQUIRE);
  71. low = atomic_load(&clock_global_time.low, ATOMIC_ACQUIRE);
  72. high2 = atomic_load(&clock_global_time.high2, ATOMIC_RELAXED);
  73. } while (high1 != high2);
  74. return ((uint64_t)high2 << 32) | low;
  75. #endif /* ATOMIC_HAVE_64B_OPS */
  76. }
  77. static inline uint64_t
  78. clock_ticks_to_ms(uint64_t ticks)
  79. {
  80. return ticks * (1000 / CLOCK_FREQ);
  81. }
  82. static inline uint64_t
  83. clock_ticks_from_ms(uint64_t ms)
  84. {
  85. return DIV_CEIL(ms, (1000 / CLOCK_FREQ));
  86. }
  87. static inline bool
  88. clock_time_expired(uint64_t t, uint64_t ref)
  89. {
  90. return (t - ref) > CLOCK_EXPIRE_THRESHOLD;
  91. }
  92. static inline bool
  93. clock_time_occurred(uint64_t t, uint64_t ref)
  94. {
  95. return (t == ref) || clock_time_expired(t, ref);
  96. }
  97. void clock_tick_intr(void);
  98. #endif /* KERN_CLOCK_H */