diff options
author | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-01-13 21:29:53 +0000 |
---|---|---|
committer | w00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-01-13 21:29:53 +0000 |
commit | c88afe5b9abba09b883d59f46a7e2795df181394 (patch) | |
tree | bfb7a57dea623645288af91fee6cf481b0ba278e /src | |
parent | 2620d1258c6a2c249b9503a7c4a764e26a2da0f3 (diff) |
New timer code. This may be a tiny fraction slower (though I think it will be acceptable given that we no longer need to tick old timers etc), but it is a lot simpler (about half the codesize of the old + no allocation of new timergroups etc), and should (I hope) always tick timers and never 'lose' them. Performs okay under 3500 connection attempts (0:00 CPU time :))
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8708 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r-- | src/inspircd.cpp | 1 | ||||
-rw-r--r-- | src/timer.cpp | 116 |
2 files changed, 22 insertions, 95 deletions
diff --git a/src/inspircd.cpp b/src/inspircd.cpp index e52aa8fc2..4ca4c50d6 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -642,7 +642,6 @@ int InspIRCd::Run() if ((TIME % 5) == 0) { FOREACH_MOD_I(this,I_OnBackgroundTimer,OnBackgroundTimer(TIME)); - Timers->TickMissedTimers(TIME); SNO->FlushSnotices(); } #ifndef WIN32 diff --git a/src/timer.cpp b/src/timer.cpp index d9b1b6414..ca7534a7c 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -16,122 +16,50 @@ #include "inspircd.h" #include "timer.h" -TimerManager::TimerManager(InspIRCd* Instance) : CantDeleteHere(false), ServerInstance(Instance) +TimerManager::TimerManager(InspIRCd* Instance) : ServerInstance(Instance) { } void TimerManager::TickTimers(time_t TIME) { - this->CantDeleteHere = true; - timerlist::iterator found = Timers.find(TIME); - - if (found != Timers.end()) + while ((Timers.size()) && (TIME > (*Timers.begin())->GetTimer())) { - timergroup* x = found->second; - /* There are pending timers to trigger. - * WARNING: Timers may delete themselves from within - * their own Tick methods! see the comment below in - * the DelTimer method. - */ - for (timergroup::iterator y = x->begin(); y != x->end(); y++) + std::vector<Timer *>::iterator i = Timers.begin(); + Timer *t = (*i); + + t->Tick(TIME); + if (t->GetRepeat()) { - Timer* n = *y; - n->Tick(TIME); - if (n->GetRepeat()) - { - AddTimer(n, n->GetSecs()); - } - else - { - delete n; - } + t->SetTimer(TIME + t->GetSecs()); + AddTimer(t); } + else + delete t; - Timers.erase(found); - delete x; + Timers.erase(i); } - - this->CantDeleteHere = false; } void TimerManager::DelTimer(Timer* T) { - if (this->CantDeleteHere) - { - /* If a developer tries to delete a timer from within its own Tick method, - * then chances are this is just going to totally fuck over the timergroup - * and timerlist iterators and cause a crash. Thanks to peavey and Bricker - * for noticing this bug. - * If we're within the tick loop when the DelTimer is called (signified - * by the var 'CantDeleteHere') then we simply return for non-repeating - * timers, and cancel the repeat on repeating timers. We can do this because - * we know that the timer tick loop will safely delete the timer for us - * anyway and therefore we avoid stack corruption. - */ - if (T->GetRepeat()) - T->CancelRepeat(); - else - return; - } - - timerlist::iterator found = Timers.find(T->GetTimer()); + std::vector<Timer *>::iterator i = std::find(Timers.begin(), Timers.end(), T); - if (found != Timers.end()) + if (i != Timers.end()) { - timergroup* x = found->second; - for (timergroup::iterator y = x->begin(); y != x->end(); y++) - { - Timer* n = *y; - if (n == T) - { - delete n; - x->erase(y); - if (!x->size()) - { - Timers.erase(found); - delete x; - } - return; - } - } + delete (*i); + Timers.erase(i); } } -/** Because some muppets may do odd things, and their ircd may lock up due - * to crappy 3rd party modules, or they may change their system time a bit, - * this accounts for shifts of up to 120 secs by looking behind for missed - * timers and executing them. This is only executed once every 5 secs. - * If you move your clock BACK, and your timers move further ahead as a result, - * then tough titty you'll just have to wait. - */ -void TimerManager::TickMissedTimers(time_t TIME) +void TimerManager::AddTimer(Timer* T) { - for (time_t n = TIME-1; n > TIME-120; n--) - this->TickTimers(TIME); + Timers.push_back(T); + sort(Timers.begin(), Timers.end(), TimerManager::TimerComparison); } -void TimerManager::AddTimer(Timer* T, long secs_from_now) +bool TimerManager::TimerComparison( Timer *one, Timer *two) { - timergroup* x = NULL; - - int time_to_trigger = 0; - if (!secs_from_now) - time_to_trigger = T->GetTimer(); - else - time_to_trigger = secs_from_now + ServerInstance->Time(); - - timerlist::iterator found = Timers.find(time_to_trigger); - - if (found != Timers.end()) - { - x = found->second; - } - else - { - x = new timergroup; - Timers[time_to_trigger] = x; - } - - x->push_back(T); + return (one->GetTimer()) < (two->GetTimer()); } + |