clock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <stdalign.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <kern/atomic.h>
  26. #include <kern/init.h>
  27. #include <kern/macros.h>
  28. #include <machine/cpu.h>
  29. union clock_global_time
  30. {
  31. __cacheline_aligned uint64_t ticks;
  32. #ifndef __LP64__
  33. struct
  34. {
  35. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  36. uint32_t high1;
  37. uint32_t low;
  38. #else
  39. uint32_t low;
  40. uint32_t high1;
  41. #endif
  42. uint32_t high2;
  43. };
  44. #endif
  45. };
  46. // Clock frequency.
  47. #define CLOCK_FREQ CONFIG_CLOCK_FREQ
  48. #if CLOCK_FREQ < 100 || CLOCK_FREQ > 1000 || (1000 % CLOCK_FREQ) != 0
  49. #error "invalid clock frequency"
  50. #endif
  51. /*
  52. * Arbitrary value used to determine if a time is in the past or the future.
  53. *
  54. * Time is represented as 64-bits unsigned integers counting ticks. The
  55. * global time currently starts from 0 but this isn't a strong assumption
  56. * users should rely on. Instead, all time checks involve a time reference
  57. * against which to compare. The result of that comparison, done by
  58. * substraction, is either in the future, i.e. the difference is less
  59. * than the expire threshold, or in the past, i.e. the difference is
  60. * greater (keep in mind the result is unsigned). The threshold must be
  61. * large enough to allow both a wide range of possible times in the future,
  62. * but also enough time in the past for reliable timeout detection. Note
  63. * that using signed integers would be equivalent to dividing the range
  64. * in two (almost) equal past and future halves.
  65. */
  66. #define CLOCK_EXPIRE_THRESHOLD (-(1ULL << 60))
  67. static inline uint64_t
  68. clock_get_time (void)
  69. {
  70. extern union clock_global_time clock_global_time;
  71. #ifdef __LP64__
  72. /*
  73. * Don't enforce a stronger memory order, since :
  74. * 1/ it's useless as long as the reader remains on the same processor
  75. * 2/ thread migration enforces sequential consistency
  76. */
  77. return (atomic_load_rlx (&clock_global_time.ticks));
  78. #else // ATOMIC_HAVE_64B_OPS.
  79. uint32_t high1, low, high2;
  80. /*
  81. * For 32-bit machines, this implementation uses a variant of the two-digit
  82. * monotonic-clock algorithm, described in the paper "Concurrent Reading and
  83. * Writing of Clocks" by Leslie Lamport.
  84. */
  85. do
  86. {
  87. high1 = atomic_load_acq (&clock_global_time.high1);
  88. low = atomic_load_acq (&clock_global_time.low);
  89. high2 = atomic_load_rlx (&clock_global_time.high2);
  90. }
  91. while (high1 != high2);
  92. return (((uint64_t) high2 << 32) | low);
  93. #endif
  94. }
  95. static inline uint64_t
  96. clock_ticks_to_ms (uint64_t ticks)
  97. {
  98. return (ticks * (1000 / CLOCK_FREQ));
  99. }
  100. static inline uint64_t
  101. clock_ticks_from_ms (uint64_t ms)
  102. {
  103. return (DIV_CEIL (ms, (1000 / CLOCK_FREQ)));
  104. }
  105. static inline bool
  106. clock_time_expired (uint64_t t, uint64_t ref)
  107. {
  108. return (t - ref > CLOCK_EXPIRE_THRESHOLD);
  109. }
  110. static inline bool
  111. clock_time_occurred (uint64_t t, uint64_t ref)
  112. {
  113. return (t == ref || clock_time_expired (t, ref));
  114. }
  115. void clock_tick_intr (void);
  116. #endif