blob: 9a472f579126e28e6a9a35bf95d5a2145fce87f1 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
Travis Geiselbrecht671cb792009-06-28 11:27:48 -07002 * Copyright (c) 2008-2009 Travis Geiselbrecht
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07003 *
Unnati Gandhi86a7b2d2014-07-17 14:40:35 +05304 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
5 *
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07006 * 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 Geiselbrecht12403e32010-05-06 13:36:57 -070025
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 Geiselbrecht1d0df692008-09-01 02:26:09 -070039#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
46static struct list_node timer_queue;
47
Travis Geiselbrecht671cb792009-06-28 11:27:48 -070048static enum handler_return timer_tick(void *arg, time_t now);
49
Travis Geiselbrecht12403e32010-05-06 13:36:57 -070050/**
51 * @brief Initialize a timer object
52 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070053void 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
63static void insert_timer_in_queue(timer_t *timer)
64{
65 timer_t *entry;
66
Travis Geiselbrecht671cb792009-06-28 11:27:48 -070067// TRACEF("timer %p, scheduled %d, periodic %d\n", timer, timer->scheduled_time, timer->periodic_time);
68
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070069 list_for_every_entry(&timer_queue, entry, timer_t, node) {
Travis Geiselbrecht671cb792009-06-28 11:27:48 -070070 if (TIME_GT(entry->scheduled_time, timer->scheduled_time)) {
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070071 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 Geiselbrecht671cb792009-06-28 11:27:48 -070080static void timer_set(timer_t *timer, time_t delay, time_t period, timer_callback callback, void *arg)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070081{
82 time_t now;
83
Travis Geiselbrecht671cb792009-06-28 11:27:48 -070084// TRACEF("timer %p, delay %d, period %d, callback %p, arg %p, now %d\n", timer, delay, period, callback, arg);
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070085
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070086 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 Geiselbrecht671cb792009-06-28 11:27:48 -070094 timer->periodic_time = period;
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070095 timer->callback = callback;
96 timer->arg = arg;
97
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070098// TRACEF("scheduled time %u\n", timer->scheduled_time);
99
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700100 enter_critical_section();
101
102 insert_timer_in_queue(timer);
103
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700104#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 Geiselbrecht1d0df692008-09-01 02:26:09 -0700112 exit_critical_section();
113}
114
Travis Geiselbrecht12403e32010-05-06 13:36:57 -0700115/**
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 Geiselbrecht671cb792009-06-28 11:27:48 -0700129void 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 Geiselbrecht12403e32010-05-06 13:36:57 -0700136/**
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 Geiselbrecht671cb792009-06-28 11:27:48 -0700150void 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 Geiselbrecht12403e32010-05-06 13:36:57 -0700157/**
158 * @brief Cancel a pending timer
159 */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700160void timer_cancel(timer_t *timer)
161{
162 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
163
164 enter_critical_section();
165
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700166#if PLATFORM_HAS_DYNAMIC_TIMER
167 timer_t *oldhead = list_peek_head_type(&timer_queue, timer_t, node);
168#endif
169
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700170 if (list_in_list(&timer->node))
171 list_delete(&timer->node);
172
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700173 /* 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 Geiselbrecht1d0df692008-09-01 02:26:09 -0700200 exit_critical_section();
201}
202
203/* called at interrupt time to process any pending timers */
204static 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 Geiselbrecht671cb792009-06-28 11:27:48 -0700213// TRACEF("now %d\n", now);
214
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700215 for (;;) {
216 /* see if there's an event to process */
217 timer = list_peek_head_type(&timer_queue, timer_t, node);
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700218 if (likely(!timer || TIME_LT(now, timer->scheduled_time)))
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700219 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 Geiselbrecht671cb792009-06-28 11:27:48 -0700227// TRACEF("dequeued timer %p, scheduled %d periodic %d\n", timer, timer->scheduled_time, timer->periodic_time);
228
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700229#if THREAD_STATS
230 thread_stats.timers++;
231#endif
232
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700233 bool periodic = timer->periodic_time > 0;
234
235// TRACEF("timer %p firing callback %p, arg %p\n", timer, timer->callback, timer->arg);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700236 if (timer->callback(timer, now, timer->arg) == INT_RESCHEDULE)
237 ret = INT_RESCHEDULE;
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700238
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 Geiselbrecht1d0df692008-09-01 02:26:09 -0700247 }
248
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700249#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 Geiselbrecht1d0df692008-09-01 02:26:09 -0700262 /* let the scheduler have a shot to do quantum expiration, etc */
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700263 /* in case of dynamic timer, the scheduler will set up a periodic timer */
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700264 if (thread_timer_tick() == INT_RESCHEDULE)
265 ret = INT_RESCHEDULE;
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700266#endif
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700267
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700268 // XXX fix this, should return ret
Unnati Gandhi86a7b2d2014-07-17 14:40:35 +0530269 ret = INT_RESCHEDULE;
270 return ret;
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700271}
272
273void 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