]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
New timer code. This may be a tiny fraction slower (though I think it will be accepta...
authorw00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7>
Sun, 13 Jan 2008 21:29:53 +0000 (21:29 +0000)
committerw00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7>
Sun, 13 Jan 2008 21:29:53 +0000 (21:29 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8708 e03df62e-2008-0410-955e-edbf42e46eb7

include/timer.h
src/inspircd.cpp
src/timer.cpp

index 7f1dd7396ba85c714449e027bcd88c326666c0a5..abe15a2b7c802474838a892223fb5b1ee82992b5 100644 (file)
@@ -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<Timer*> timergroup;
-       /** A map of timergroups, each group has a specific trigger time
-        */
-       typedef std::map<time_t, timergroup*> timerlist;
-       /** Set when ticking timers, to prevent deletion while iterating
-        */
-       bool CantDeleteHere;
+       std::vector<Timer *> 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
index e52aa8fc22d4fd6863a975a3d40246fb8c1055ad..4ca4c50d6c729abf67c3a1a60327db60bde09b01 100644 (file)
@@ -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
index d9b1b6414f9c9b6a9bce03979b2f855b1735875c..ca7534a7c7409759d42808258c3162db706f019a 100644 (file)
 #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());
 }
 
+