]> git.tdb.fi Git - libs/core.git/commitdiff
Rewrite Timer to use a heap instead of set to deal with duplicate timeouts
authorMikko Rasa <tdb@tdb.fi>
Fri, 11 Sep 2009 17:32:58 +0000 (17:32 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 11 Sep 2009 17:32:58 +0000 (17:32 +0000)
Get local timezone from /etc/localtime instead of tzset()
Add TimeStamp::to_unixtime

source/time/timer.cpp
source/time/timer.h
source/time/timestamp.h
source/time/timezone.cpp
source/time/timezone.h

index c6e546fc78b782a127a4603076e7c0a0ae7f03d4..9471463216206b701d285562bb41e0af1901fafc 100644 (file)
@@ -1,10 +1,11 @@
 /* $Id$
 
 This file is part of libmspcore     
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+Copyright © 2006, 2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <algorithm>
 #include "timer.h"
 #include "utils.h"
 
@@ -15,7 +16,7 @@ namespace Time {
 
 Timer::~Timer()
 {
-       for(set<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
+       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
                delete i->slot;
 }
 
@@ -23,7 +24,8 @@ Timer::Slot &Timer::add(const TimeDelta &td)
 {
        Slot *s=new Slot(td);
        mutex.lock();
-       slots.insert(s);
+       slots.push_back(s);
+       push_heap(slots.begin(), slots.end());
        mutex.unlock();
        sem.signal();
        return *s;
@@ -34,7 +36,8 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
        Slot *s=new Slot(ts);
        {
                MutexLock l(mutex);
-               slots.insert(s);
+               slots.push_back(s);
+               push_heap(slots.begin(), slots.end());
        }
        sem.signal();
        return *s;
@@ -43,36 +46,62 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
 void Timer::cancel(Slot &slot)
 {
        MutexLock l(mutex);
-       if(slots.erase(&slot))
-               delete &slot;
+       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
+               if(i->slot==&slot)
+               {
+                       delete i->slot;
+                       slots.erase(i);
+                       make_heap(slots.begin(), slots.end());
+                       return;
+               }
 }
 
 void Timer::tick(bool block)
 {
-       if(slots.empty())
-       {
-               if(block)
-                       sem.wait();
-               else
-                       return;
-       }
-
-       Slot *next;
+       Slot *next=0;
        {
                MutexLock l(mutex);
-               next=slots.begin()->slot;
+               while(1)
+               {
+                       if(slots.empty())
+                       {
+                               if(block)
+                                       sem.wait();
+                               else
+                                       return;
+                       }
+
+                       next=slots.begin()->slot;
+                       const TimeStamp &stamp=next->get_timeout();
+                       const TimeStamp t=now();
+                       if(stamp<=t)
+                               break;
+                       else if(block)
+                               sem.wait(stamp-t);
+                       else
+                               return;
+               }
+
+               pop_heap(slots.begin(), slots.end());
+               slots.pop_back();
        }
 
-       const TimeStamp &stamp=next->get_timeout();
-       const TimeStamp t=now();
-       if(stamp<=t || (block && sem.wait(stamp-t)==1))
+       try
        {
-               slots.erase(slots.begin());
                if(next->signal_timeout.emit() && next->increment())
-                       slots.insert(next);
+               {
+                       MutexLock l(mutex);
+                       slots.push_back(next);
+                       push_heap(slots.begin(), slots.end());
+               }
                else
                        delete next;
        }
+       catch(...)
+       {
+               delete next;
+               throw;
+       }
 }
 
 TimeStamp Timer::get_next_timeout() const
@@ -107,7 +136,7 @@ Timer::SlotProxy::SlotProxy(Slot *s):
 
 bool Timer::SlotProxy::operator<(const SlotProxy &sp) const
 {
-       return slot->get_timeout()<sp.slot->get_timeout();
+       return slot->get_timeout()>sp.slot->get_timeout();
 }
 
 } // namespace Time
index 935df0323debc6c25dbb6e1bb8f62c54f12d67db..a0dabcdbd7b7cc8e08d553ba7c3e8ea46797762c 100644 (file)
@@ -1,14 +1,14 @@
 /* $Id$
 
 This file is part of libmspcore     
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+Copyright © 2006, 2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
 #ifndef MSP_TIME_TIMER_H_
 #define MSP_TIME_TIMER_H_
 
-#include <set>
+#include <vector>
 #include <sigc++/sigc++.h>
 #include "../core/mutex.h"
 #include "../core/semaphore.h"
@@ -53,7 +53,7 @@ private:
                bool operator<(const SlotProxy &) const;
        };
 
-       std::set<SlotProxy> slots;
+       std::vector<SlotProxy> slots;
        Semaphore sem;
        Mutex mutex;
 
index faa200c6590cb9ae14de30cc69286193fd83222f..db3587a253e915787aa6f643e80d02124b6f49cc 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspcore     
-Copyright © 2006  Mikko Rasa, Mikkosoft Productions
+Copyright © 2006, 2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -44,6 +44,8 @@ public:
        */
        RawTime raw() const { return usec; }
 
+       time_t to_unixtime() { return usec/1000000LL; }
+
        TimeStamp operator+(const TimeDelta &t) const  { return TimeStamp(usec+t.raw()); }
        TimeStamp &operator+=(const TimeDelta &t)      { usec+=t.raw(); return *this; }
        TimeStamp operator-(const TimeDelta &t) const  { return TimeStamp(usec-t.raw()); }
index 541514f8b8272c1573b4a361c85675a3cf92ce09..3910c24bbf3a88bd615c1a8bc7ccfd1700c65719 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspcore
-Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -11,11 +11,13 @@ Distributed under the LGPL
 #ifdef WIN32
 #include <windows.h>
 #else
-#include <time.h>
+#include <fcntl.h>
 #endif
 #include <msp/core/except.h>
+#include "timestamp.h"
 #include "timezone.h"
 #include "units.h"
+#include "utils.h"
 
 using namespace std;
 
@@ -23,6 +25,16 @@ namespace {
 
 using Msp::Time::TimeZone;
 
+#ifndef WIN32
+long get_long(char *&ptr)
+{
+       long result=0;
+       for(unsigned i=0; i<4; ++i)
+               result=(result<<8)+static_cast<unsigned char >(*ptr++);
+       return result;
+}
+#endif
+
 TimeZone get_local_timezone()
 {
 #ifdef WIN32
@@ -39,8 +51,60 @@ TimeZone get_local_timezone()
 
        return TimeZone(offset);
 #else
-       tzset();
-       return TimeZone(timezone/60);
+       int fd=open("/etc/localtime", O_RDONLY);
+       if(fd>=-1)
+       {
+               char hdr[44];
+               int len=read(fd, hdr, sizeof(hdr));
+               long gmtoff=-1;
+               string name;
+               if(len==44 && hdr[0]=='T' && hdr[1]=='Z' && hdr[2]=='i' && hdr[3]=='f')
+               {
+                       char *ptr=hdr+20;
+                       long isgmtcnt=get_long(ptr);
+                       long isstdcnt=get_long(ptr);
+                       long leapcnt=get_long(ptr);
+                       long timecnt=get_long(ptr);
+                       long typecnt=get_long(ptr);
+                       long charcnt=get_long(ptr);
+                       int size=timecnt*5+typecnt*6+isgmtcnt+isstdcnt+leapcnt*8+charcnt;
+                       char buf[size];
+                       len=read(fd, buf, size);
+                       if(len==size)
+                       {
+                               ptr=buf;
+                               int index=-1;
+                               time_t cur_time=Msp::Time::now().to_unixtime();
+                               for(int i=0; i<timecnt; ++i)
+                                       if(get_long(ptr)<=cur_time)
+                                               index=i;
+
+                               if(index>0)
+                                       index=ptr[index];
+                               ptr+=timecnt;
+
+                               int abbrind=0;
+                               for(int i=0; i<typecnt; ++i)
+                               {
+                                       if((index>=0 && i==index) || (index<0 && !ptr[4] && gmtoff==-1))
+                                       {
+                                               gmtoff=get_long(ptr);
+                                               ++ptr;
+                                               abbrind=*ptr++;
+                                       }
+                                       else
+                                               ptr+=6;
+                               }
+
+                               name=ptr+abbrind;
+                       }
+               }
+               close(fd);
+
+               if(gmtoff!=-1)
+                       return TimeZone(-gmtoff/60, name);
+       }
+       return TimeZone();
 #endif
 }
 
@@ -69,6 +133,11 @@ TimeZone::TimeZone(int minutes_west):
                name="UTC";
 }
 
+TimeZone::TimeZone(int minutes_west, const string &n):
+       name(n),
+       offset(minutes_west*min)
+{ }
+
 const TimeZone &TimeZone::utc()
 {
        static TimeZone tz(0);
index 699801d47c6b2ead5fd8709f6f7bdc90bc39af72..1a71dd852e760590a4d1424bf011a880aa91e1df 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspcore
-Copyright © 2008  Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -22,6 +22,7 @@ private:
 public:
        TimeZone();
        TimeZone(int);
+       TimeZone(int, const std::string &);
        
        const std::string &get_name() const { return name; }
        const TimeDelta &get_offset() const { return offset; }