void SDL_ThreadedTimerCheck(void) { Uint32 now, ms; SDL_TimerID t, prev, next; 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; 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 = %d)\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); } else { /* 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; } } } /* Don't update prev if the timer has disappeared */ if ( ! removed ) { prev = t; } } SDL_mutexV(SDL_timer_mutex); }
SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param) { SDL_TimerID t; if ( ! SDL_timer_mutex ) { if ( SDL_timer_started ) { SDL_SetError("This platform doesn't support multiple timers"); } else { SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first"); } return NULL; } if ( ! SDL_timer_threaded ) { SDL_SetError("Multiple timers require threaded events!"); return NULL; } SDL_mutexP(SDL_timer_mutex); t = (SDL_TimerID) malloc(sizeof(struct _SDL_TimerID)); if ( t ) { t->interval = ROUND_RESOLUTION(interval); t->cb = callback; t->param = param; t->last_alarm = SDL_GetTicks(); t->next = SDL_timers; SDL_timers = t; ++ num_timers; list_changed = SDL_TRUE; SDL_timer_running = 1; } #ifdef DEBUG_TIMERS printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, num_timers); #endif SDL_mutexV(SDL_timer_mutex); return t; }
void SDL_ThreadedTimerCheck(void) { Uint32 now, ms; SDL_TimerID t, prev, next; int removed; now = SDL_GetTicks(); SDL_mutexP(SDL_timer_mutex); for ( prev = NULL, t = SDL_timers; t; t = next ) { removed = 0; ms = t->interval - SDL_TIMESLICE; next = t->next; if ( (t->last_alarm < now) && ((now - t->last_alarm) > ms) ) { if ( (now - t->last_alarm) < t->interval ) { t->last_alarm += t->interval; } else { t->last_alarm = now; } list_changed = SDL_FALSE; #ifdef DEBUG_TIMERS printf("Executing timer %p (thread = %d)\n", t, SDL_ThreadID()); #endif SDL_mutexV(SDL_timer_mutex); ms = t->cb(t->interval, t->param); SDL_mutexP(SDL_timer_mutex); if ( list_changed ) { /* Abort, list of timers has been modified */ break; } if ( ms != t->interval ) { if ( ms ) { t->interval = ROUND_RESOLUTION(ms); } else { /* Remove the timer from the linked list */ #ifdef DEBUG_TIMERS printf("SDL: Removing timer %p\n", t); #endif if ( prev ) { prev->next = next; } else { SDL_timers = next; } free(t); -- num_timers; removed = 1; } } } /* Don't update prev if the timer has disappeared */ if ( ! removed ) { prev = t; } } SDL_mutexV(SDL_timer_mutex); }
void SDLMOD_ThreadedTimerCheck(void) { uint32_t now, ms; SDLMOD_TimerID t, prev, next; int removed; pthread_mutex_lock(SDLMOD_timer_mutex); list_changed = 0; now = SDLMOD_GetTicks(); for ( prev = NULL, t = SDLMOD_timers; t; t = next ) { removed = 0; ms = t->interval - SDLMOD_TIMESLICE; next = t->next; if ( (int)(now - t->last_alarm) > (int)ms ) { struct _SDLMOD_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\n", t); #endif timer = *t; pthread_mutex_unlock(SDLMOD_timer_mutex); ms = timer.cb(timer.interval, timer.param); pthread_mutex_lock(SDLMOD_timer_mutex); if ( list_changed ) { break; } if ( ms != t->interval ) { if ( ms ) { t->interval = ROUND_RESOLUTION(ms); } else { #ifdef DEBUG_TIMERS printf("SDL: Removing timer %p\n", t); #endif if ( prev ) { prev->next = next; } else { SDLMOD_timers = next; } free(t); --SDLMOD_timer_running; removed = 1; } } } if ( ! removed ) { prev = t; } } pthread_mutex_unlock(SDLMOD_timer_mutex); }
/* Our Stub routine to set up and then call the real routine. */ pascal void TimerCallbackProc(TMTaskPtr tmTaskPtr) { Uint32 ms; WakeUpProcess(&((ExtendedTimerPtr) tmTaskPtr)->taskPSN); ms = SDL_alarm_callback(SDL_alarm_interval); if ( ms ) { SDL_alarm_interval = ROUND_RESOLUTION(ms); PrimeTime((QElemPtr)&gExtendedTimerRec.tmTask, SDL_alarm_interval); } else { SDL_alarm_interval = 0; } }
static SDL_TimerID SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback, void *param) { SDL_TimerID t; t = (SDL_TimerID) SDL_malloc(sizeof(struct _SDL_TimerID)); if ( t ) { t->interval = ROUND_RESOLUTION(interval); t->cb = callback; t->param = param; t->last_alarm = SDL_GetTicks(); t->next = SDL_timers; SDL_timers = t; ++SDL_timer_running; list_changed = SDL_TRUE; } #ifdef DEBUG_TIMERS printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, SDL_timer_running); #endif return t; }
void RISCOS_CheckTimer() { if (SDL_timer_running && SDL_GetTicks() - timerStart >= SDL_alarm_interval) { Uint32 ms; ms = SDL_alarm_callback(SDL_alarm_interval); if (ms != SDL_alarm_interval) { if (ms) { SDL_alarm_interval = ROUND_RESOLUTION(ms); } else { SDL_alarm_interval = 0; SDL_timer_running = 0; } } if (SDL_alarm_interval) timerStart = SDL_GetTicks(); } }