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