/* $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"
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;
}
{
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;
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;
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
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
/* $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"
bool operator<(const SlotProxy &) const;
};
- std::set<SlotProxy> slots;
+ std::vector<SlotProxy> slots;
Semaphore sem;
Mutex mutex;
/* $Id$
This file is part of libmspcore
-Copyright © 2006 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006, 2009 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
*/
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()); }
/* $Id$
This file is part of libmspcore
-Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009 Mikko Rasa, Mikkosoft Productions
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;
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
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
}
name="UTC";
}
+TimeZone::TimeZone(int minutes_west, const string &n):
+ name(n),
+ offset(minutes_west*min)
+{ }
+
const TimeZone &TimeZone::utc()
{
static TimeZone tz(0);
/* $Id$
This file is part of libmspcore
-Copyright © 2008 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008-2009 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
public:
TimeZone();
TimeZone(int);
+ TimeZone(int, const std::string &);
const std::string &get_name() const { return name; }
const TimeDelta &get_offset() const { return offset; }