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