VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
deadline_timer.h 文件参考

An absolute-deadline timer for lightweight, lock-free timeout tracking. 更多...

#include <atomic>
#include <cstdint>
#include "./elapsed_timer.h"
#include "./macros.h"
deadline_timer.h 的引用(Include)关系图:

浏览该文件的源代码.

 Atomic absolute-deadline timer for lock-free timeout detection. 更多...

命名空间

详细描述

An absolute-deadline timer for lightweight, lock-free timeout tracking.

DeadlineTimer stores an absolute expiry timestamp (rather than a countdown duration) in an atomic 64-bit word, making it safe to read from multiple threads without a mutex. It is used inside VLink connection and request handling to detect expired operations.

The deadline can be set in two ways:

  • set_deadline(interval) – sets the deadline interval time units from now.
  • set_deadline_abs(abs) – sets an explicit absolute timestamp.

The same Accuracy enum from ElapsedTimer is reused to select millisecond, microsecond, or nanosecond granularity.

注解
  • A default-constructed DeadlineTimer has deadline() == 0 and is_valid() == false. Always call set_deadline() before checking has_expired().
  • remaining_time() returns 0 once the deadline has already passed; callers should treat any value <= 0 as expired.
  • The internal deadline is stored as a 64-bit atomic, so concurrent reads are safe. Concurrent writes from multiple threads are technically racy at the application level, but the store itself is atomic.
  • The timer is aligned to 64 bytes to prevent false sharing in structures that embed multiple timers.
Example
// Set a 200 ms deadline from now:
while (!t.has_expired()) {
process_events();
}
// Returns remaining ms (0 once expired):
int64_t left = t.remaining_time();
// Set an absolute deadline:
t.set_deadline_abs(abs_ms + 500);