blob: eb86e4ad74f38637eb5d39b3e50d3323ebe1a3fc [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
61 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
62
63 if (list_in_list(&timer->node)) {
64 panic("timer %p already in list\n", timer);
65 }
66
67 now = current_time();
68 timer->scheduled_time = now + delay;
69 timer->periodic_time = 0;
70 timer->callback = callback;
71 timer->arg = arg;
72
73 enter_critical_section();
74
75 insert_timer_in_queue(timer);
76
77 exit_critical_section();
78}
79
80void timer_cancel(timer_t *timer)
81{
82 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
83
84 enter_critical_section();
85
86 if (list_in_list(&timer->node))
87 list_delete(&timer->node);
88
89 exit_critical_section();
90}
91
92/* called at interrupt time to process any pending timers */
93static enum handler_return timer_tick(void *arg, time_t now)
94{
95 timer_t *timer;
96 enum handler_return ret = INT_NO_RESCHEDULE;
97
98#if THREAD_STATS
99 thread_stats.timer_ints++;
100#endif
101
102 for (;;) {
103 /* see if there's an event to process */
104 timer = list_peek_head_type(&timer_queue, timer_t, node);
105 if (likely(!timer || now < timer->scheduled_time))
106 break;
107
108 /* process it */
109 DEBUG_ASSERT(timer->magic == TIMER_MAGIC);
110 list_delete(&timer->node);
111// timer = list_remove_head_type(&timer_queue, timer_t, node);
112// ASSERT(timer);
113
114#if THREAD_STATS
115 thread_stats.timers++;
116#endif
117
118 if (timer->callback(timer, now, timer->arg) == INT_RESCHEDULE)
119 ret = INT_RESCHEDULE;
120 }
121
122 /* let the scheduler have a shot to do quantum expiration, etc */
123 if (thread_timer_tick() == INT_RESCHEDULE)
124 ret = INT_RESCHEDULE;
125
126 return INT_RESCHEDULE;
127}
128
129void timer_init(void)
130{
131 list_initialize(&timer_queue);
132
133 /* register for a periodic timer tick */
134 platform_set_periodic_timer(timer_tick, NULL, 10); /* 10ms */
135}
136
137