25 #ifndef __I6ENGINE_UTILS_CLOCK_H__
26 #define __I6ENGINE_UTILS_CLOCK_H__
31 #include <condition_variable>
34 #include "boost/bind.hpp"
39 template<
class Updater>
57 for (
size_t i = 0; i < _timer.size(); i++) {
58 _timer[i].second->notify_all();
59 delete _timer[i].second;
72 _systemTime = Updater::getCurrentTime(_systemTime);
88 std::pair<uint64_t, std::condition_variable *> p(std::make_pair(UINT64_MAX,
new std::condition_variable()));
90 std::lock_guard<std::mutex> lock(_lock);
93 return _timer.size() - 1;
102 std::lock_guard<std::mutex> lock(_lock);
103 _timer[timerID].first = UINT64_MAX;
110 if (time <= _systemTime) {
111 std::lock_guard<std::mutex> lock(_lock);
112 std::condition_variable * cond = _timer[size_t(timerID)].second;
116 std::lock_guard<std::mutex> lock(_lock);
117 _timer[size_t(timerID)].first = time;
124 if (time <= _systemTime) {
127 std::unique_lock<std::mutex> lock(_lock);
128 _timer[size_t(timerID)].first = time;
129 _timer[size_t(timerID)].second->wait(lock);
142 std::vector<std::pair<uint64_t, std::condition_variable *>> _timer;
147 mutable std::mutex _lock;
150 std::atomic<uint64_t> _systemTime;
152 std::atomic<bool> _running;
158 std::unique_lock<std::mutex> lock(_lock);
159 for (
size_t i = 0; i < _timer.size(); i++) {
160 if (_systemTime >= _timer[i].first) {
161 _timer[i].first = UINT64_MAX;
162 _timer[i].second->notify_all();
void updateWaitTime(uint64_t timerID, uint64_t time)
updates the time a timer is waiting for
void unregisterTimer(uint64_t timerID)
removes the given timer
uint64_t registerTimer()
registers a new timer waiting for this clock
bool waitForTime(uint64_t timerID, uint64_t time)
let's a timer wait for the given time
uint64_t getTime() const
Will return the time since the Clock has been started.
void Update()
updates current time and notifies timers, called from outside This function tells the clock to update...
Clock()
default constructor
~Clock()
stops the updating of the clock, removes all timers
bool isRunning() const
returns state of the Clock