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.9 2007/06/15 17:02:38 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 "misc.h" |
41 |
#include "timer.h" |
42 |
|
43 |
|
44 |
/* #define TEST */ |
45 |
|
46 |
|
47 |
struct timer { |
48 |
struct timer *next; |
49 |
|
50 |
double freq; |
51 |
void (*timer_tick)(struct timer *timer, void *extra); |
52 |
void *extra; |
53 |
|
54 |
double interval; |
55 |
double next_tick_at; |
56 |
}; |
57 |
|
58 |
static struct timer *first_timer = NULL; |
59 |
struct timeval timer_start_tv; |
60 |
static double timer_freq; |
61 |
static int timer_countdown_to_next_gettimeofday; |
62 |
static double timer_current_time; |
63 |
static double timer_current_time_step; |
64 |
|
65 |
static int timer_is_running; |
66 |
|
67 |
#define SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH 1.65 |
68 |
|
69 |
|
70 |
/* |
71 |
* timer_add(): |
72 |
* |
73 |
* Adds a virtual timer to the list of timers. |
74 |
* |
75 |
* Return value is a pointer to a timer struct. |
76 |
*/ |
77 |
struct timer *timer_add(double freq, void (*timer_tick)(struct timer *timer, |
78 |
void *extra), void *extra) |
79 |
{ |
80 |
struct timer *newtimer; |
81 |
|
82 |
CHECK_ALLOCATION(newtimer = malloc(sizeof(struct timer))); |
83 |
|
84 |
if (freq <= 0.00000001) |
85 |
freq = 0.00000001; |
86 |
|
87 |
newtimer->freq = freq; |
88 |
newtimer->timer_tick = timer_tick; |
89 |
newtimer->extra = extra; |
90 |
|
91 |
newtimer->interval = 1.0 / freq; |
92 |
newtimer->next_tick_at = timer_current_time + newtimer->interval; |
93 |
|
94 |
newtimer->next = first_timer; |
95 |
first_timer = newtimer; |
96 |
|
97 |
return newtimer; |
98 |
} |
99 |
|
100 |
|
101 |
/* |
102 |
* timer_remove(): |
103 |
* |
104 |
* Removes a virtual timer from the list of timers. |
105 |
*/ |
106 |
void timer_remove(struct timer *t) |
107 |
{ |
108 |
struct timer *prev = NULL, *cur = first_timer; |
109 |
|
110 |
while (cur != NULL && cur != t) { |
111 |
prev = cur; |
112 |
cur = cur->next; |
113 |
} |
114 |
|
115 |
if (cur == t) { |
116 |
if (prev == NULL) |
117 |
first_timer = cur->next; |
118 |
else |
119 |
prev->next = cur->next; |
120 |
free(cur); |
121 |
} else { |
122 |
fprintf(stderr, "attempt to remove timer %p which " |
123 |
"doesn't exist. aborting\n", t); |
124 |
exit(1); |
125 |
} |
126 |
} |
127 |
|
128 |
|
129 |
/* |
130 |
* timer_update_frequency(): |
131 |
* |
132 |
* Changes the frequency of an existing timer. |
133 |
*/ |
134 |
void timer_update_frequency(struct timer *t, double new_freq) |
135 |
{ |
136 |
if (t->freq == new_freq) |
137 |
return; |
138 |
|
139 |
t->freq = new_freq; |
140 |
|
141 |
if (new_freq <= 0.00000001) |
142 |
new_freq = 0.00000001; |
143 |
|
144 |
t->interval = 1.0 / new_freq; |
145 |
t->next_tick_at = timer_current_time + t->interval; |
146 |
} |
147 |
|
148 |
|
149 |
/* |
150 |
* timer_tick(): |
151 |
* |
152 |
* Timer tick handler. This is where the interesting stuff happens. |
153 |
*/ |
154 |
static void timer_tick(int signal_nr) |
155 |
{ |
156 |
struct timer *timer = first_timer; |
157 |
struct timeval tv; |
158 |
|
159 |
timer_current_time += timer_current_time_step; |
160 |
|
161 |
if ((--timer_countdown_to_next_gettimeofday) < 0) { |
162 |
gettimeofday(&tv, NULL); |
163 |
tv.tv_sec -= timer_start_tv.tv_sec; |
164 |
tv.tv_usec -= timer_start_tv.tv_usec; |
165 |
if (tv.tv_usec < 0) { |
166 |
tv.tv_usec += 1000000; |
167 |
tv.tv_sec --; |
168 |
} |
169 |
|
170 |
#ifdef TIMER_DEBUG |
171 |
/* For debugging/testing: */ |
172 |
{ |
173 |
double diff = tv.tv_usec * 0.000001 + tv.tv_sec |
174 |
- timer_current_time; |
175 |
printf("timer: lagging behind %f seconds\n", diff); |
176 |
} |
177 |
#endif |
178 |
|
179 |
timer_current_time = tv.tv_usec * 0.000001 + tv.tv_sec; |
180 |
|
181 |
timer_countdown_to_next_gettimeofday = timer_freq * |
182 |
SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH; |
183 |
} |
184 |
|
185 |
while (timer != NULL) { |
186 |
while (timer_current_time >= timer->next_tick_at) { |
187 |
timer->timer_tick(timer, timer->extra); |
188 |
timer->next_tick_at += timer->interval; |
189 |
} |
190 |
|
191 |
timer = timer->next; |
192 |
} |
193 |
|
194 |
#ifdef TEST |
195 |
printf("T"); fflush(stdout); |
196 |
#endif |
197 |
} |
198 |
|
199 |
|
200 |
/* |
201 |
* timer_start(): |
202 |
* |
203 |
* Set the interval timer to timer_freq Hz, and install the signal handler. |
204 |
*/ |
205 |
void timer_start(void) |
206 |
{ |
207 |
struct timer *timer = first_timer; |
208 |
struct itimerval val; |
209 |
struct sigaction saction; |
210 |
|
211 |
if (timer_is_running) |
212 |
return; |
213 |
|
214 |
timer_is_running = 1; |
215 |
|
216 |
gettimeofday(&timer_start_tv, NULL); |
217 |
timer_current_time = 0.0; |
218 |
|
219 |
/* Reset all timers: */ |
220 |
while (timer != NULL) { |
221 |
timer->next_tick_at = timer->interval; |
222 |
timer = timer->next; |
223 |
} |
224 |
val.it_interval.tv_sec = 0; |
225 |
val.it_interval.tv_usec = 1000000.0 / timer_freq; |
226 |
val.it_value.tv_sec = 0; |
227 |
val.it_value.tv_usec = 1000000.0 / timer_freq; |
228 |
|
229 |
memset(&saction, 0, sizeof(saction)); |
230 |
saction.sa_handler = timer_tick; |
231 |
|
232 |
sigaction(SIGALRM, &saction, NULL); |
233 |
|
234 |
setitimer(ITIMER_REAL, &val, NULL); |
235 |
} |
236 |
|
237 |
|
238 |
/* |
239 |
* timer_stop(): |
240 |
* |
241 |
* Deinstall the signal handler, and disable the interval timer. |
242 |
*/ |
243 |
void timer_stop(void) |
244 |
{ |
245 |
struct itimerval val; |
246 |
struct sigaction saction; |
247 |
|
248 |
if (!timer_is_running) |
249 |
return; |
250 |
|
251 |
timer_is_running = 0; |
252 |
|
253 |
val.it_interval.tv_sec = 0; |
254 |
val.it_interval.tv_usec = 0; |
255 |
val.it_value.tv_sec = 0; |
256 |
val.it_value.tv_usec = 0; |
257 |
|
258 |
setitimer(ITIMER_REAL, &val, NULL); |
259 |
|
260 |
memset(&saction, 0, sizeof(saction)); |
261 |
saction.sa_handler = NULL; |
262 |
|
263 |
sigaction(SIGALRM, &saction, NULL); |
264 |
} |
265 |
|
266 |
|
267 |
#ifdef TEST |
268 |
static void timer_tick_test(struct timer *t, void *extra) |
269 |
{ |
270 |
printf((char *) extra); fflush(stdout); |
271 |
} |
272 |
#endif |
273 |
|
274 |
|
275 |
/* |
276 |
* timer_init(): |
277 |
* |
278 |
* Initialize the timer framework. |
279 |
*/ |
280 |
void timer_init(void) |
281 |
{ |
282 |
first_timer = NULL; |
283 |
timer_current_time = 0.0; |
284 |
timer_is_running = 0; |
285 |
timer_countdown_to_next_gettimeofday = 0; |
286 |
|
287 |
timer_freq = TIMER_BASE_FREQUENCY; |
288 |
timer_current_time_step = 1.0 / timer_freq; |
289 |
|
290 |
#ifdef TEST |
291 |
timer_add(0.5, timer_tick_test, "X"); |
292 |
timer_add(10.0, timer_tick_test, "."); |
293 |
timer_add(200.0, timer_tick_test, " "); |
294 |
timer_start(); |
295 |
while (1) |
296 |
sleep(999999); |
297 |
#endif |
298 |
} |
299 |
|