Timer: do not clear timers restarted from their own callback function

This commit is contained in:
o9000
2017-11-17 15:08:27 +01:00
parent 07339c09a0
commit 38488b8d75

View File

@@ -45,6 +45,8 @@ struct _timeout {
multi_timeout *multi_timeout; multi_timeout *multi_timeout;
timeout **self; timeout **self;
gboolean expired; gboolean expired;
// timer has been restarted from its own callback function
gboolean reactivated;
}; };
void add_timeout_intern(int value_msec, int interval_msec, void (*_callback)(void *), void *arg, timeout *t); void add_timeout_intern(int value_msec, int interval_msec, void (*_callback)(void *), void *arg, timeout *t);
@@ -158,18 +160,21 @@ void handle_expired_timers()
if (compare_timespecs(&t->timeout_expires, &cur_time) <= 0) { if (compare_timespecs(&t->timeout_expires, &cur_time) <= 0) {
// it's time for the callback function // it's time for the callback function
t->expired = t->interval_msec == 0; t->expired = t->interval_msec == 0;
t->reactivated = FALSE;
t->_callback(t->arg); t->_callback(t->arg);
// If _callback() calls stop_timeout(t) the timer 't' was freed and is not in the timeout_list if (!t->reactivated) {
if (g_slist_find(timeout_list, t)) { // If _callback() calls stop_timeout(t) the timer 't' was freed and is not in the timeout_list
// Timer still exists if (g_slist_find(timeout_list, t)) {
timeout_list = g_slist_remove(timeout_list, t); // Timer still exists
if (t->interval_msec > 0) { timeout_list = g_slist_remove(timeout_list, t);
add_timeout_intern(t->interval_msec, t->interval_msec, t->_callback, t->arg, t); if (t->interval_msec > 0) {
} else { add_timeout_intern(t->interval_msec, t->interval_msec, t->_callback, t->arg, t);
// Destroy single-shot timer } else {
if (t->self) // Destroy single-shot timer
*t->self = NULL; if (t->self)
free(t); *t->self = NULL;
free(t);
}
} }
} }
} else { } else {
@@ -208,6 +213,7 @@ void add_timeout_intern(int value_msec, int interval_msec, void (*_callback)(),
can_align = align_with_existing_timeouts(t); can_align = align_with_existing_timeouts(t);
if (!can_align) if (!can_align)
timeout_list = g_slist_insert_sorted(timeout_list, t, compare_timeouts); timeout_list = g_slist_insert_sorted(timeout_list, t, compare_timeouts);
t->reactivated = TRUE;
} }
gint compare_timeouts(gconstpointer t1, gconstpointer t2) gint compare_timeouts(gconstpointer t1, gconstpointer t2)