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