- close(id);
- GSList* timer_iter = timer_list;
- while (timer_iter) {
- struct timer* t = timer_iter->data;
- if (t->id == id) {
- timer_list = g_slist_remove(timer_list, t);
- free(t);
- return;
+ if (timeout_list) {
+ struct timeout* t = timeout_list->data;
+ struct timespec cur_time;
+ clock_gettime(CLOCK_MONOTONIC, &cur_time);
+ if (timespec_subtract(&next_timeout, &t->timeout_expires, &cur_time)) {
+ next_timeout.tv_sec = 0;
+ next_timeout.tv_nsec = 0;
+ }
+ }
+ else
+ next_timeout.tv_sec = -1;
+}
+
+
+void callback_timeout_expired()
+{
+ struct timespec cur_time;
+ struct timeout* t;
+ while (timeout_list) {
+ clock_gettime(CLOCK_MONOTONIC, &cur_time);
+ t = timeout_list->data;
+ if (compare_timespecs(&t->timeout_expires, &cur_time) <= 0) {
+ // it's time for the callback function
+ t->_callback();
+ if (g_slist_find(timeout_list, t)) {
+ // if _callback() calls stop_timeout(t) the timeout 't' was freed and is not in the timeout_list
+ timeout_list = g_slist_remove(timeout_list, t);
+ if (t->interval_msec > 0)
+ add_timeout_intern(t->interval_msec, t->interval_msec, t->_callback, t);
+ else
+ free(t);
+ }