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