Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
game_loop.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <cstdint>
6#include <nlohmann/json_fwd.hpp>
7#include <vector>
8
9#include "core/cyclic_task.h"
10
15 uint64_t periodUs = 0;
16 double targetHz = 0.0;
17 double achievedHz = 0.0;
18 uint64_t executedCycles = 0;
19 uint64_t skippedCycles = 0;
20 uint64_t lastExecNs = 0;
21 uint64_t maxExecNs = 0;
22 uint64_t avgExecNs = 0;
23 bool schedFifo = false;
24 bool memLocked = false;
25 uint64_t timestampUs = 0;
26};
27
29void to_json(nlohmann::json& j, const GameLoopHealth& h);
30
44class GameLoop {
45 public:
48 explicit GameLoop(std::chrono::microseconds period);
49
52 ~GameLoop() = default;
53
56 GameLoop(const GameLoop&) = delete;
58 GameLoop& operator=(const GameLoop&) = delete;
59
68 void addTask(CyclicTask* task);
69
80 void run();
81
85 void stop();
86
107 void setPeriod(std::chrono::microseconds period);
108
119 uint64_t executedCycles() const;
120
133 uint64_t skippedCycles() const;
134
140 GameLoopHealth health() const;
141
142 private:
143 // Cycle period; read by the RT loop each iteration, written by setPeriod from another thread.
144 // std::chrono::microseconds is an 8-byte trivially-copyable type, so this atomic is lock-free.
145 std::atomic<std::chrono::microseconds> period_;
146 std::atomic<bool> running_{false};
147 std::atomic<uint64_t> executedCycles_{0};
148 std::atomic<uint64_t> skippedCycles_{0};
149 std::atomic<uint64_t> lastExecNs_{0}; // most recent cycle's task-execution time
150 std::atomic<uint64_t> maxExecNs_{0}; // worst task-execution time since run()
151 std::atomic<uint64_t> sumExecNs_{0}; // running sum → avg = sum / executedCycles
152 std::atomic<uint64_t> startMonoNs_{0}; // steady_clock ns at run() start; 0 until then
153 std::atomic<bool> schedFifo_{false}; // SCHED_FIFO acquired in run()
154 std::atomic<bool> memLocked_{false}; // mlockall succeeded in run()
155 std::vector<CyclicTask*> tasks_;
156};
Interface for work executed once per game loop cycle.
Definition cyclic_task.h:42
Fixed-period real-time loop.
Definition game_loop.h:44
GameLoopHealth health() const
Returns a snapshot of real-time loop health for diagnostics.
Definition game_loop.cc:147
uint64_t skippedCycles() const
Returns the total number of cycles skipped since run() was called.
Definition game_loop.cc:145
~GameLoop()=default
Destructor. Does not call stop() — the caller is responsible for stopping the loop before destroying ...
uint64_t executedCycles() const
Returns the number of cycles actually executed since run() was called.
Definition game_loop.cc:141
GameLoop(const GameLoop &)=delete
Copying is deleted — the loop owns real-time scheduling state that cannot be shared.
void run()
Blocks the calling thread, running one cycle per period until stop() is called.
Definition game_loop.cc:68
void addTask(CyclicTask *task)
Registers a task to be executed every cycle.
Definition game_loop.cc:66
GameLoop & operator=(const GameLoop &)=delete
Copy assignment is deleted — see copy constructor.
void setPeriod(std::chrono::microseconds period)
Changes the cycle period while the loop is running.
Definition game_loop.cc:137
void stop()
Signals the loop to stop after the current cycle completes.
Definition game_loop.cc:135
void to_json(nlohmann::json &j, const GameLoopHealth &h)
Serializes a GameLoopHealth to JSON (found via ADL by nlohmann::json).
Definition game_loop.cc:170
Snapshot of game-loop real-time health for the GET /api/game-loop diagnostic endpoint....
Definition game_loop.h:14
bool schedFifo
SCHED_FIFO acquired (false on Windows / failure).
Definition game_loop.h:23
uint64_t avgExecNs
Mean task-exec time since run() (ns).
Definition game_loop.h:22
uint64_t periodUs
Configured target period (µs).
Definition game_loop.h:15
double targetHz
Target rate = 1e6 / periodUs.
Definition game_loop.h:16
uint64_t timestampUs
Server stamp (epoch µs) for exact client Δt.
Definition game_loop.h:25
uint64_t lastExecNs
Task-exec time of the most recent cycle (ns).
Definition game_loop.h:20
double achievedHz
Cumulative avg = executedCycles / uptime; 0 pre-run.
Definition game_loop.h:17
uint64_t skippedCycles
Cycles skipped to catch up since run().
Definition game_loop.h:19
uint64_t maxExecNs
Worst task-exec time since run() (ns).
Definition game_loop.h:21
bool memLocked
mlockall ok (Linux only; false elsewhere).
Definition game_loop.h:24
uint64_t executedCycles
Loop iterations run since run().
Definition game_loop.h:18