22 #ifndef __M2ETIS_UTIL_CLOCK_H__
23 #define __M2ETIS_UTIL_CLOCK_H__
30 #include "boost/thread.hpp"
38 template<
class Updater>
44 Clock() : Updater(
boost::bind(&
Clock::Update, this)), timer_(), lock_(), usedIds_(0), systemTime_(0), running_(true), offset_(0) {
54 boost::mutex::scoped_lock l(lock_);
55 for (std::map<uint64_t, std::pair<uint64_t, boost::condition_variable *>>::iterator it = timer_.begin(); it != timer_.end(); ++it) {
56 it->second.second->notify_all();
58 delete it->second.second;
69 return systemTime_ + offset_;
85 std::pair<uint64_t, boost::condition_variable *> p(std::make_pair(UINT64_MAX,
new boost::condition_variable()));
87 boost::mutex::scoped_lock lock(lock_);
88 uint64_t
id = usedIds_++;
98 boost::mutex::scoped_lock lock(lock_);
99 delete timer_[timerID].second;
100 timer_.erase(timerID);
107 boost::mutex::scoped_lock lock(lock_);
108 if (time <= systemTime_ + offset_) {
109 timer_[timerID].second->notify_all();
112 timer_[timerID].first = time;
122 boost::mutex::scoped_lock lock(lock_);
123 if (time <= systemTime_ + offset_) {
126 timer_[timerID].first = time;
127 timer_[timerID].second->wait(lock);
147 std::map<uint64_t, std::pair<uint64_t, boost::condition_variable *>> timer_;
153 volatile uint64_t systemTime_;
155 volatile bool running_;
157 volatile uint64_t offset_;
164 boost::mutex::scoped_lock sl(lock_);
165 systemTime_ = Updater::getCurrentTime(systemTime_);
170 for (std::map<uint64_t, std::pair<uint64_t, boost::condition_variable *>>::iterator it = timer_.begin(); it != timer_.end(); ++it) {
171 if (systemTime_ + offset_ >= it->second.first) {
172 it->second.first = UINT64_MAX;
173 it->second.second->notify_all();
Clock for time handling, Updater is the implementation of the time, like a RealTimeClock measuring re...
void setOffset(uint64_t offset)
sets offset of this Clock to adjust times over network
bool isRunning()
gets state of the Clock
void updateWaitTime(uint64_t timerID, uint64_t time)
updates the time a given timer has to wait
void unregisterTimer(uint64_t timerID)
frees a timer ID
Clock()
initializes Clock and propagates Update method to Updater
uint64_t registerTimer()
returns a new ID for a timer
uint64_t getRealTime() const
Will return the real time since the Clock has been started.
uint64_t getTime() const
Will return the time since the Clock on the rendezvouz node has started.
bool waitForTime(uint64_t timerID, uint64_t time)
timer with timerID will wait for time and then be unlocked again