i6engine  1.0
Clock.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_CLOCK_H__
26 #define __I6ENGINE_UTILS_CLOCK_H__
27 
28 #include <atomic>
29 #include <cassert>
30 #include <climits>
31 #include <condition_variable>
32 #include <vector>
33 
34 #include "boost/bind.hpp"
35 
36 namespace i6e {
37 namespace utils {
38 
39  template<class Updater>
40  class Clock : public Updater {
41  public:
45  Clock() : Updater(boost::bind(&Clock::Update, this)), _timer(), _lock(), _systemTime(0), _running(true) {
46  Updater::Init();
47  }
48 
54  ~Clock() {
55  _running = false;
56  Updater::Stop();
57  for (size_t i = 0; i < _timer.size(); i++) {
58  _timer[i].second->notify_all();
59  delete _timer[i].second;
60  }
61  _timer.clear();
62  }
63 
70  void Update() {
71  // Get current time.
72  _systemTime = Updater::getCurrentTime(_systemTime);
73  notifyTimer();
74  }
75 
80  inline uint64_t getTime() const {
81  return _systemTime;
82  }
83 
87  uint64_t registerTimer() {
88  std::pair<uint64_t, std::condition_variable *> p(std::make_pair(UINT64_MAX, new std::condition_variable()));
89 
90  std::lock_guard<std::mutex> lock(_lock);
91  _timer.push_back(p);
92 
93  return _timer.size() - 1;
94  }
95 
101  void unregisterTimer(uint64_t timerID) {
102  std::lock_guard<std::mutex> lock(_lock);
103  _timer[timerID].first = UINT64_MAX;
104  }
105 
109  void updateWaitTime(uint64_t timerID, uint64_t time) {
110  if (time <= _systemTime) {
111  std::lock_guard<std::mutex> lock(_lock);
112  std::condition_variable * cond = _timer[size_t(timerID)].second;
113  cond->notify_all();
114  return;
115  }
116  std::lock_guard<std::mutex> lock(_lock);
117  _timer[size_t(timerID)].first = time;
118  }
119 
123  bool waitForTime(uint64_t timerID, uint64_t time) {
124  if (time <= _systemTime) {
125  return true;
126  }
127  std::unique_lock<std::mutex> lock(_lock);
128  _timer[size_t(timerID)].first = time;
129  _timer[size_t(timerID)].second->wait(lock);
130  return _running;
131  }
132 
136  bool isRunning() const {
137  return _running;
138  }
139 
140  private:
141  // wakeuptime variable
142  std::vector<std::pair<uint64_t, std::condition_variable *>> _timer;
143 
147  mutable std::mutex _lock;
148 
149  // last system time
150  std::atomic<uint64_t> _systemTime;
151 
152  std::atomic<bool> _running;
153 
157  void notifyTimer() {
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();
163  }
164  }
165  }
166 
170  Clock(const Clock &) = delete;
171  Clock & operator=(const Clock &) = delete;
172  };
173 
174 } /* namespace utils */
175 } /* namespace i6e */
176 
177 #endif /* __I6ENGINE_UTILS_CLOCK_H__ */
178 
void updateWaitTime(uint64_t timerID, uint64_t time)
updates the time a timer is waiting for
Definition: Clock.h:109
void unregisterTimer(uint64_t timerID)
removes the given timer
Definition: Clock.h:101
uint64_t registerTimer()
registers a new timer waiting for this clock
Definition: Clock.h:87
bool waitForTime(uint64_t timerID, uint64_t time)
let's a timer wait for the given time
Definition: Clock.h:123
uint64_t getTime() const
Will return the time since the Clock has been started.
Definition: Clock.h:80
void Update()
updates current time and notifies timers, called from outside This function tells the clock to update...
Definition: Clock.h:70
Clock()
default constructor
Definition: Clock.h:45
~Clock()
stops the updating of the clock, removes all timers
Definition: Clock.h:54
bool isRunning() const
returns state of the Clock
Definition: Clock.h:136