timer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/init.h>
  24. /*
  25. * Scheduling flags.
  26. */
  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. /*
  32. * Type for timer functions.
  33. */
  34. typedef void (*timer_fn_t)(struct timer *);
  35. #include <kern/timer_i.h>
  36. /*
  37. * Return the absolute expiration time of the timer, in ticks.
  38. *
  39. * This function may not be called while another thread is scheduling the
  40. * timer.
  41. */
  42. static inline uint64_t
  43. timer_get_time(const struct timer *timer)
  44. {
  45. return timer->ticks;
  46. }
  47. /*
  48. * Initialize a timer.
  49. *
  50. * Timers that are reponsible for releasing their own resources must
  51. * be detached.
  52. */
  53. void timer_init(struct timer *timer, timer_fn_t fn, int flags);
  54. /*
  55. * Schedule a timer.
  56. *
  57. * The time of expiration is an absolute time in ticks.
  58. *
  59. * Timers may safely be rescheduled after completion. Periodic timers are
  60. * implemented by rescheduling from the handler.
  61. *
  62. * If the timer has been canceled, this function does nothing. A
  63. * canceled timer must be reinitialized before being scheduled again.
  64. *
  65. * This function may safely be called in interrupt context.
  66. */
  67. void timer_schedule(struct timer *timer, uint64_t ticks);
  68. /*
  69. * Cancel a timer.
  70. *
  71. * The given timer must not be detached.
  72. *
  73. * If the timer has already expired, this function waits until the timer
  74. * function completes, or returns immediately if the function has already
  75. * completed.
  76. *
  77. * This function may safely be called from the timer handler, but not on
  78. * the current timer. Canceling a timer from the handler is achieved by
  79. * simply not rescheduling it.
  80. */
  81. void timer_cancel(struct timer *timer);
  82. /*
  83. * Report a periodic event on the current processor.
  84. *
  85. * Interrupts and preemption must be disabled when calling this function.
  86. */
  87. void timer_report_periodic_event(void);
  88. /*
  89. * This init operation provides :
  90. * - timer initialization and scheduling
  91. */
  92. INIT_OP_DECLARE(timer_bootstrap);
  93. #endif /* KERN_TIMER_H */