VLink  2.0.0
A high-performance communication middleware
timer.h File Reference

MessageLoop-driven periodic or one-shot timer with priority support. More...

#include <atomic>
#include <cstdint>
#include <memory>
#include "./functional.h"
#include "./macros.h"
Include dependency graph for timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  vlink::Timer
 Periodic or one-shot timer that fires on an associated MessageLoop thread. More...
 

Namespaces

 

Detailed Description

MessageLoop-driven periodic or one-shot timer with priority support.

vlink::Timer dispatches its callback on a MessageLoop thread, so the callback runs serially with every other task posted to the same loop and no additional synchronisation is required inside the callback body.

Timer modes determined by loop_count:

Mode loop_count value Behaviour
One-shot 1 Fires exactly once after one full interval
Counted 2 ... INT_MAX Fires the configured number of times
Repeating kInfinite (-1) Fires forever until stop() is called

Key features:

  • Strict mode: when the loop falls behind, missed ticks are dispatched immediately on the next iteration so the long-term cadence is preserved.
  • Configurable dispatch priority for use with kPriorityType message loops.
  • When interval_ms is 0 the internal interval clamps to kMinInterval (10000 ns = 10 us) to avoid pathological busy-spinning.
  • The static call_once() helper posts a fire-and-forget one-shot without managing a Timer object.
Lifecycle
Construct -> attach() (if not done at construction) -> start() -> repeated ticks -> stop() / restart() / destruction.
Example
vlink::Timer timer(&loop, 500, vlink::Timer::kInfinite, [] {
handle_tick();
});
timer.start();
loop.run();