1 |
/* |
2 |
* Copyright (C) 2006-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: timer.c,v 1.6 2006/12/30 13:30:53 debug Exp $ |
29 |
* |
30 |
* Timer framework. This is used by emulated clocks. |
31 |
*/ |
32 |
|
33 |
#include <stdio.h> |
34 |
#include <stdlib.h> |
35 |
#include <signal.h> |
36 |
#include <string.h> |
37 |
#include <unistd.h> |
38 |
#include <sys/time.h> |
39 |
|
40 |
#include "timer.h" |
41 |
|
42 |
|
43 |
/* #define TEST */ |
44 |
|
45 |
|
46 |
struct timer { |
47 |
struct timer *next; |
48 |
|
49 |
double freq; |
50 |
void (*timer_tick)(struct timer *timer, void *extra); |
51 |
void *extra; |
52 |
|
53 |
double interval; |
54 |
double next_tick_at; |
55 |
}; |
56 |
|
57 |
static struct timer *first_timer = NULL; |
58 |
struct timeval timer_start_tv; |
59 |
static double timer_freq; |
60 |
static int timer_countdown_to_next_gettimeofday; |
61 |
static double timer_current_time; |
62 |
static double timer_current_time_step; |
63 |
|
64 |
static int timer_is_running; |
65 |
|
66 |
#define SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH 1.5 |
67 |
|
68 |
|
69 |
/* |
70 |
* timer_add(): |
71 |
* |
72 |
* Adds a virtual timer to the list of timers. |
73 |
* |
74 |
* Return value is a pointer to a timer struct. |
75 |
*/ |
76 |
struct timer *timer_add(double freq, void (*timer_tick)(struct timer *timer, |
77 |
void *extra), void *extra) |
78 |
{ |
79 |
struct timer *newtimer = malloc(sizeof(struct timer)); |
80 |
if (newtimer == NULL) { |
81 |
fprintf(stderr, "out of memory\n"); |
82 |
exit(1); |
83 |
} |
84 |
|
85 |
if (freq <= 0.00000001) |
86 |
freq = 0.00000001; |
87 |
|
88 |
newtimer->freq = freq; |
89 |
newtimer->timer_tick = timer_tick; |
90 |
newtimer->extra = extra; |
91 |
|
92 |
newtimer->interval = 1.0 / freq; |
93 |
newtimer->next_tick_at = timer_current_time + newtimer->interval; |
94 |
|
95 |
newtimer->next = first_timer; |
96 |
first_timer = newtimer; |
97 |
|
98 |
return newtimer; |
99 |
} |
100 |
|
101 |
|
102 |
/* |
103 |
* timer_remove(): |
104 |
* |
105 |
* Removes a virtual timer from the list of timers. |
106 |
*/ |
107 |
void timer_remove(struct timer *t) |
108 |
{ |
109 |
struct timer *prev = NULL, *cur = first_timer; |
110 |
|
111 |
while (cur != NULL && cur != t) { |
112 |
prev = cur; |
113 |
cur = cur->next; |
114 |
} |
115 |
|
116 |
if (cur == t) { |
117 |
if (prev == NULL) |
118 |
first_timer = cur->next; |
119 |
else |
120 |
prev->next = cur->next; |
121 |
free(cur); |
122 |
} else { |
123 |
fprintf(stderr, "attempt to remove timer %p which " |
124 |
"doesn't exist. aborting\n", t); |
125 |
exit(1); |
126 |
} |
127 |
} |
128 |
|
129 |
|
130 |
/* |
131 |
* timer_update_frequency(): |
132 |
* |
133 |
* Changes the frequency of an existing timer. |
134 |
*/ |
135 |
void timer_update_frequency(struct timer *t, double new_freq) |
136 |
{ |
137 |
if (t->freq == new_freq) |
138 |
return; |
139 |
|
140 |
t->freq = new_freq; |
141 |
|
142 |
if (new_freq <= 0.00000001) |
143 |
new_freq = 0.00000001; |
144 |
|
145 |
t->interval = 1.0 / new_freq; |
146 |
t->next_tick_at = timer_current_time + t->interval; |
147 |
} |
148 |
|
149 |
|
150 |
/* |
151 |
* timer_tick(): |
152 |
* |
153 |
* Timer tick handler. This is where the interesting stuff happens. |
154 |
*/ |
155 |
static void timer_tick(int signal_nr) |
156 |
{ |
157 |
struct timer *timer = first_timer; |
158 |
struct timeval tv; |
159 |
|
160 |
timer_current_time += timer_current_time_step; |
161 |
|
162 |
if ((--timer_countdown_to_next_gettimeofday) < 0) { |
163 |
gettimeofday(&tv, NULL); |
164 |
tv.tv_sec -= timer_start_tv.tv_sec; |
165 |
tv.tv_usec -= timer_start_tv.tv_usec; |
166 |
if (tv.tv_usec < 0) { |
167 |
tv.tv_usec += 1000000; |
168 |
tv.tv_sec --; |
169 |
} |
170 |
|
171 |
#if 0 |
172 |
/* For debugging/testing: */ |
173 |
{ |
174 |
double diff = tv.tv_usec * 0.000001 + tv.tv_sec |
175 |
- timer_current_time; |
176 |
printf("timer: lagging behind %f seconds\n", diff); |
177 |
} |
178 |
#endif |
179 |
|
180 |
timer_current_time = tv.tv_usec * 0.000001 + tv.tv_sec; |
181 |
|
182 |
timer_countdown_to_next_gettimeofday = timer_freq * |
183 |
SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH; |
184 |
} |
185 |
|
186 |
while (timer != NULL) { |
187 |
while (timer_current_time >= timer->next_tick_at) { |
188 |
timer->timer_tick(timer, timer->extra); |
189 |
timer->next_tick_at += timer->interval; |
190 |
} |
191 |
|
192 |
timer = timer->next; |
193 |
} |
194 |
|
195 |
#ifdef TEST |
196 |
printf("T"); fflush(stdout); |
197 |
#endif |
198 |
} |
199 |
|
200 |
|
201 |
/* |
202 |
* timer_start(): |
203 |
* |
204 |
* Set the interval timer to timer_freq Hz, and install the signal handler. |
205 |
*/ |
206 |
void timer_start(void) |
207 |
{ |
208 |
struct timer *timer = first_timer; |
209 |
struct itimerval val; |
210 |
struct sigaction saction; |
211 |
|
212 |
if (timer_is_running) |
213 |
return; |
214 |
|
215 |
timer_is_running = 1; |
216 |
|
217 |
gettimeofday(&timer_start_tv, NULL); |
218 |
timer_current_time = 0.0; |
219 |
|
220 |
/* Reset all timers: */ |
221 |
while (timer != NULL) { |
222 |
timer->next_tick_at = timer->interval; |
223 |
timer = timer->next; |
224 |
} |
225 |
val.it_interval.tv_sec = 0; |
226 |
val.it_interval.tv_usec = 1000000.0 / timer_freq; |
227 |
val.it_value.tv_sec = 0; |
228 |
val.it_value.tv_usec = 1000000.0 / timer_freq; |
229 |
|
230 |
memset(&saction, 0, sizeof(saction)); |
231 |
saction.sa_handler = timer_tick; |
232 |
|
233 |
sigaction(SIGALRM, &saction, NULL); |
234 |
|
235 |
setitimer(ITIMER_REAL, &val, NULL); |
236 |
} |
237 |
|
238 |
|
239 |
/* |
240 |
* timer_stop(): |
241 |
* |
242 |
* Deinstall the signal handler, and disable the interval timer. |
243 |
*/ |
244 |
void timer_stop(void) |
245 |
{ |
246 |
struct itimerval val; |
247 |
struct sigaction saction; |
248 |
|
249 |
if (!timer_is_running) |
250 |
return; |
251 |
|
252 |
timer_is_running = 0; |
253 |
|
254 |
val.it_interval.tv_sec = 0; |
255 |
val.it_interval.tv_usec = 0; |
256 |
val.it_value.tv_sec = 0; |
257 |
val.it_value.tv_usec = 0; |
258 |
|
259 |
setitimer(ITIMER_REAL, &val, NULL); |
260 |
|
261 |
memset(&saction, 0, sizeof(saction)); |
262 |
saction.sa_handler = NULL; |
263 |
|
264 |
sigaction(SIGALRM, &saction, NULL); |
265 |
} |
266 |
|
267 |
|
268 |
#ifdef TEST |
269 |
static void timer_tick_test(struct timer *t, void *extra) |
270 |
{ |
271 |
printf((char *) extra); fflush(stdout); |
272 |
} |
273 |
#endif |
274 |
|
275 |
|
276 |
/* |
277 |
* timer_init(): |
278 |
* |
279 |
* Initialize the timer framework. |
280 |
*/ |
281 |
void timer_init(void) |
282 |
{ |
283 |
first_timer = NULL; |
284 |
timer_current_time = 0.0; |
285 |
timer_is_running = 0; |
286 |
timer_countdown_to_next_gettimeofday = 0; |
287 |
|
288 |
timer_freq = 50.0; |
289 |
timer_current_time_step = 1.0 / timer_freq; |
290 |
|
291 |
#ifdef TEST |
292 |
timer_add(0.5, timer_tick_test, "X"); |
293 |
timer_add(10.0, timer_tick_test, "."); |
294 |
timer_add(200.0, timer_tick_test, " "); |
295 |
timer_start(); |
296 |
while (1) |
297 |
sleep(999999); |
298 |
#endif |
299 |
} |
300 |
|