blob: 71bc4fb052a2f6dea32d0cee73a5f56ca5db74c2 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 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#include <debug.h>
24#include <list.h>
25#include <kernel/thread.h>
26#include <kernel/timer.h>
27#include <platform/timer.h>
28#include <platform.h>
29
30static struct list_node timer_queue;
31
32void timer_initialize(timer_t *timer)
33{
34 timer->magic = TIMER_MAGIC;
35 list_clear_node(&timer->node);
36 timer->scheduled_time = 0;
37 timer->periodic_time = 0;
38 timer->callback = 0;
39 timer->arg = 0;
40}
41
42static void insert_timer_in_queue(timer_t *timer)
43{
44 timer_t *entry;
45
46 list_for_every_entry(&timer_queue, entry, timer_t, node) {
47 if (entry->scheduled_time > timer->scheduled_time) {
48 list_add_before(&entry->node, &timer->node);
49 return;
50 }
51 }
52
53 /* walked off the end of the list */
54 list_add_tail(&timer_queue, &timer->node);
55}
56
57void timer_set_oneshot(timer_t *timer, time_t delay, timer_callback callback, void *arg)
58{
59 time_t now;
60
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070061// TRACEF("delay %d, callback %p, arg %p\n", delay, callback, arg);
62
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070063 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
64
65 if (list_in_list(&timer->node)) {
66 panic("timer %p already in list\n", timer);
67 }
68
69 now = current_time();
70 timer->scheduled_time = now + delay;
71 timer->periodic_time = 0;
72 timer->callback = callback;
73 timer->arg = arg;
74
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070075// TRACEF("scheduled time %u\n", timer->scheduled_time);
76
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070077 enter_critical_section();
78
79 insert_timer_in_queue(timer);
80
81 exit_critical_section();
82}
83
84void timer_cancel(timer_t *timer)
85{
86 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
87
88 enter_critical_section();
89
90 if (list_in_list(&timer->node))
91 list_delete(&timer->node);
92
93 exit_critical_section();
94}
95
96/* called at interrupt time to process any pending timers */
97static enum handler_return timer_tick(void *arg, time_t now)
98{
99 timer_t *timer;
100 enum handler_return ret = INT_NO_RESCHEDULE;
101
102#if THREAD_STATS
103 thread_stats.timer_ints++;
104#endif
105
106 for (;;) {
107 /* see if there's an event to process */
108 timer = list_peek_head_type(&timer_queue, timer_t, node);
109 if (likely(!timer || now < timer->scheduled_time))
110 break;
111
112 /* process it */
113 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
114 list_delete(&timer->node);
115// timer = list_remove_head_type(&timer_queue, timer_t, node);
116// ASSERT(timer);
117
118#if THREAD_STATS
119 thread_stats.timers++;
120#endif
121
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700122// TRACEF("firing callback %p, arg %p\n", timer->callback, timer->arg);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700123 if (timer->callback(timer, now, timer->arg) == INT_RESCHEDULE)
124 ret = INT_RESCHEDULE;
125 }
126
127 /* let the scheduler have a shot to do quantum expiration, etc */
128 if (thread_timer_tick() == INT_RESCHEDULE)
129 ret = INT_RESCHEDULE;
130
131 return INT_RESCHEDULE;
132}
133
134void timer_init(void)
135{
136 list_initialize(&timer_queue);
137
138 /* register for a periodic timer tick */
139 platform_set_periodic_timer(timer_tick, NULL, 10); /* 10ms */
140}
141
142