Motion Master 6.0.0-alpha.86
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
44 // Trivially destructible only in the build being analysed: the Windows implementation closes the
45 // waitable-timer handle here, so defaulting this would leak a kernel object on the one platform
46 // that owns one.
47 // NOLINTNEXTLINE(performance-trivially-destructible)
49
52 CyclicTimer(const CyclicTimer&) = delete;
55
78 uint64_t waitForNextCycle();
79
95 void setPeriod(std::chrono::microseconds period);
96
97 private:
98#ifdef _WIN32
99 void* handle_ = nullptr; // HANDLE — avoids pulling <windows.h> into every consumer
100 int64_t qpcFreq_ = 0; // QueryPerformanceCounter ticks per second
101 int64_t periodTicks_ = 0; // period expressed in QPC ticks
102 int64_t next_ = 0; // absolute deadline on the QPC (monotonic) timeline, in ticks
103#else
110 void advanceOnePeriod() {
111 next_nsec_ += period_ns_;
112 if (next_nsec_ >= 1'000'000'000L) {
113 next_nsec_ -= 1'000'000'000L;
114 next_sec_++;
115 }
116 }
117
118 int64_t period_ns_ = 0;
119 int64_t next_sec_ = 0;
120 int64_t next_nsec_ = 0;
121#endif
122};
123
124} // 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 http_server.h:25