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

Atomic absolute-deadline timer used for lock-free timeout tracking on hot paths. More...

#include <atomic>
#include <cstdint>
#include "./elapsed_timer.h"
#include "./macros.h"
Include dependency graph for deadline_timer.h:

Go to the source code of this file.

Classes

class  vlink::DeadlineTimer
 Cache-line aligned atomic deadline counter with millisecond / microsecond / nanosecond precision. More...
 

Namespaces

 

Detailed Description

Atomic absolute-deadline timer used for lock-free timeout tracking on hot paths.

DeadlineTimer stores a single absolute monotonic timestamp inside a 64-bit atomic, which makes deadline checks branch-light and concurrent reads safe without external locking. It is used inside connection bookkeeping and RPC request tracking to detect expired operations.

State diagram
*   +--------+ set_deadline / set_deadline_abs +---------+ now >= deadline +----------+
*   |  idle  | ------------------------------> |  armed  | --------------> | expired  |
*   | (zero) |                                 |         |                 +----------+
*   +--------+ <------- reset() --------------- +---------+                       |
*      ^                                                                          |
*      +-----------------------  reset() / set 0 --------------------------------+
* 
Comparison vs the periodic vlink::Timer
Aspect vlink::DeadlineTimer vlink::Timer
Purpose Track a single deadline Schedule repeating callbacks
Backing storage One atomic<uint64_t> Loop-managed timer list
Owns a thread No Owned by an attached MessageLoop
Cost per check One atomic load Insertion / removal on the loop
Cancellation reset() detach() on the timer instance
Example
vlink::DeadlineTimer t(200); // 200 ms from now
while (!t.has_expired()) {
process_events();
}
const int64_t left = t.remaining_time(); // 0 once past deadline
t.set_deadline_abs(now + 500); // absolute reset