timer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * Low resolution timer system.
  19. */
  20. #ifndef KERN_TIMER_H
  21. #define KERN_TIMER_H
  22. #include <stdint.h>
  23. #include <kern/hlist.h>
  24. #include <kern/init.h>
  25. #include <kern/work.h>
  26. // Scheduling flags.
  27. #define TIMER_DETACHED 0x1 // Timer completion isn't synchronized.
  28. #define TIMER_INTR 0x2 // Handler is run from interrupt context.
  29. #define TIMER_HIGH_PRIO 0x4 // Handler is run in high priority thread.
  30. struct timer;
  31. // Type for timer functions.
  32. typedef void (*timer_fn_t) (struct timer *);
  33. /*
  34. * Locking keys :
  35. * (c) cpu_data
  36. * (a) atomic
  37. *
  38. * (*) The cpu member is used to determine which lock serializes access to
  39. * the structure. It must be accessed atomically, but updated while the
  40. * timer is locked.
  41. */
  42. struct timer
  43. {
  44. union
  45. {
  46. struct hlist_node node; // (c)
  47. struct work work;
  48. };
  49. uint64_t ticks; // (c)
  50. timer_fn_t fn;
  51. unsigned int cpu; // (c,a,*)
  52. unsigned short state; // (c)
  53. unsigned short flags; // (c)
  54. struct thread *joiner; // (c)
  55. };
  56. /*
  57. * Return the absolute expiration time of the timer, in ticks.
  58. *
  59. * This function may not be called while another thread is scheduling the
  60. * timer.
  61. */
  62. static inline uint64_t
  63. timer_get_time (const struct timer *timer)
  64. {
  65. return (timer->ticks);
  66. }
  67. /*
  68. * Initialize a timer.
  69. *
  70. * Timers that are reponsible for releasing their own resources must
  71. * be detached.
  72. */
  73. void timer_init (struct timer *timer, timer_fn_t fn, int flags);
  74. /*
  75. * Schedule a timer.
  76. *
  77. * The time of expiration is an absolute time in ticks.
  78. *
  79. * Timers may safely be rescheduled after completion. Periodic timers are
  80. * implemented by rescheduling from the handler.
  81. *
  82. * If the timer has been canceled, this function does nothing. A
  83. * canceled timer must be reinitialized before being scheduled again.
  84. *
  85. * This function may safely be called in interrupt context.
  86. */
  87. void timer_schedule (struct timer *timer, uint64_t ticks);
  88. /*
  89. * Cancel a timer.
  90. *
  91. * The given timer must not be detached.
  92. *
  93. * If the timer has already expired, this function waits until the timer
  94. * function completes, or returns immediately if the function has already
  95. * completed.
  96. *
  97. * This function may safely be called from the timer handler, but not on
  98. * the current timer. Canceling a timer from the handler is achieved by
  99. * simply not rescheduling it.
  100. */
  101. void timer_cancel (struct timer *timer);
  102. /*
  103. * Report a periodic event on the current processor.
  104. *
  105. * Interrupts and preemption must be disabled when calling this function.
  106. */
  107. void timer_report_periodic_event (void);
  108. /*
  109. * This init operation provides :
  110. * - timer initialization and scheduling
  111. */
  112. INIT_OP_DECLARE (timer_bootstrap);
  113. #endif