Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug in timer when the list of timers changed.
Browse files Browse the repository at this point in the history
Fix contributed by Michael Bicha
  • Loading branch information
slouken committed Jan 22, 2011
1 parent 48df290 commit 09caf23
Showing 1 changed file with 51 additions and 40 deletions.
91 changes: 51 additions & 40 deletions src/timer/SDL_timer.c
Expand Up @@ -113,56 +113,67 @@ SDL_ThreadedTimerCheck(void)
SDL_bool removed;

SDL_mutexP(SDL_timer_mutex);
list_changed = SDL_FALSE;

now = SDL_GetTicks();
for (prev = NULL, t = SDL_timers; t; t = next) {
removed = SDL_FALSE;
ms = t->interval - SDL_TIMESLICE;
next = t->next;
if ((int) (now - t->last_alarm) > (int) ms) {
struct _SDL_TimerID timer;
do {
list_changed = SDL_FALSE;
for (prev = NULL, t = SDL_timers; t; t = next) {
removed = SDL_FALSE;
ms = t->interval - SDL_TIMESLICE;
next = t->next;
if ((int) (now - t->last_alarm) > (int) ms) {
struct _SDL_TimerID timer;

if ((now - t->last_alarm) < t->interval) {
t->last_alarm += t->interval;
} else {
t->last_alarm = now;
}
#ifdef DEBUG_TIMERS
printf("Executing timer %p (thread = %lu)\n", t, SDL_ThreadID());
#endif
timer = *t;
SDL_mutexV(SDL_timer_mutex);
ms = timer.cb(timer.interval, timer.param);
SDL_mutexP(SDL_timer_mutex);
if (list_changed) {
/* Abort, list of timers modified */
/* FIXME: what if ms was changed? */
break;
}
if (ms != t->interval) {
if (ms) {
t->interval = ROUND_RESOLUTION(ms);
if ((now - t->last_alarm) < t->interval) {
t->last_alarm += t->interval;
} else {
/* Remove timer from the list */
t->last_alarm = now;
}
#ifdef DEBUG_TIMERS
printf("SDL: Removing timer %p\n", t);
printf("Executing timer %p (thread = %lu)\n",
t, SDL_ThreadID());
#endif
if (prev) {
prev->next = next;
timer = *t;
SDL_mutexV(SDL_timer_mutex);
ms = timer.cb(timer.interval, timer.param);
SDL_mutexP(SDL_timer_mutex);
if (list_changed) {
next = t->next;
for (prev = SDL_timers; prev; prev = prev->next) {
if (prev->next == t)
break;
}
}
if (ms != t->interval) {
if (ms) {
t->interval = ROUND_RESOLUTION(ms);
} else {
SDL_timers = next;
/* Remove timer from the list */
#ifdef DEBUG_TIMERS
printf("SDL: Removing timer %p\n", t);
#endif
if (prev) {
prev->next = next;
} else {
SDL_timers = next;
}
SDL_free(t);
--SDL_timer_running;
removed = SDL_TRUE;
}
SDL_free(t);
--SDL_timer_running;
removed = SDL_TRUE;
}
if (list_changed) {
/* Abort, list of timers modified */
break;
}
}
/* Don't update prev if the timer has disappeared */
if (!removed) {
prev = t;
}
}
/* Don't update prev if the timer has disappeared */
if (!removed) {
prev = t;
}
}
} while (list_changed);

SDL_mutexV(SDL_timer_mutex);
}

Expand Down

0 comments on commit 09caf23

Please sign in to comment.