timer.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2008-2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * @file
  25. * @brief Kernel timer subsystem
  26. * @defgroup timer Timers
  27. *
  28. * The timer subsystem allows functions to be scheduled for later
  29. * execution. Each timer object is used to cause one function to
  30. * be executed at a later time.
  31. *
  32. * Timer callback functions are called in interrupt context.
  33. *
  34. * @{
  35. */
  36. #include <debug.h>
  37. #include <list.h>
  38. #include <kernel/thread.h>
  39. #include <kernel/timer.h>
  40. #include <platform/timer.h>
  41. #include <platform.h>
  42. static struct list_node timer_queue;
  43. static enum handler_return timer_tick(void *arg, time_t now);
  44. /**
  45. * @brief Initialize a timer object
  46. */
  47. void timer_initialize(timer_t *timer)
  48. {
  49. timer->magic = TIMER_MAGIC;
  50. list_clear_node(&timer->node);
  51. timer->scheduled_time = 0;
  52. timer->periodic_time = 0;
  53. timer->callback = 0;
  54. timer->arg = 0;
  55. }
  56. static void insert_timer_in_queue(timer_t *timer)
  57. {
  58. timer_t *entry;
  59. // TRACEF("timer %p, scheduled %d, periodic %d\n", timer, timer->scheduled_time, timer->periodic_time);
  60. list_for_every_entry(&timer_queue, entry, timer_t, node) {
  61. if (TIME_GT(entry->scheduled_time, timer->scheduled_time)) {
  62. list_add_before(&entry->node, &timer->node);
  63. return;
  64. }
  65. }
  66. /* walked off the end of the list */
  67. list_add_tail(&timer_queue, &timer->node);
  68. }
  69. static void timer_set(timer_t *timer, time_t delay, time_t period, timer_callback callback, void *arg)
  70. {
  71. time_t now;
  72. // TRACEF("timer %p, delay %d, period %d, callback %p, arg %p, now %d\n", timer, delay, period, callback, arg);
  73. DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
  74. if (list_in_list(&timer->node)) {
  75. panic("timer %p already in list\n", timer);
  76. }
  77. now = current_time();
  78. timer->scheduled_time = now + delay;
  79. timer->periodic_time = period;
  80. timer->callback = callback;
  81. timer->arg = arg;
  82. // TRACEF("scheduled time %u\n", timer->scheduled_time);
  83. enter_critical_section();
  84. insert_timer_in_queue(timer);
  85. #if PLATFORM_HAS_DYNAMIC_TIMER
  86. if (list_peek_head_type(&timer_queue, timer_t, node) == timer) {
  87. /* we just modified the head of the timer queue */
  88. // TRACEF("setting new timer for %u msecs\n", (uint)delay);
  89. platform_set_oneshot_timer(timer_tick, NULL, delay);
  90. }
  91. #endif
  92. exit_critical_section();
  93. }
  94. /**
  95. * @brief Set up a timer that executes once
  96. *
  97. * This function specifies a callback function to be called after a specified
  98. * delay. The function will be called one time.
  99. *
  100. * @param timer The timer to use
  101. * @param delay The delay, in ms, before the timer is executed
  102. * @param callback The function to call when the timer expires
  103. * @param arg The argument to pass to the callback
  104. *
  105. * The timer function is declared as:
  106. * enum handler_return callback(struct timer *, time_t now, void *arg) { ... }
  107. */
  108. void timer_set_oneshot(timer_t *timer, time_t delay, timer_callback callback, void *arg)
  109. {
  110. if (delay == 0)
  111. delay = 1;
  112. timer_set(timer, delay, 0, callback, arg);
  113. }
  114. /**
  115. * @brief Set up a timer that executes repeatedly
  116. *
  117. * This function specifies a callback function to be called after a specified
  118. * delay. The function will be called repeatedly.
  119. *
  120. * @param timer The timer to use
  121. * @param delay The delay, in ms, before the timer is executed
  122. * @param callback The function to call when the timer expires
  123. * @param arg The argument to pass to the callback
  124. *
  125. * The timer function is declared as:
  126. * enum handler_return callback(struct timer *, time_t now, void *arg) { ... }
  127. */
  128. void timer_set_periodic(timer_t *timer, time_t period, timer_callback callback, void *arg)
  129. {
  130. if (period == 0)
  131. period = 1;
  132. timer_set(timer, period, period, callback, arg);
  133. }
  134. /**
  135. * @brief Cancel a pending timer
  136. */
  137. void timer_cancel(timer_t *timer)
  138. {
  139. DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
  140. enter_critical_section();
  141. #if PLATFORM_HAS_DYNAMIC_TIMER
  142. timer_t *oldhead = list_peek_head_type(&timer_queue, timer_t, node);
  143. #endif
  144. if (list_in_list(&timer->node))
  145. list_delete(&timer->node);
  146. /* to keep it from being reinserted into the queue if called from
  147. * periodic timer callback.
  148. */
  149. timer->periodic_time = 0;
  150. timer->callback = NULL;
  151. timer->arg = NULL;
  152. #if PLATFORM_HAS_DYNAMIC_TIMER
  153. /* see if we've just modified the head of the timer queue */
  154. timer_t *newhead = list_peek_head_type(&timer_queue, timer_t, node);
  155. if (newhead == NULL) {
  156. // TRACEF("clearing old hw timer, nothing in the queue\n");
  157. platform_stop_timer();
  158. } else if (newhead != oldhead) {
  159. time_t delay;
  160. time_t now = current_time();
  161. if (TIME_LT(newhead->scheduled_time, now))
  162. delay = 0;
  163. else
  164. delay = newhead->scheduled_time - now;
  165. // TRACEF("setting new timer to %d\n", delay);
  166. platform_set_oneshot_timer(timer_tick, NULL, delay);
  167. }
  168. #endif
  169. exit_critical_section();
  170. }
  171. /* called at interrupt time to process any pending timers */
  172. static enum handler_return timer_tick(void *arg, time_t now)
  173. {
  174. timer_t *timer;
  175. enum handler_return ret = INT_NO_RESCHEDULE;
  176. #if THREAD_STATS
  177. thread_stats.timer_ints++;
  178. #endif
  179. // TRACEF("now %d\n", now);
  180. for (;;) {
  181. /* see if there's an event to process */
  182. timer = list_peek_head_type(&timer_queue, timer_t, node);
  183. if (likely(!timer || TIME_LT(now, timer->scheduled_time)))
  184. break;
  185. /* process it */
  186. DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
  187. list_delete(&timer->node);
  188. // timer = list_remove_head_type(&timer_queue, timer_t, node);
  189. // ASSERT(timer);
  190. // TRACEF("dequeued timer %p, scheduled %d periodic %d\n", timer, timer->scheduled_time, timer->periodic_time);
  191. #if THREAD_STATS
  192. thread_stats.timers++;
  193. #endif
  194. bool periodic = timer->periodic_time > 0;
  195. // TRACEF("timer %p firing callback %p, arg %p\n", timer, timer->callback, timer->arg);
  196. if (timer->callback(timer, now, timer->arg) == INT_RESCHEDULE)
  197. {
  198. ret = INT_RESCHEDULE;
  199. }
  200. /* if it was a periodic timer and it hasn't been requeued
  201. * by the callback put it back in the list
  202. */
  203. if (periodic && !list_in_list(&timer->node) && timer->periodic_time > 0) {
  204. // TRACEF("periodic timer, period %u\n", (uint)timer->periodic_time);
  205. timer->scheduled_time = now + timer->periodic_time;
  206. insert_timer_in_queue(timer);
  207. }
  208. }
  209. #if PLATFORM_HAS_DYNAMIC_TIMER
  210. /* reset the timer to the next event */
  211. timer = list_peek_head_type(&timer_queue, timer_t, node);
  212. if (timer) {
  213. /* has to be the case or it would have fired already */
  214. ASSERT(TIME_GT(timer->scheduled_time, now));
  215. time_t delay = timer->scheduled_time - now;
  216. // TRACEF("setting new timer for %u msecs for event %p\n", (uint)delay, timer);
  217. platform_set_oneshot_timer(timer_tick, NULL, delay);
  218. }
  219. #else
  220. /* let the scheduler have a shot to do quantum expiration, etc */
  221. /* in case of dynamic timer, the scheduler will set up a periodic timer */
  222. if (thread_timer_tick() == INT_RESCHEDULE)
  223. {
  224. ret = INT_RESCHEDULE;
  225. }
  226. #endif
  227. // XXX fix this, should return ret
  228. return ret;
  229. }
  230. void timer_init(void)
  231. {
  232. list_initialize(&timer_queue);
  233. /* register for a periodic timer tick */
  234. platform_set_periodic_timer(timer_tick, NULL, 10); /* 10ms */
  235. }