1 /**************************************************************************
3 * Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
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.
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 **************************************************************************/
24 GSList
* timeout_list
= 0;
25 struct timeval next_timeout
;
28 // functions and structs for multi timeouts
31 int count_to_expiration
;
36 timeout
* parent_timeout
;
37 } multi_timeout_handler
;
41 struct timespec timeout_expires
;
42 void (*_callback
)(void*);
44 multi_timeout
* multi_timeout
;
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
);
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
);
63 GHashTable
* multi_timeouts
= 0;
65 /** Implementation notes for timeouts: The timeouts are kept in a GSList sorted by their
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.
77 timeout
* add_timeout(int value_msec
, int interval_msec
, void (*_callback
)(void*), void* arg
)
79 timeout
* t
= malloc(sizeof(timeout
));
81 add_timeout_intern(value_msec
, interval_msec
, _callback
, arg
, t
);
86 void change_timeout(timeout
*t
, int value_msec
, int interval_msec
, void(*_callback
)(), void* arg
)
88 if ( g_slist_find(timeout_list
, t
) == 0 && g_hash_table_lookup(multi_timeouts
, t
) == 0)
89 printf("programming error: timeout already deleted...");
92 remove_from_multi_timeout((timeout
*)t
);
94 timeout_list
= g_slist_remove(timeout_list
, t
);
95 add_timeout_intern(value_msec
, interval_msec
, _callback
, arg
, (timeout
*)t
);
100 void update_next_timeout()
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;
112 next_timeout
.tv_sec
= next_timeout2
.tv_sec
;
113 next_timeout
.tv_usec
= next_timeout2
.tv_nsec
/1000;
117 next_timeout
.tv_sec
= -1;
121 void callback_timeout_expired()
123 struct timespec cur_time
;
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
);
146 void stop_timeout(timeout
* t
)
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
);
158 void stop_all_timeouts()
160 while (timeout_list
) {
161 timeout
* t
= timeout_list
->data
;
162 if (t
->multi_timeout
)
163 stop_multi_timeout(t
);
165 timeout_list
= g_slist_remove(timeout_list
, t
);
170 void add_timeout_intern(int value_msec
, int interval_msec
, void(*_callback
)(), void* arg
, timeout
*t
)
172 t
->interval_msec
= interval_msec
;
173 t
->_callback
= _callback
;
175 struct timespec cur_time
;
176 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
177 t
->timeout_expires
= add_msec_to_timespec(cur_time
, value_msec
);
180 if (interval_msec
> 0 && !t
->multi_timeout
)
181 can_align
= align_with_existing_timeouts(t
);
183 timeout_list
= g_slist_insert_sorted(timeout_list
, t
, compare_timeouts
);
187 gint
compare_timeouts(gconstpointer t1
, gconstpointer t2
)
189 return compare_timespecs(&((timeout
*)t1
)->timeout_expires
,
190 &((timeout
*)t2
)->timeout_expires
);
194 gint
compare_timespecs(const struct timespec
* t1
, const struct timespec
* t2
)
196 if (t1
->tv_sec
< t2
->tv_sec
)
198 else if (t1
->tv_sec
== t2
->tv_sec
) {
199 if (t1
->tv_nsec
< t2
->tv_nsec
)
201 else if (t1
->tv_nsec
== t2
->tv_nsec
)
210 int timespec_subtract(struct timespec
* result
, struct timespec
* x
, struct timespec
* y
)
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
;
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
;
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
;
228 /* Return 1 if result is negative. */
229 return x
->tv_sec
< y
->tv_sec
;
233 struct timespec
add_msec_to_timespec(struct timespec ts
, int msec
)
235 ts
.tv_sec
+= msec
/ 1000;
236 ts
.tv_nsec
+= (msec
% 1000)*1000000;
237 if (ts
.tv_nsec
>= 1000000000) { // 10^9
239 ts
.tv_nsec
-= 1000000000;
245 int align_with_existing_timeouts(timeout
*t
)
247 GSList
* it
= timeout_list
;
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
);
258 // there is already a multi timeout, so we append the new timeout to the multi timeout
259 append_multi_timeout(t
, t2
);
269 int calc_multi_timeout_interval(multi_timeout_handler
* mth
)
271 GSList
* it
= mth
->timeout_list
;
272 timeout
* t
= it
->data
;
273 int min_interval
= t
->interval_msec
;
277 if (t
->interval_msec
< min_interval
)
278 min_interval
= t
->interval_msec
;
285 void create_multi_timeout(timeout
* t1
, timeout
* t2
)
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
));
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
;
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
);
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
;
307 timeout_list
= g_slist_remove(timeout_list
, t1
);
308 timeout_list
= g_slist_remove(timeout_list
, t2
);
310 update_multi_timeout_values(mth
);
314 void append_multi_timeout(timeout
* t1
, timeout
* t2
)
316 if (t2
->multi_timeout
) {
317 // swap t1 and t2 such that t1 is the multi timeout
323 multi_timeout
* mt
= malloc(sizeof(multi_timeout
));
324 multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t1
);
326 mth
->timeout_list
= g_slist_prepend(mth
->timeout_list
, t2
);
327 g_hash_table_insert(multi_timeouts
, t2
, mth
);
329 t2
->multi_timeout
= mt
;
331 update_multi_timeout_values(mth
);
335 void update_multi_timeout_values(multi_timeout_handler
* mth
)
337 int interval
= calc_multi_timeout_interval(mth
);
338 int next_timeout_msec
= interval
;
340 struct timespec cur_time
;
341 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
343 GSList
* it
= mth
->timeout_list
;
344 struct timespec diff_time
;
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
;
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
);
363 void callback_multi_timeout(void* arg
)
365 multi_timeout_handler
* mth
= arg
;
366 struct timespec cur_time
;
367 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
368 GSList
* it
= mth
->timeout_list
;
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
);
381 void remove_from_multi_timeout(timeout
* t
)
383 multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t
);
384 g_hash_table_remove(multi_timeouts
, t
);
386 mth
->timeout_list
= g_slist_remove(mth
->timeout_list
, t
);
387 free(t
->multi_timeout
);
388 t
->multi_timeout
= 0;
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
);
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
);
407 update_multi_timeout_values(mth
);
411 void stop_multi_timeout(timeout
* t
)
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
);