1 |
dpavlin |
32 |
/* |
2 |
dpavlin |
34 |
* Copyright (C) 2006-2007 Anders Gavare. All rights reserved. |
3 |
dpavlin |
32 |
* |
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 |
dpavlin |
44 |
* $Id: timer.c,v 1.11 2007/06/18 04:23:19 debug Exp $ |
29 |
dpavlin |
32 |
* |
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 |
dpavlin |
42 |
#include "misc.h" |
41 |
dpavlin |
32 |
#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 |
dpavlin |
42 |
#define SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH 1.65 |
68 |
dpavlin |
32 |
|
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 |
dpavlin |
42 |
struct timer *newtimer; |
81 |
dpavlin |
32 |
|
82 |
dpavlin |
42 |
CHECK_ALLOCATION(newtimer = malloc(sizeof(struct timer))); |
83 |
|
|
|
84 |
dpavlin |
32 |
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 |
dpavlin |
42 |
#ifdef TIMER_DEBUG |
171 |
dpavlin |
32 |
/* 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 |
dpavlin |
44 |
/* Get exponentially closer to the real time, instead of |
180 |
|
|
just changing to it directly: */ |
181 |
|
|
timer_current_time = ( (tv.tv_usec * 0.000001 + tv.tv_sec) + |
182 |
|
|
timer_current_time ) / 2; |
183 |
dpavlin |
32 |
|
184 |
|
|
timer_countdown_to_next_gettimeofday = timer_freq * |
185 |
|
|
SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
while (timer != NULL) { |
189 |
|
|
while (timer_current_time >= timer->next_tick_at) { |
190 |
|
|
timer->timer_tick(timer, timer->extra); |
191 |
|
|
timer->next_tick_at += timer->interval; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
timer = timer->next; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
#ifdef TEST |
198 |
|
|
printf("T"); fflush(stdout); |
199 |
|
|
#endif |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
|
203 |
|
|
/* |
204 |
|
|
* timer_start(): |
205 |
|
|
* |
206 |
|
|
* Set the interval timer to timer_freq Hz, and install the signal handler. |
207 |
|
|
*/ |
208 |
|
|
void timer_start(void) |
209 |
|
|
{ |
210 |
|
|
struct timer *timer = first_timer; |
211 |
|
|
struct itimerval val; |
212 |
|
|
struct sigaction saction; |
213 |
|
|
|
214 |
|
|
if (timer_is_running) |
215 |
|
|
return; |
216 |
|
|
|
217 |
|
|
timer_is_running = 1; |
218 |
|
|
|
219 |
|
|
gettimeofday(&timer_start_tv, NULL); |
220 |
|
|
timer_current_time = 0.0; |
221 |
|
|
|
222 |
|
|
/* Reset all timers: */ |
223 |
|
|
while (timer != NULL) { |
224 |
|
|
timer->next_tick_at = timer->interval; |
225 |
|
|
timer = timer->next; |
226 |
|
|
} |
227 |
|
|
val.it_interval.tv_sec = 0; |
228 |
|
|
val.it_interval.tv_usec = 1000000.0 / timer_freq; |
229 |
|
|
val.it_value.tv_sec = 0; |
230 |
|
|
val.it_value.tv_usec = 1000000.0 / timer_freq; |
231 |
|
|
|
232 |
|
|
memset(&saction, 0, sizeof(saction)); |
233 |
|
|
saction.sa_handler = timer_tick; |
234 |
|
|
|
235 |
|
|
sigaction(SIGALRM, &saction, NULL); |
236 |
|
|
|
237 |
|
|
setitimer(ITIMER_REAL, &val, NULL); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
|
241 |
|
|
/* |
242 |
|
|
* timer_stop(): |
243 |
|
|
* |
244 |
|
|
* Deinstall the signal handler, and disable the interval timer. |
245 |
|
|
*/ |
246 |
|
|
void timer_stop(void) |
247 |
|
|
{ |
248 |
|
|
struct itimerval val; |
249 |
|
|
struct sigaction saction; |
250 |
|
|
|
251 |
|
|
if (!timer_is_running) |
252 |
|
|
return; |
253 |
|
|
|
254 |
|
|
timer_is_running = 0; |
255 |
|
|
|
256 |
|
|
val.it_interval.tv_sec = 0; |
257 |
|
|
val.it_interval.tv_usec = 0; |
258 |
|
|
val.it_value.tv_sec = 0; |
259 |
|
|
val.it_value.tv_usec = 0; |
260 |
|
|
|
261 |
|
|
setitimer(ITIMER_REAL, &val, NULL); |
262 |
|
|
|
263 |
|
|
memset(&saction, 0, sizeof(saction)); |
264 |
|
|
saction.sa_handler = NULL; |
265 |
|
|
|
266 |
|
|
sigaction(SIGALRM, &saction, NULL); |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
|
270 |
|
|
#ifdef TEST |
271 |
|
|
static void timer_tick_test(struct timer *t, void *extra) |
272 |
|
|
{ |
273 |
|
|
printf((char *) extra); fflush(stdout); |
274 |
|
|
} |
275 |
|
|
#endif |
276 |
|
|
|
277 |
|
|
|
278 |
|
|
/* |
279 |
|
|
* timer_init(): |
280 |
|
|
* |
281 |
|
|
* Initialize the timer framework. |
282 |
|
|
*/ |
283 |
|
|
void timer_init(void) |
284 |
|
|
{ |
285 |
|
|
first_timer = NULL; |
286 |
|
|
timer_current_time = 0.0; |
287 |
|
|
timer_is_running = 0; |
288 |
|
|
timer_countdown_to_next_gettimeofday = 0; |
289 |
|
|
|
290 |
dpavlin |
42 |
timer_freq = TIMER_BASE_FREQUENCY; |
291 |
dpavlin |
32 |
timer_current_time_step = 1.0 / timer_freq; |
292 |
|
|
|
293 |
|
|
#ifdef TEST |
294 |
|
|
timer_add(0.5, timer_tick_test, "X"); |
295 |
|
|
timer_add(10.0, timer_tick_test, "."); |
296 |
|
|
timer_add(200.0, timer_tick_test, " "); |
297 |
|
|
timer_start(); |
298 |
|
|
while (1) |
299 |
|
|
sleep(999999); |
300 |
|
|
#endif |
301 |
|
|
} |
302 |
|
|
|