6#include <nlohmann/json_fwd.hpp>
48 explicit GameLoop(std::chrono::microseconds period);
107 void setPeriod(std::chrono::microseconds period);
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};
150 std::atomic<uint64_t> maxExecNs_{0};
151 std::atomic<uint64_t> sumExecNs_{0};
152 std::atomic<uint64_t> startMonoNs_{0};
153 std::atomic<bool> schedFifo_{
false};
154 std::atomic<bool> memLocked_{
false};
155 std::vector<CyclicTask*> tasks_;
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