hlswmaster-ng/timerservice.cpp

74 lines
1.2 KiB
C++
Raw Normal View History

2006-02-02 16:55:44 +01:00
#include <time.h>
#include "timerservice.h"
2006-03-06 20:13:26 +01:00
// TODO: how to deregister TimerEvents?
2006-02-20 21:58:59 +01:00
Timer::Timer(Event* event, unsigned int timeval)
: next(0), event(event), timeval(timeval)
2006-02-02 16:55:44 +01:00
{
}
Timer::~Timer()
{
2006-02-20 21:58:59 +01:00
delete event;
2006-02-02 16:55:44 +01:00
}
TimerService::~TimerService()
{
Timer* search = firstTimer;
while (search != NULL) {
Timer* next = search->next;
delete search;
search = next;
}
}
void TimerService::addTimer(Timer* te, bool trigger)
{
te->timeout = time(NULL) + ((!trigger) ? te->timeval : 0);
Timer** search = &firstTimer;
while ((*search) != NULL && (*search)->timeout <= te->timeout)
search = &((*search)->next);
te->next = (*search);
(*search) = te;
}
void TimerService::checkTimeouts()
{
long now = time(NULL);
Timer* search = firstTimer;
while (search != NULL && search->timeout <= now) {
2006-02-20 21:58:59 +01:00
search->event->execute();
2006-02-02 16:55:44 +01:00
firstTimer = search->next;
addTimer(search, false);
search = firstTimer;
}
}
TimerService* TimerService::getInstance()
{
static TimerService ts;
return &ts;
}
void TimerService::registerTimer(Timer* te)
{
TimerService* ts = getInstance();
AutoMutex am(ts->mutex);
ts->addTimer(te, true);
}
void TimerService::checkTimers()
{
TimerService* ts = getInstance();
AutoMutex am(ts->mutex);
ts->checkTimeouts();
}