From: w00t Date: Sun, 13 Jan 2008 21:29:53 +0000 (+0000) Subject: New timer code. This may be a tiny fraction slower (though I think it will be accepta... X-Git-Tag: v2.0.23~4037 X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=c88afe5b9abba09b883d59f46a7e2795df181394;p=user%2Fhenk%2Fcode%2Finspircd.git 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 --- diff --git a/include/timer.h b/include/timer.h index 7f1dd7396..abe15a2b7 100644 --- a/include/timer.h +++ b/include/timer.h @@ -44,7 +44,7 @@ class CoreExport Timer : public Extensible * @param now The time now * @param repeating Repeat this timer every secs_from_now seconds if set to true */ - Timer(long secs_from_now,time_t now, bool repeating = false) + Timer(long secs_from_now, time_t now, bool repeating = false) { trigger = now + secs_from_now; secs = secs_from_now; @@ -62,6 +62,13 @@ class CoreExport Timer : public Extensible return trigger; } + /** Sets the trigger timeout to a new value + */ + virtual void SetTimer(time_t t) + { + trigger = t; + } + /** Called when the timer ticks. * You should override this method with some useful code to * handle the tick event. @@ -107,32 +114,23 @@ class CoreExport Timer : public Extensible class CoreExport TimerManager : public Extensible { protected: - /** A group of timers all set to trigger at the same time + /** A list of all pending timers */ - typedef std::vector timergroup; - /** A map of timergroups, each group has a specific trigger time - */ - typedef std::map timerlist; - /** Set when ticking timers, to prevent deletion while iterating - */ - bool CantDeleteHere; + std::vector Timers; + /** Creating server instance */ InspIRCd* ServerInstance; - private: - - /** The current timer set, a map of timergroups - */ - timerlist Timers; - public: /** Constructor */ TimerManager(InspIRCd* Instance); + /** Tick all pending Timers * @param TIME the current system time */ void TickTimers(time_t TIME); + /** Add an Timer * @param T an Timer derived class to add * @param secs_from_now You may set this to the number of seconds @@ -141,15 +139,16 @@ class CoreExport TimerManager : public Extensible * will be used. This is used internally for re-triggering repeating * timers. */ - void AddTimer(Timer* T, long secs_from_now = 0); + void AddTimer(Timer *T); + /** Delete an Timer * @param T an Timer derived class to delete */ void DelTimer(Timer* T); - /** Tick any timers that have been missed due to lag - * @param TIME the current system time + + /** Compares two timers */ - void TickMissedTimers(time_t TIME); + static bool TimerComparison( Timer *one, Timer*two); }; #endif 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::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::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()); } +