Reinitialize timers correctly when created from their own callbacks

This commit is contained in:
o9000 2017-08-21 14:26:20 +02:00
parent c5d2ddc156
commit 8e50c20c9d
5 changed files with 9 additions and 9 deletions

View file

@ -226,8 +226,7 @@ void init_battery()
battery_found = battery_os_init();
if (!battery_timeout)
battery_timeout = add_timeout(10, 30000, update_battery_tick, 0, &battery_timeout);
battery_timeout = add_timeout(10, 30000, update_battery_tick, 0, &battery_timeout);
update_battery();
}

View file

@ -154,8 +154,8 @@ int ms_until_second_change(struct timeval* tm)
void update_clocks_sec(void *arg)
{
gettimeofday(&time_clock, 0);
clock_timeout = add_timeout(ms_until_second_change(&time_clock), 0, update_clocks_sec, 0, NULL);
update_clocks();
clock_timeout = add_timeout(ms_until_second_change(&time_clock), 0, update_clocks_sec, 0, &clock_timeout);
}
void update_clocks_min(void *arg)
@ -164,10 +164,10 @@ void update_clocks_min(void *arg)
// on next minute change
static time_t old_sec = 0;
gettimeofday(&time_clock, 0);
clock_timeout = add_timeout(ms_until_second_change(&time_clock), 0, update_clocks_min, 0, NULL);
if (time_clock.tv_sec % 60 == 0 || time_clock.tv_sec - old_sec > 60)
update_clocks();
old_sec = time_clock.tv_sec;
clock_timeout = add_timeout(ms_until_second_change(&time_clock), 0, update_clocks_min, 0, &clock_timeout);
}
gboolean time_format_needs_sec_ticks(char *time_format)

View file

@ -196,8 +196,7 @@ void init_execp_panel(void *p)
execp->area.on_screen = TRUE;
instantiate_area_gradients(&execp->area);
if (!execp->backend->timer)
execp->backend->timer = add_timeout(10, 0, execp_timer_callback, execp, &execp->backend->timer);
execp->backend->timer = add_timeout(10, 0, execp_timer_callback, execp, &execp->backend->timer);
execp_update_post_read(execp);
}

View file

@ -1498,7 +1498,7 @@ void systray_render_icon(void *t)
Window root;
if (!XGetGeometry(server.display, traywin->win, &root, &xpos, &ypos, &width, &height, &border_width, &depth)) {
stop_timeout(traywin->render_timeout);
if (!traywin->resize_timeout)
if (!traywin->render_timeout)
traywin->render_timeout =
add_timeout(min_refresh_period, 0, systray_render_icon, traywin, &traywin->render_timeout);
systray_render_icon_from_image(traywin);
@ -1507,7 +1507,7 @@ void systray_render_icon(void *t)
} else {
if (xpos != 0 || ypos != 0 || width != traywin->width || height != traywin->height) {
stop_timeout(traywin->render_timeout);
if (!traywin->resize_timeout)
if (!traywin->render_timeout)
traywin->render_timeout =
add_timeout(min_refresh_period, 0, systray_render_icon, traywin, &traywin->render_timeout);
systray_render_icon_from_image(traywin);

View file

@ -44,6 +44,7 @@ struct _timeout {
void *arg;
multi_timeout *multi_timeout;
timeout **self;
gboolean expired;
};
void add_timeout_intern(int value_msec, int interval_msec, void (*_callback)(void *), void *arg, timeout *t);
@ -106,7 +107,7 @@ int gettime(struct timespec *tp)
timeout *add_timeout(int value_msec, int interval_msec, void (*_callback)(void *), void *arg, timeout **self)
{
if (self && *self)
if (self && *self && !(*self)->expired)
return *self;
timeout *t = calloc(1, sizeof(timeout));
t->self = self;
@ -155,6 +156,7 @@ void callback_timeout_expired()
t = timeout_list->data;
if (compare_timespecs(&t->timeout_expires, &cur_time) <= 0) {
// it's time for the callback function
t->expired = t->interval_msec == 0;
t->_callback(t->arg);
// If _callback() calls stop_timeout(t) the timer 't' was freed and is not in the timeout_list
if (g_slist_find(timeout_list, t)) {