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
;
27 void add_timeout_intern(int value_msec
, int interval_msec
, void(*_callback
)(void*), void* arg
, struct timeout
* t
);
28 gint
compare_timeouts(gconstpointer t1
, gconstpointer t2
);
29 gint
compare_timespecs(const struct timespec
* t1
, const struct timespec
* t2
);
30 int timespec_subtract(struct timespec
* result
, struct timespec
* x
, struct timespec
* y
);
31 struct timespec
add_msec_to_timespec(struct timespec ts
, int msec
);
33 // functions and structs for multi timeouts
34 struct multi_timeout
{
36 int count_to_expiration
;
39 struct multi_timeout_handler
{
41 struct timeout
* parent_timeout
;
44 int align_with_existing_timeouts(struct timeout
* t
);
45 void create_multi_timeout(struct timeout
* t1
, struct timeout
* t2
);
46 void append_multi_timeout(struct timeout
* t1
, struct timeout
* t2
);
47 int calc_multi_timeout_interval(struct multi_timeout_handler
* mth
);
48 void update_multi_timeout_values(struct multi_timeout_handler
* mth
);
49 void callback_multi_timeout(void* mth
);
50 void remove_from_multi_timeout(struct timeout
* t
);
51 void stop_multi_timeout(struct timeout
* t
);
53 GHashTable
* multi_timeouts
= 0;
55 /** Implementation notes for timeouts: The timeouts are kept in a GSList sorted by their
57 * That means that update_next_timeout() only have to consider the first timeout in the list,
58 * and callback_timeout_expired() only have to consider the timeouts as long as the expiration time
59 * is in the past to the current time.
60 * As time measurement we use clock_gettime(CLOCK_MONOTONIC) because this refers to a timer, which
61 * reference point lies somewhere in the past and cannot be changed, but just queried.
62 * If a single shot timer is installed it will be automatically deleted. I.e. the returned value
63 * of add_timeout will not be valid anymore. You do not need to call stop_timeout for these timeouts,
64 * however it's save to call it.
67 const struct timeout
* add_timeout(int value_msec
, int interval_msec
, void (*_callback
)(void*), void* arg
)
69 struct timeout
* t
= malloc(sizeof(struct timeout
));
71 add_timeout_intern(value_msec
, interval_msec
, _callback
, arg
, t
);
76 void change_timeout(const struct timeout
*t
, int value_msec
, int interval_msec
, void(*_callback
)(), void* arg
)
78 if ( g_slist_find(timeout_list
, t
) == 0 && g_hash_table_lookup(multi_timeouts
, t
) == 0)
79 printf("programming error: timeout already deleted...");
82 remove_from_multi_timeout((struct timeout
*)t
);
84 timeout_list
= g_slist_remove(timeout_list
, t
);
85 add_timeout_intern(value_msec
, interval_msec
, _callback
, arg
, (struct timeout
*)t
);
90 void update_next_timeout()
93 struct timeout
* t
= timeout_list
->data
;
94 struct timespec cur_time
;
95 struct timespec next_timeout2
= { .tv_sec
=next_timeout
.tv_sec
, .tv_nsec
=next_timeout
.tv_usec
*1000 };
96 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
97 if (timespec_subtract(&next_timeout2
, &t
->timeout_expires
, &cur_time
)) {
98 next_timeout
.tv_sec
= 0;
99 next_timeout
.tv_usec
= 0;
102 next_timeout
.tv_sec
= next_timeout2
.tv_sec
;
103 next_timeout
.tv_usec
= next_timeout2
.tv_nsec
/1000;
107 next_timeout
.tv_sec
= -1;
111 void callback_timeout_expired()
113 struct timespec cur_time
;
115 while (timeout_list
) {
116 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
117 t
= timeout_list
->data
;
118 if (compare_timespecs(&t
->timeout_expires
, &cur_time
) <= 0) {
119 // it's time for the callback function
120 t
->_callback(t
->arg
);
121 if (g_slist_find(timeout_list
, t
)) {
122 // if _callback() calls stop_timeout(t) the timeout 't' was freed and is not in the timeout_list
123 timeout_list
= g_slist_remove(timeout_list
, t
);
124 if (t
->interval_msec
> 0)
125 add_timeout_intern(t
->interval_msec
, t
->interval_msec
, t
->_callback
, t
->arg
, t
);
136 void stop_timeout(const struct timeout
* t
)
138 // if not in the list, it was deleted in callback_timeout_expired
139 if (g_slist_find(timeout_list
, t
) || g_hash_table_lookup(multi_timeouts
, t
)) {
140 if (t
->multi_timeout
)
141 remove_from_multi_timeout((struct timeout
*)t
);
142 timeout_list
= g_slist_remove(timeout_list
, t
);
148 void stop_all_timeouts()
150 while (timeout_list
) {
151 struct timeout
* t
= timeout_list
->data
;
152 if (t
->multi_timeout
)
153 stop_multi_timeout(t
);
155 timeout_list
= g_slist_remove(timeout_list
, t
);
160 void add_timeout_intern(int value_msec
, int interval_msec
, void(*_callback
)(), void* arg
, struct timeout
*t
)
162 t
->interval_msec
= interval_msec
;
163 t
->_callback
= _callback
;
165 struct timespec cur_time
;
166 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
167 t
->timeout_expires
= add_msec_to_timespec(cur_time
, value_msec
);
170 if (interval_msec
> 0 && !t
->multi_timeout
)
171 can_align
= align_with_existing_timeouts(t
);
173 timeout_list
= g_slist_insert_sorted(timeout_list
, t
, compare_timeouts
);
177 gint
compare_timeouts(gconstpointer t1
, gconstpointer t2
)
179 return compare_timespecs(&((const struct timeout
*)t1
)->timeout_expires
,
180 &((const struct timeout
*)t2
)->timeout_expires
);
184 gint
compare_timespecs(const struct timespec
* t1
, const struct timespec
* t2
)
186 if (t1
->tv_sec
< t2
->tv_sec
)
188 else if (t1
->tv_sec
== t2
->tv_sec
) {
189 if (t1
->tv_nsec
< t2
->tv_nsec
)
191 else if (t1
->tv_nsec
== t2
->tv_nsec
)
200 int timespec_subtract(struct timespec
* result
, struct timespec
* x
, struct timespec
* y
)
202 /* Perform the carry for the later subtraction by updating y. */
203 if (x
->tv_nsec
< y
->tv_nsec
) {
204 int nsec
= (y
->tv_nsec
- x
->tv_nsec
) / 1000000000 + 1;
205 y
->tv_nsec
-= 1000000000 * nsec
;
208 if (x
->tv_nsec
- y
->tv_nsec
> 1000000000) {
209 int nsec
= (x
->tv_nsec
- y
->tv_nsec
) / 1000000000;
210 y
->tv_nsec
+= 1000000000 * nsec
;
214 /* Compute the time remaining to wait. tv_nsec is certainly positive. */
215 result
->tv_sec
= x
->tv_sec
- y
->tv_sec
;
216 result
->tv_nsec
= x
->tv_nsec
- y
->tv_nsec
;
218 /* Return 1 if result is negative. */
219 return x
->tv_sec
< y
->tv_sec
;
223 struct timespec
add_msec_to_timespec(struct timespec ts
, int msec
)
225 ts
.tv_sec
+= msec
/ 1000;
226 ts
.tv_nsec
+= (msec
% 1000)*1000000;
227 if (ts
.tv_nsec
>= 1000000000) { // 10^9
229 ts
.tv_nsec
-= 1000000000;
235 int align_with_existing_timeouts(struct timeout
*t
)
237 GSList
* it
= timeout_list
;
239 struct timeout
* t2
= it
->data
;
240 if (t2
->interval_msec
> 0) {
241 if (t
->interval_msec
% t2
->interval_msec
== 0 || t2
->interval_msec
% t
->interval_msec
== 0) {
242 if (multi_timeouts
== 0)
243 multi_timeouts
= g_hash_table_new(0, 0);
244 if (!t
->multi_timeout
&& !t2
->multi_timeout
)
245 // both timeouts can be aligned, but there is no multi timeout for them
246 create_multi_timeout(t
, t2
);
248 // there is already a multi timeout, so we append the new timeout to the multi timeout
249 append_multi_timeout(t
, t2
);
259 int calc_multi_timeout_interval(struct multi_timeout_handler
* mth
)
261 GSList
* it
= mth
->timeout_list
;
262 struct timeout
* t
= it
->data
;
263 int min_interval
= t
->interval_msec
;
267 if (t
->interval_msec
< min_interval
)
268 min_interval
= t
->interval_msec
;
275 void create_multi_timeout(struct timeout
* t1
, struct timeout
* t2
)
277 struct multi_timeout
* mt1
= malloc(sizeof(struct multi_timeout
));
278 struct multi_timeout
* mt2
= malloc(sizeof(struct multi_timeout
));
279 struct multi_timeout_handler
* mth
= malloc(sizeof(struct multi_timeout_handler
));
280 struct timeout
* real_timeout
= malloc(sizeof(struct timeout
));
282 mth
->timeout_list
= 0;
283 mth
->timeout_list
= g_slist_prepend(mth
->timeout_list
, t1
);
284 mth
->timeout_list
= g_slist_prepend(mth
->timeout_list
, t2
);
285 mth
->parent_timeout
= real_timeout
;
287 g_hash_table_insert(multi_timeouts
, t1
, mth
);
288 g_hash_table_insert(multi_timeouts
, t2
, mth
);
289 g_hash_table_insert(multi_timeouts
, real_timeout
, mth
);
291 t1
->multi_timeout
= mt1
;
292 t2
->multi_timeout
= mt2
;
293 real_timeout
->multi_timeout
= real_timeout
;
295 timeout_list
= g_slist_remove(timeout_list
, t1
);
296 timeout_list
= g_slist_remove(timeout_list
, t2
);
298 update_multi_timeout_values(mth
);
302 void append_multi_timeout(struct timeout
* t1
, struct timeout
* t2
)
304 if (t2
->multi_timeout
) {
305 // swap t1 and t2 such that t1 is the multi timeout
306 struct timeout
* tmp
= t2
;
311 struct multi_timeout
* mt
= malloc(sizeof(struct multi_timeout
));
312 struct multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t1
);
314 mth
->timeout_list
= g_slist_prepend(mth
->timeout_list
, t2
);
315 g_hash_table_insert(multi_timeouts
, t2
, mth
);
317 t2
->multi_timeout
= mt
;
319 update_multi_timeout_values(mth
);
323 void update_multi_timeout_values(struct multi_timeout_handler
* mth
)
325 int interval
= calc_multi_timeout_interval(mth
);
326 int next_timeout_msec
= interval
;
328 struct timespec cur_time
;
329 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
331 GSList
* it
= mth
->timeout_list
;
332 struct timespec diff_time
;
334 struct timeout
* t
= it
->data
;
335 struct multi_timeout
* mt
= t
->multi_timeout
;
336 mt
->count_to_expiration
= t
->interval_msec
/ interval
;
337 timespec_subtract(&diff_time
, &t
->timeout_expires
, &cur_time
);
338 int msec_to_expiration
= diff_time
.tv_sec
*1000 + diff_time
.tv_nsec
/1000000;
339 int count_left
= msec_to_expiration
/ interval
+ (msec_to_expiration%interval
!= 0);
340 mt
->current_count
= mt
->count_to_expiration
- count_left
;
341 if (msec_to_expiration
< next_timeout_msec
)
342 next_timeout_msec
= msec_to_expiration
;
346 mth
->parent_timeout
->interval_msec
= interval
;
347 timeout_list
= g_slist_remove(timeout_list
, mth
->parent_timeout
);
348 add_timeout_intern(next_timeout_msec
, interval
, callback_multi_timeout
, mth
, mth
->parent_timeout
);
352 void callback_multi_timeout(void* arg
)
354 struct multi_timeout_handler
* mth
= arg
;
355 struct timespec cur_time
;
356 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
357 GSList
* it
= mth
->timeout_list
;
359 struct timeout
* t
= it
->data
;
360 struct multi_timeout
* mt
= t
->multi_timeout
;
361 if (++mt
->current_count
>= mt
->count_to_expiration
) {
362 t
->_callback(t
->arg
);
363 mt
->current_count
= 0;
364 t
->timeout_expires
= add_msec_to_timespec(cur_time
, t
->interval_msec
);
371 void remove_from_multi_timeout(struct timeout
* t
)
373 struct multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t
);
374 g_hash_table_remove(multi_timeouts
, t
);
376 mth
->timeout_list
= g_slist_remove(mth
->timeout_list
, t
);
377 free(t
->multi_timeout
);
378 t
->multi_timeout
= 0;
380 if (g_slist_length(mth
->timeout_list
) == 1) {
381 struct timeout
* last_timeout
= mth
->timeout_list
->data
;
382 free(last_timeout
->multi_timeout
);
383 last_timeout
->multi_timeout
= 0;
384 g_hash_table_remove(multi_timeouts
, last_timeout
);
385 g_hash_table_remove(multi_timeouts
, mth
->parent_timeout
);
386 mth
->parent_timeout
->multi_timeout
= 0;
387 stop_timeout(mth
->parent_timeout
);
390 struct timespec cur_time
, diff_time
;
391 clock_gettime(CLOCK_MONOTONIC
, &cur_time
);
392 timespec_subtract(&diff_time
, &t
->timeout_expires
, &cur_time
);
393 int msec_to_expiration
= diff_time
.tv_sec
*1000 + diff_time
.tv_nsec
/1000000;
394 add_timeout_intern(msec_to_expiration
, last_timeout
->interval_msec
, last_timeout
->_callback
, last_timeout
->arg
, last_timeout
);
397 update_multi_timeout_values(mth
);
401 void stop_multi_timeout(struct timeout
* t
)
403 struct multi_timeout_handler
* mth
= g_hash_table_lookup(multi_timeouts
, t
);
404 g_hash_table_remove(multi_timeouts
, mth
->parent_timeout
);
405 while (mth
->timeout_list
) {
406 struct timeout
* t
= mth
->timeout_list
->data
;
407 mth
->timeout_list
= g_slist_remove(mth
->timeout_list
, t
);
408 g_hash_table_remove(multi_timeouts
, t
);