Motion Master
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <mutex>
6#include <thread>
7
8#ifdef _WIN32
9#include <windows.h> // Must be included before mmsystem.h for Windows multimedia timer
10#endif
11#ifdef __linux__
12#include <sys/timerfd.h> // Linux timerfd
13#include <unistd.h> // read, close
14#endif
15
16#ifdef _WIN32
17#include <mmsystem.h> // Windows multimedia system for timeSetEvent
18#endif
19
20#include <condition_variable>
21
22namespace mm::core::timer {
23
36class MainTimer {
37 public:
41 static unsigned int DELAY_MS;
42
51 static std::mutex listenerMx;
52
61 static std::condition_variable listenerCv;
62
71 static MainTimer& getInstance();
72
79 static void notifyAllListeners();
80
87 void wait();
88
97 uint64_t getTickCount() const;
98
106 uint64_t getCycleCount() const;
107
115 uint64_t getCycleMissedTickCount() const;
116
124 int64_t getElapsedTime() const;
125
132 int64_t getWaitIntervalUs() const;
133
134 private:
136 std::mutex mx_;
137
139 std::condition_variable cv_;
140
142 unsigned int timerID_;
143
145 bool timerTick_;
146
148 std::atomic<uint64_t> tickCount_;
149
151 std::atomic<uint64_t> cycleCount_;
152
154 std::atomic<uint64_t> prevTickCount_;
155
157 std::atomic<uint64_t> cycleMissedTickCount_;
158
160 int64_t start_;
161
163 std::chrono::steady_clock::time_point lastWaitTime_{};
164
166 std::atomic<int64_t> waitIntervalUs_{0};
167
174 MainTimer();
175
179 ~MainTimer();
180
182 MainTimer(const MainTimer&) = delete;
183
185 MainTimer& operator=(const MainTimer&) = delete;
186
187#ifdef _WIN32
199 static void CALLBACK TimerCallback(unsigned int uID, unsigned int uMsg,
200 DWORD_PTR dwUser, DWORD_PTR dw1,
201 DWORD_PTR dw2);
202#endif
203#ifdef __linux__
205 int timerfd_{-1};
206
208 std::thread timerThread_;
209
211 std::atomic<bool> timerRunning_{false};
212#endif
213
219 void onTimerTick();
220};
221
233 public:
241
249 void wait();
250
259 uint64_t getTickCount() const;
260
269 uint64_t getCycleCount() const;
270
278 int64_t getElapsedTime() const;
279
280 private:
282 std::atomic<uint64_t> tickCount_;
283
285 std::atomic<uint64_t> refTickCount_;
286
288 std::atomic<uint64_t> cycleCount_;
289
291 int64_t start_;
292};
293
294} // namespace mm::core::timer
void wait()
Waits for the next timer tick and updates internal state.
Definition timer.cc:153
MainTimerListener()
Constructs a new MainTimerListener instance.
Definition timer.cc:145
uint64_t getCycleCount() const
Returns the number of completed wait cycles.
Definition timer.cc:193
uint64_t getTickCount() const
Returns the number of ticks since the first tick received.
Definition timer.cc:191
int64_t getElapsedTime() const
Returns the total elapsed time since the listener was created.
Definition timer.cc:195
uint64_t getCycleMissedTickCount() const
Returns the number of missed timer ticks in the current cycle.
Definition timer.cc:132
static void notifyAllListeners()
Notifies all listeners (waiting threads) about a timer tick.
Definition timer.cc:15
static std::mutex listenerMx
Static mutex protecting the static condition variable for notifying all listener threads.
Definition timer.h:51
int64_t getElapsedTime() const
Returns the total elapsed time since the timer started.
Definition timer.cc:136
uint64_t getTickCount() const
Returns the current tick count.
Definition timer.cc:128
static MainTimer & getInstance()
Returns the singleton instance of the MainTimer.
Definition timer.cc:10
int64_t getWaitIntervalUs() const
Returns the interval in microseconds between two consecutive wait() calls.
Definition timer.cc:143
static unsigned int DELAY_MS
Static delay in milliseconds between each timer tick.
Definition timer.h:41
void wait()
Waits for the next timer tick to occur.
Definition timer.cc:86
static std::condition_variable listenerCv
Static condition variable used to notify all listener threads of timer ticks.
Definition timer.h:61
uint64_t getCycleCount() const
Returns the number of completed wait cycles.
Definition timer.cc:130
Definition timer.cc:3