From 5889d53f8f073ff0e1e1ebbd786abecd8352a7b3 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 11 Sep 2009 17:32:58 +0000 Subject: [PATCH] Rewrite Timer to use a heap instead of set to deal with duplicate timeouts Get local timezone from /etc/localtime instead of tzset() Add TimeStamp::to_unixtime --- source/time/timer.cpp | 73 +++++++++++++++++++++++++------------ source/time/timer.h | 6 ++-- source/time/timestamp.h | 4 ++- source/time/timezone.cpp | 77 +++++++++++++++++++++++++++++++++++++--- source/time/timezone.h | 3 +- 5 files changed, 132 insertions(+), 31 deletions(-) diff --git a/source/time/timer.cpp b/source/time/timer.cpp index c6e546f..9471463 100644 --- a/source/time/timer.cpp +++ b/source/time/timer.cpp @@ -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 #include "timer.h" #include "utils.h" @@ -15,7 +16,7 @@ namespace Time { Timer::~Timer() { - for(set::iterator i=slots.begin(); i!=slots.end(); ++i) + for(vector::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::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()get_timeout(); + return slot->get_timeout()>sp.slot->get_timeout(); } } // namespace Time diff --git a/source/time/timer.h b/source/time/timer.h index 935df03..a0dabcd 100644 --- a/source/time/timer.h +++ b/source/time/timer.h @@ -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 +#include #include #include "../core/mutex.h" #include "../core/semaphore.h" @@ -53,7 +53,7 @@ private: bool operator<(const SlotProxy &) const; }; - std::set slots; + std::vector slots; Semaphore sem; Mutex mutex; diff --git a/source/time/timestamp.h b/source/time/timestamp.h index faa200c..db3587a 100644 --- a/source/time/timestamp.h +++ b/source/time/timestamp.h @@ -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()); } diff --git a/source/time/timezone.cpp b/source/time/timezone.cpp index 541514f..3910c24 100644 --- a/source/time/timezone.cpp +++ b/source/time/timezone.cpp @@ -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 #else -#include +#include #endif #include +#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(*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; i0) + index=ptr[index]; + ptr+=timecnt; + + int abbrind=0; + for(int i=0; i=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); diff --git a/source/time/timezone.h b/source/time/timezone.h index 699801d..1a71dd8 100644 --- a/source/time/timezone.h +++ b/source/time/timezone.h @@ -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; } -- 2.45.2