Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
cyclic_timer.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5
6namespace mm::core {
7
36 public:
40 explicit CyclicTimer(std::chrono::microseconds period);
41
45
48 CyclicTimer(const CyclicTimer&) = delete;
51
74 uint64_t waitForNextCycle();
75
91 void setPeriod(std::chrono::microseconds period);
92
93 private:
94#ifdef _WIN32
95 void* handle_ = nullptr; // HANDLE — avoids pulling <windows.h> into every consumer
96 int64_t qpcFreq_ = 0; // QueryPerformanceCounter ticks per second
97 int64_t periodTicks_ = 0; // period expressed in QPC ticks
98 int64_t next_ = 0; // absolute deadline on the QPC (monotonic) timeline, in ticks
99#else
106 void advanceOnePeriod() {
107 next_nsec_ += period_ns_;
108 if (next_nsec_ >= 1'000'000'000L) {
109 next_nsec_ -= 1'000'000'000L;
110 next_sec_++;
111 }
112 }
113
114 int64_t period_ns_ = 0;
115 int64_t next_sec_ = 0;
116 int64_t next_nsec_ = 0;
117#endif
118};
119
120} // namespace mm::core
Precision cyclic timer for fixed-period loops.
Definition cyclic_timer.h:35
CyclicTimer & operator=(const CyclicTimer &)=delete
Copy assignment is deleted — see copy constructor.
CyclicTimer(const CyclicTimer &)=delete
Copying is deleted — each instance owns its own deadline state and, on Windows, an OS timer handle th...
~CyclicTimer()
Destroys the timer. On Windows, cancels and closes the OS timer handle. On Linux, no OS resource is h...
Definition cyclic_timer_windows.cc:36
void setPeriod(std::chrono::microseconds period)
Changes the cycle period and re-anchors the deadline grid to now.
Definition cyclic_timer_linux.cc:16
uint64_t waitForNextCycle()
Blocks until the next cycle deadline, then returns.
Definition cyclic_timer_linux.cc:24
Definition cyclic_timer.h:6