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 **************************************************************************/
26 struct timeval next_timeout
;
27 GHashTable
* multi_timeouts
;
30 // functions and structs for multi timeouts
33 int count_to_expiration
;
38 timeout
* parent_timeout
;
39 } multi_timeout_handler
;
43 struct timespec timeout_expires
;
44 void (*_callback
)(void*);
46 multi_timeout
* multi_timeout
;
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
);
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
);
65 void default_timeout()
71 void cleanup_timeout()
73 while (timeout_list
) {
74 timeout
* t
= timeout_list
->data
;
76 stop_multi_timeout(t
);
78 timeout_list
= g_slist_remove(timeout_list
, t
);
82 /** Implementation notes for timeouts: The timeouts are kept in a GSList sorted by their
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.
94 timeout
* add_timeout(int value_msec
, int interval_msec
, void (*_callback
)(void*), void* arg
)
96 timeout
* t
= malloc(sizeof(timeout
));
98 add_timeout_intern(value_msec
, interval_msec
, _callback
, arg
, t
);
103 void change_timeout(timeout
*t
, int value_msec
, int interval_msec
, void(*_callback
)(), void* arg
)
105 if ( g_slist_find(timeout_list
, t
) == 0 && g_hash_table_lookup(multi_timeouts
, t
) == 0)
106 printf("programming error: timeout already deleted...");
108 if (t
->multi_timeout
)
109 remove_from_multi_timeout((timeout
*)t
);
111 timeout_list
= g_slist_remove(timeout_list
, t
);
112 add_timeout_intern(value_msec
, interval_msec
, _callback
, arg
, (timeout
*)t
);
117 void update_next_timeout()
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;
129 next_timeout
.tv_sec
= next_timeout2
.tv_sec
;
130 next_timeout
.tv_usec
= next_timeout2
.tv_nsec
/1000;
134 next_timeout
.tv_sec
= -1;
138 void callback_timeout_expired()
140 struct timespec cur_time
;
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
);
163 void stop_timeout(timeout
* t
)
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
);
175 void add_timeout_intern(int value_msec
, int interval_msec
, void(*_callback
)(), void* arg
, timeout
*t
)
177 t
->interval_msec
= interval_msec
;
178 t
->_callback
= _callback
;
180 struct timespec cur_time
;
181 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
182 t
->timeout_expires
= add_msec_to_timespec(cur_time
, value_msec
);
185 if (interval_msec
> 0 && !t
->multi_timeout
)
186 can_align
= align_with_existing_timeouts(t
);
188 timeout_list
= g_slist_insert_sorted(timeout_list
, t
, compare_timeouts
);
192 gint
compare_timeouts(gconstpointer t1
, gconstpointer t2
)
194 return compare_timespecs(&((timeout
*)t1
)->timeout_expires
,
195 &((timeout
*)t2
)->timeout_expires
);
199 gint
compare_timespecs(const struct timespec
* t1
, const struct timespec
* t2
)
201 if (t1
->tv_sec
< t2
->tv_sec
)
203 else if (t1
->tv_sec
== t2
->tv_sec
) {
204 if (t1
->tv_nsec
< t2
->tv_nsec
)
206 else if (t1
->tv_nsec
== t2
->tv_nsec
)
215 int timespec_subtract(struct timespec
* result
, struct timespec
* x
, struct timespec
* y
)
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
;
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
;
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
;
233 /* Return 1 if result is negative. */
234 return x
->tv_sec
< y
->tv_sec
;
238 struct timespec
add_msec_to_timespec(struct timespec ts
, int msec
)
240 ts
.tv_sec
+= msec
/ 1000;
241 ts
.tv_nsec
+= (msec
% 1000)*1000000;
242 if (ts
.tv_nsec
>= 1000000000) { // 10^9
244 ts
.tv_nsec
-= 1000000000;
250 int align_with_existing_timeouts(timeout
*t
)
252 GSList
* it
= timeout_list
;
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
);
263 // there is already a multi timeout, so we append the new timeout to the multi timeout
264 append_multi_timeout(t
, t2
);
274 int calc_multi_timeout_interval(multi_timeout_handler
* mth
)
276 GSList
* it
= mth
->timeout_list
;
277 timeout
* t
= it
->data
;
278 int min_interval
= t
->interval_msec
;
282 if (t
->interval_msec
< min_interval
)
283 min_interval
= t
->interval_msec
;
290 void create_multi_timeout(timeout
* t1
, timeout
* t2
)
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
));
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
;
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
);
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
;
312 timeout_list
= g_slist_remove(timeout_list
, t1
);
313 timeout_list
= g_slist_remove(timeout_list
, t2
);
315 update_multi_timeout_values(mth
);
319 void append_multi_timeout(timeout
* t1
, timeout
* t2
)
321 if (t2
->multi_timeout
) {
322 // swap t1 and t2 such that t1 is the multi timeout
328 multi_timeout
* mt
= malloc(sizeof(multi_timeout
));
329 multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t1
);
331 mth
->timeout_list
= g_slist_prepend(mth
->timeout_list
, t2
);
332 g_hash_table_insert(multi_timeouts
, t2
, mth
);
334 t2
->multi_timeout
= mt
;
336 update_multi_timeout_values(mth
);
340 void update_multi_timeout_values(multi_timeout_handler
* mth
)
342 int interval
= calc_multi_timeout_interval(mth
);
343 int next_timeout_msec
= interval
;
345 struct timespec cur_time
;
346 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
348 GSList
* it
= mth
->timeout_list
;
349 struct timespec diff_time
;
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
;
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
);
368 void callback_multi_timeout(void* arg
)
370 multi_timeout_handler
* mth
= arg
;
371 struct timespec cur_time
;
372 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
373 GSList
* it
= mth
->timeout_list
;
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
);
386 void remove_from_multi_timeout(timeout
* t
)
388 multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t
);
389 g_hash_table_remove(multi_timeouts
, t
);
391 mth
->timeout_list
= g_slist_remove(mth
->timeout_list
, t
);
392 free(t
->multi_timeout
);
393 t
->multi_timeout
= 0;
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
);
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
);
412 update_multi_timeout_values(mth
);
416 void stop_multi_timeout(timeout
* t
)
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
);