void Timers::Execute() { timeval curTime; gettimeofday(&curTime, 0); for (Timer* s = m_firstTimer; s != 0; s = m_firstTimer) { if (CompareTimevals(s->GetNextExecution(), curTime)) break; TimerCbk_t cbk = s->GetCallback(); void* userdata = s->GetUserdata(); s->Update(); int remainingReps = s->GetRemainingRepetitions(); cbk(userdata); if (FindTimer(s)) { DeleteFromList(s); if (remainingReps == 0) delete s; else InsertTimer(s); } } }
void Timers::InsertTimer(Timer* timer) { if (!m_firstTimer) { m_firstTimer = timer; m_lastTimer = timer; } else { Timer* s; Timer* s2 = 0; const timeval& tv = timer->GetNextExecution(); for (s = m_firstTimer; s != 0; s = s->next) { if (CompareTimevals(s->GetNextExecution(), tv)) break; s2 = s; } if (!s2) { /* First timer to be executed */ m_firstTimer->prev = timer; timer->next = m_firstTimer; m_firstTimer = timer; } else if (!s) { /* Last timer to be executed */ m_lastTimer->next = timer; timer->prev = m_lastTimer; m_lastTimer = timer; } else { /* Intermediate */ s2->next = timer; s->prev = timer; timer->next = s; timer->prev = s2; } } }