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
7#ifdef _WIN32
8#include <windows.h> // Must be included before mmsystem.h for Windows multimedia timer
9#endif
10#ifdef __linux__
11#include <signal.h> // POSIX signal handling
12#include <time.h> // POSIX timer and time functions
13#endif
14
15#ifdef _WIN32
16#include <mmsystem.h> // Windows multimedia system for timeSetEvent
17#endif
18
19#include <condition_variable>
20
21namespace mm::core::timer {
22
35class MainTimer {
36 public:
40 static unsigned int DELAY_MS;
41
50 static std::mutex listenerMx;
51
60 static std::condition_variable listenerCv;
61
70 static MainTimer& getInstance();
71
78 static void notifyAllListeners();
79
86 void wait();
87
96 uint64_t getTickCount() const;
97
105 uint64_t getCycleCount() const;
106
114 uint64_t getCycleMissedTickCount() const;
115
123 int64_t getElapsedTime() const;
124
131 int64_t getWaitIntervalUs() const;
132
133 private:
135 std::mutex mx_;
136
138 std::condition_variable cv_;
139
141 unsigned int timerID_;
142
144 bool timerTick_;
145
147 std::atomic<uint64_t> tickCount_;
148
150 std::atomic<uint64_t> cycleCount_;
151
153 std::atomic<uint64_t> prevTickCount_;
154
156 std::atomic<uint64_t> cycleMissedTickCount_;
157
159 int64_t start_;
160
162 std::chrono::steady_clock::time_point lastWaitTime_{};
163
165 std::atomic<int64_t> waitIntervalUs_{0};
166
173 MainTimer();
174
178 ~MainTimer();
179
181 MainTimer(const MainTimer&) = delete;
182
184 MainTimer& operator=(const MainTimer&) = delete;
185
186#ifdef _WIN32
198 static void CALLBACK TimerCallback(unsigned int uID, unsigned int uMsg,
199 DWORD_PTR dwUser, DWORD_PTR dw1,
200 DWORD_PTR dw2);
201#endif
202#ifdef __linux__
204 timer_t timerid_;
205
213 static void timer_handler(int sig);
214#endif
215
221 void onTimerTick();
222};
223
235 public:
243
251 void wait();
252
261 uint64_t getTickCount() const;
262
271 uint64_t getCycleCount() const;
272
280 int64_t getElapsedTime() const;
281
282 private:
284 std::atomic<uint64_t> tickCount_;
285
287 std::atomic<uint64_t> refTickCount_;
288
290 std::atomic<uint64_t> cycleCount_;
291
293 int64_t start_;
294};
295
296} // namespace mm::core::timer
Listener for periodic ticks from a MainTimer.
Definition: timer.h:234
void wait()
Waits for the next timer tick and updates internal state.
Definition: timer.cc:158
MainTimerListener()
Constructs a new MainTimerListener instance.
Definition: timer.cc:150
uint64_t getCycleCount() const
Returns the number of completed wait cycles.
Definition: timer.cc:198
uint64_t getTickCount() const
Returns the number of ticks since the first tick received.
Definition: timer.cc:196
int64_t getElapsedTime() const
Returns the total elapsed time since the listener was created.
Definition: timer.cc:200
Cross-platform high-resolution periodic timer.
Definition: timer.h:35
uint64_t getCycleMissedTickCount() const
Returns the number of missed timer ticks in the current cycle.
Definition: timer.cc:137
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:50
int64_t getElapsedTime() const
Returns the total elapsed time since the timer started.
Definition: timer.cc:141
uint64_t getTickCount() const
Returns the current tick count.
Definition: timer.cc:133
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:148
static unsigned int DELAY_MS
Static delay in milliseconds between each timer tick.
Definition: timer.h:40
void wait()
Waits for the next timer tick to occur.
Definition: timer.cc:91
static std::condition_variable listenerCv
Static condition variable used to notify all listener threads of timer ticks.
Definition: timer.h:60
uint64_t getCycleCount() const
Returns the number of completed wait cycles.
Definition: timer.cc:135
Definition: timer.cc:3