]> Dogcows Code - chaz/tint2/blob - src/util/timer.c
8d22082f4d79b1321f73cb6288094887bf1094ad
[chaz/tint2] / src / util / timer.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 **************************************************************************/
17
18 #include <time.h>
19 #include <sys/time.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "timer.h"
24
25 GSList* timeout_list;
26 struct timeval next_timeout;
27 GHashTable* multi_timeouts;
28
29
30 // functions and structs for multi timeouts
31 typedef struct {
32 int current_count;
33 int count_to_expiration;
34 } multi_timeout;
35
36 typedef struct {
37 GSList* timeout_list;
38 timeout* parent_timeout;
39 } multi_timeout_handler;
40
41 struct _timeout {
42 int interval_msec;
43 struct timespec timeout_expires;
44 void (*_callback)(void*);
45 void* arg;
46 multi_timeout* multi_timeout;
47 };
48
49 void add_timeout_intern(int value_msec, int interval_msec, void(*_callback)(void*), void* arg, timeout* t);
50 gint compare_timeouts(gconstpointer t1, gconstpointer t2);
51 gint compare_timespecs(const struct timespec* t1, const struct timespec* t2);
52 int timespec_subtract(struct timespec* result, struct timespec* x, struct timespec* y);
53 struct timespec add_msec_to_timespec(struct timespec ts, int msec);
54
55
56 int align_with_existing_timeouts(timeout* t);
57 void create_multi_timeout(timeout* t1, timeout* t2);
58 void append_multi_timeout(timeout* t1, timeout* t2);
59 int calc_multi_timeout_interval(multi_timeout_handler* mth);
60 void update_multi_timeout_values(multi_timeout_handler* mth);
61 void callback_multi_timeout(void* mth);
62 void remove_from_multi_timeout(timeout* t);
63 void stop_multi_timeout(timeout* t);
64
65 void default_timeout()
66 {
67 timeout_list = 0;
68 multi_timeouts = 0;
69 }
70
71 void cleanup_timeout()
72 {
73 while (timeout_list) {
74 timeout* t = timeout_list->data;
75 if (t->multi_timeout)
76 stop_multi_timeout(t);
77 free(t);
78 timeout_list = g_slist_remove(timeout_list, t);
79 }
80 }
81
82 /** Implementation notes for timeouts: The timeouts are kept in a GSList sorted by their
83 * expiration time.
84 * That means that update_next_timeout() only have to consider the first timeout in the list,
85 * and callback_timeout_expired() only have to consider the timeouts as long as the expiration time
86 * is in the past to the current time.
87 * As time measurement we use clock_gettime(CLOCK_MONOTONIC) because this refers to a timer, which
88 * reference point lies somewhere in the past and cannot be changed, but just queried.
89 * If a single shot timer is installed it will be automatically deleted. I.e. the returned value
90 * of add_timeout will not be valid anymore. You do not need to call stop_timeout for these timeouts,
91 * however it's save to call it.
92 **/
93
94 timeout* add_timeout(int value_msec, int interval_msec, void (*_callback)(void*), void* arg)
95 {
96 timeout* t = malloc(sizeof(timeout));
97 t->multi_timeout = 0;
98 add_timeout_intern(value_msec, interval_msec, _callback, arg, t);
99 return t;
100 }
101
102
103 void change_timeout(timeout *t, int value_msec, int interval_msec, void(*_callback)(), void* arg)
104 {
105 if ( g_slist_find(timeout_list, t) == 0 && g_hash_table_lookup(multi_timeouts, t) == 0)
106 printf("programming error: timeout already deleted...");
107 else {
108 if (t->multi_timeout)
109 remove_from_multi_timeout((timeout*)t);
110 else
111 timeout_list = g_slist_remove(timeout_list, t);
112 add_timeout_intern(value_msec, interval_msec, _callback, arg, (timeout*)t);
113 }
114 }
115
116
117 void update_next_timeout()
118 {
119 if (timeout_list) {
120 timeout* t = timeout_list->data;
121 struct timespec cur_time;
122 struct timespec next_timeout2 = { .tv_sec=next_timeout.tv_sec, .tv_nsec=next_timeout.tv_usec*1000 };
123 clock_gettime(CLOCK_MONOTONIC, &cur_time);
124 if (timespec_subtract(&next_timeout2, &t->timeout_expires, &cur_time)) {
125 next_timeout.tv_sec = 0;
126 next_timeout.tv_usec = 0;
127 }
128 else {
129 next_timeout.tv_sec = next_timeout2.tv_sec;
130 next_timeout.tv_usec = next_timeout2.tv_nsec/1000;
131 }
132 }
133 else
134 next_timeout.tv_sec = -1;
135 }
136
137
138 void callback_timeout_expired()
139 {
140 struct timespec cur_time;
141 timeout* t;
142 while (timeout_list) {
143 clock_gettime(CLOCK_MONOTONIC, &cur_time);
144 t = timeout_list->data;
145 if (compare_timespecs(&t->timeout_expires, &cur_time) <= 0) {
146 // it's time for the callback function
147 t->_callback(t->arg);
148 if (g_slist_find(timeout_list, t)) {
149 // if _callback() calls stop_timeout(t) the timeout 't' was freed and is not in the timeout_list
150 timeout_list = g_slist_remove(timeout_list, t);
151 if (t->interval_msec > 0)
152 add_timeout_intern(t->interval_msec, t->interval_msec, t->_callback, t->arg, t);
153 else
154 free(t);
155 }
156 }
157 else
158 return;
159 }
160 }
161
162
163 void stop_timeout(timeout* t)
164 {
165 // if not in the list, it was deleted in callback_timeout_expired
166 if (g_slist_find(timeout_list, t) || g_hash_table_lookup(multi_timeouts, t)) {
167 if (t->multi_timeout)
168 remove_from_multi_timeout((timeout*)t);
169 timeout_list = g_slist_remove(timeout_list, t);
170 free((void*)t);
171 }
172 }
173
174
175 void add_timeout_intern(int value_msec, int interval_msec, void(*_callback)(), void* arg, timeout *t)
176 {
177 t->interval_msec = interval_msec;
178 t->_callback = _callback;
179 t->arg = arg;
180 struct timespec cur_time;
181 clock_gettime(CLOCK_MONOTONIC, &cur_time);
182 t->timeout_expires = add_msec_to_timespec(cur_time, value_msec);
183
184 int can_align = 0;
185 if (interval_msec > 0 && !t->multi_timeout)
186 can_align = align_with_existing_timeouts(t);
187 if (!can_align)
188 timeout_list = g_slist_insert_sorted(timeout_list, t, compare_timeouts);
189 }
190
191
192 gint compare_timeouts(gconstpointer t1, gconstpointer t2)
193 {
194 return compare_timespecs(&((timeout*)t1)->timeout_expires,
195 &((timeout*)t2)->timeout_expires);
196 }
197
198
199 gint compare_timespecs(const struct timespec* t1, const struct timespec* t2)
200 {
201 if (t1->tv_sec < t2->tv_sec)
202 return -1;
203 else if (t1->tv_sec == t2->tv_sec) {
204 if (t1->tv_nsec < t2->tv_nsec)
205 return -1;
206 else if (t1->tv_nsec == t2->tv_nsec)
207 return 0;
208 else
209 return 1;
210 }
211 else
212 return 1;
213 }
214
215 int timespec_subtract(struct timespec* result, struct timespec* x, struct timespec* y)
216 {
217 /* Perform the carry for the later subtraction by updating y. */
218 if (x->tv_nsec < y->tv_nsec) {
219 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
220 y->tv_nsec -= 1000000000 * nsec;
221 y->tv_sec += nsec;
222 }
223 if (x->tv_nsec - y->tv_nsec > 1000000000) {
224 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
225 y->tv_nsec += 1000000000 * nsec;
226 y->tv_sec -= nsec;
227 }
228
229 /* Compute the time remaining to wait. tv_nsec is certainly positive. */
230 result->tv_sec = x->tv_sec - y->tv_sec;
231 result->tv_nsec = x->tv_nsec - y->tv_nsec;
232
233 /* Return 1 if result is negative. */
234 return x->tv_sec < y->tv_sec;
235 }
236
237
238 struct timespec add_msec_to_timespec(struct timespec ts, int msec)
239 {
240 ts.tv_sec += msec / 1000;
241 ts.tv_nsec += (msec % 1000)*1000000;
242 if (ts.tv_nsec >= 1000000000) { // 10^9
243 ts.tv_sec++;
244 ts.tv_nsec -= 1000000000;
245 }
246 return ts;
247 }
248
249
250 int align_with_existing_timeouts(timeout *t)
251 {
252 GSList* it = timeout_list;
253 while (it) {
254 timeout* t2 = it->data;
255 if (t2->interval_msec > 0) {
256 if (t->interval_msec % t2->interval_msec == 0 || t2->interval_msec % t->interval_msec == 0) {
257 if (multi_timeouts == 0)
258 multi_timeouts = g_hash_table_new(0, 0);
259 if (!t->multi_timeout && !t2->multi_timeout)
260 // both timeouts can be aligned, but there is no multi timeout for them
261 create_multi_timeout(t, t2);
262 else
263 // there is already a multi timeout, so we append the new timeout to the multi timeout
264 append_multi_timeout(t, t2);
265 return 1;
266 }
267 }
268 it = it->next;
269 }
270 return 0;
271 }
272
273
274 int calc_multi_timeout_interval(multi_timeout_handler* mth)
275 {
276 GSList* it = mth->timeout_list;
277 timeout* t = it->data;
278 int min_interval = t->interval_msec;
279 it = it->next;
280 while (it) {
281 t = it->data;
282 if (t->interval_msec < min_interval)
283 min_interval = t->interval_msec;
284 it = it->next;
285 }
286 return min_interval;
287 }
288
289
290 void create_multi_timeout(timeout* t1, timeout* t2)
291 {
292 multi_timeout* mt1 = malloc(sizeof(multi_timeout));
293 multi_timeout* mt2 = malloc(sizeof(multi_timeout));
294 multi_timeout_handler* mth = malloc(sizeof(multi_timeout_handler));
295 timeout* real_timeout = malloc(sizeof(timeout));
296
297 mth->timeout_list = 0;
298 mth->timeout_list = g_slist_prepend(mth->timeout_list, t1);
299 mth->timeout_list = g_slist_prepend(mth->timeout_list, t2);
300 mth->parent_timeout = real_timeout;
301
302 g_hash_table_insert(multi_timeouts, t1, mth);
303 g_hash_table_insert(multi_timeouts, t2, mth);
304 g_hash_table_insert(multi_timeouts, real_timeout, mth);
305
306 t1->multi_timeout = mt1;
307 t2->multi_timeout = mt2;
308 // set real_timeout->multi_timeout to something, such that we see in add_timeout_intern that
309 // it is already a multi_timeout (we never use it, except of checking for 0 ptr)
310 real_timeout->multi_timeout = (void*)real_timeout;
311
312 timeout_list = g_slist_remove(timeout_list, t1);
313 timeout_list = g_slist_remove(timeout_list, t2);
314
315 update_multi_timeout_values(mth);
316 }
317
318
319 void append_multi_timeout(timeout* t1, timeout* t2)
320 {
321 if (t2->multi_timeout) {
322 // swap t1 and t2 such that t1 is the multi timeout
323 timeout* tmp = t2;
324 t2 = t1;
325 t1 = tmp;
326 }
327
328 multi_timeout* mt = malloc(sizeof(multi_timeout));
329 multi_timeout_handler* mth = g_hash_table_lookup(multi_timeouts, t1);
330
331 mth->timeout_list = g_slist_prepend(mth->timeout_list, t2);
332 g_hash_table_insert(multi_timeouts, t2, mth);
333
334 t2->multi_timeout = mt;
335
336 update_multi_timeout_values(mth);
337 }
338
339
340 void update_multi_timeout_values(multi_timeout_handler* mth)
341 {
342 int interval = calc_multi_timeout_interval(mth);
343 int next_timeout_msec = interval;
344
345 struct timespec cur_time;
346 clock_gettime(CLOCK_MONOTONIC, &cur_time);
347
348 GSList* it = mth->timeout_list;
349 struct timespec diff_time;
350 while (it) {
351 timeout* t = it->data;
352 t->multi_timeout->count_to_expiration = t->interval_msec / interval;
353 timespec_subtract(&diff_time, &t->timeout_expires, &cur_time);
354 int msec_to_expiration = diff_time.tv_sec*1000 + diff_time.tv_nsec/1000000;
355 int count_left = msec_to_expiration / interval + (msec_to_expiration%interval != 0);
356 t->multi_timeout->current_count = t->multi_timeout->count_to_expiration - count_left;
357 if (msec_to_expiration < next_timeout_msec)
358 next_timeout_msec = msec_to_expiration;
359 it = it->next;
360 }
361
362 mth->parent_timeout->interval_msec = interval;
363 timeout_list = g_slist_remove(timeout_list, mth->parent_timeout);
364 add_timeout_intern(next_timeout_msec, interval, callback_multi_timeout, mth, mth->parent_timeout);
365 }
366
367
368 void callback_multi_timeout(void* arg)
369 {
370 multi_timeout_handler* mth = arg;
371 struct timespec cur_time;
372 clock_gettime(CLOCK_MONOTONIC, &cur_time);
373 GSList* it = mth->timeout_list;
374 while (it) {
375 timeout* t = it->data;
376 if (++t->multi_timeout->current_count >= t->multi_timeout->count_to_expiration) {
377 t->_callback(t->arg);
378 t->multi_timeout->current_count = 0;
379 t->timeout_expires = add_msec_to_timespec(cur_time, t->interval_msec);
380 }
381 it = it->next;
382 }
383 }
384
385
386 void remove_from_multi_timeout(timeout* t)
387 {
388 multi_timeout_handler* mth = g_hash_table_lookup(multi_timeouts, t);
389 g_hash_table_remove(multi_timeouts, t);
390
391 mth->timeout_list = g_slist_remove(mth->timeout_list, t);
392 free(t->multi_timeout);
393 t->multi_timeout = 0;
394
395 if (g_slist_length(mth->timeout_list) == 1) {
396 timeout* last_timeout = mth->timeout_list->data;
397 free(last_timeout->multi_timeout);
398 last_timeout->multi_timeout = 0;
399 g_hash_table_remove(multi_timeouts, last_timeout);
400 g_hash_table_remove(multi_timeouts, mth->parent_timeout);
401 mth->parent_timeout->multi_timeout = 0;
402 stop_timeout(mth->parent_timeout);
403 free(mth);
404
405 struct timespec cur_time, diff_time;
406 clock_gettime(CLOCK_MONOTONIC, &cur_time);
407 timespec_subtract(&diff_time, &t->timeout_expires, &cur_time);
408 int msec_to_expiration = diff_time.tv_sec*1000 + diff_time.tv_nsec/1000000;
409 add_timeout_intern(msec_to_expiration, last_timeout->interval_msec, last_timeout->_callback, last_timeout->arg, last_timeout);
410 }
411 else
412 update_multi_timeout_values(mth);
413 }
414
415
416 void stop_multi_timeout(timeout* t)
417 {
418 multi_timeout_handler* mth = g_hash_table_lookup(multi_timeouts, t);
419 g_hash_table_remove(multi_timeouts, mth->parent_timeout);
420 while (mth->timeout_list) {
421 timeout* t1 = mth->timeout_list->data;
422 mth->timeout_list = g_slist_remove(mth->timeout_list, t1);
423 g_hash_table_remove(multi_timeouts, t1);
424 free(t1->multi_timeout);
425 free(t1);
426 }
427 free(mth);
428 }
This page took 0.048628 seconds and 3 git commands to generate.