VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
deadline_timer.h
浏览该文件的文档.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file deadline_timer.h
26 * @brief An absolute-deadline timer for lightweight, lock-free timeout tracking.
27 *
28 * @details
29 * @c DeadlineTimer stores an absolute expiry timestamp (rather than a countdown
30 * duration) in an atomic 64-bit word, making it safe to read from multiple threads
31 * without a mutex. It is used inside VLink connection and request handling to
32 * detect expired operations.
33 *
34 * The deadline can be set in two ways:
35 * - @c set_deadline(interval) -- sets the deadline @p interval time units from now.
36 * - @c set_deadline_abs(abs) -- sets an explicit absolute timestamp.
37 *
38 * The same @c Accuracy enum from @c ElapsedTimer is reused to select
39 * millisecond, microsecond, or nanosecond granularity.
40 *
41 * @note
42 * - A default-constructed @c DeadlineTimer has @c deadline() == 0 and
43 * @c is_valid() == false. Always call @c set_deadline() before
44 * checking @c has_expired().
45 * - @c remaining_time() returns 0 once the deadline has already passed; callers
46 * should treat any value <= 0 as expired.
47 * - The internal deadline is stored as a 64-bit atomic, so concurrent reads are
48 * safe. Concurrent writes from multiple threads are technically racy at the
49 * application level, but the store itself is atomic.
50 * - The timer is aligned to 64 bytes to prevent false sharing in structures
51 * that embed multiple timers.
52 *
53 * @par Example
54 * @code
55 * // Set a 200 ms deadline from now:
56 * vlink::DeadlineTimer t(200);
57 * while (!t.has_expired()) {
58 * process_events();
59 * }
60 * // Returns remaining ms (0 once expired):
61 * int64_t left = t.remaining_time();
62 *
63 * // Set an absolute deadline:
64 * uint64_t abs_ms = vlink::ElapsedTimer::get_cpu_timestamp();
65 * t.set_deadline_abs(abs_ms + 500);
66 * @endcode
67 */
68
69#pragma once
70
71#include <atomic>
72#include <cstdint>
73
74#include "./elapsed_timer.h"
75#include "./macros.h"
76
77namespace vlink {
78
79/**
80 * @class DeadlineTimer
81 * @brief Atomic absolute-deadline timer for lock-free timeout detection.
82 *
83 * @details
84 * Stores an absolute expiry time (monotonic CPU timestamp) in a 64-bit atomic.
85 * Concurrent @c has_expired() / @c remaining_time() reads from multiple threads
86 * are safe without external locking.
87 */
89 public:
90 /**
91 * @brief Time precision. Aliases @c ElapsedTimer::Accuracy for convenience.
92 */
94
95 /**
96 * @brief Default constructor. Creates an invalid (unset) deadline.
97 *
98 * @details
99 * @c is_valid() returns @c false and @c has_expired() returns @c false
100 * until @c set_deadline() or @c set_deadline_abs() is called.
101 */
102 DeadlineTimer() noexcept;
103
104 /**
105 * @brief Constructs a @c DeadlineTimer that expires @p interval time units from now.
106 *
107 * @details
108 * The absolute deadline is computed as the current monotonic timestamp plus
109 * @p interval. If @p interval <= 0 the timer is reset to the invalid state
110 * (equivalent to @c reset()).
111 *
112 * @param interval Duration until expiry, in @p accuracy units.
113 * @param accuracy Time precision (default: @c ElapsedTimer::kMilli).
114 */
115 explicit DeadlineTimer(int64_t interval, Accuracy accuracy = ElapsedTimer::kMilli) noexcept;
116
117 /**
118 * @brief Copy constructor.
119 *
120 * @param other The source timer.
121 */
122 DeadlineTimer(const DeadlineTimer& other) noexcept;
123
124 /**
125 * @brief Move constructor.
126 *
127 * @param other The source timer (moved from).
128 */
129 DeadlineTimer(DeadlineTimer&& other) noexcept;
130
131 /**
132 * @brief Destructor.
133 */
134 ~DeadlineTimer() noexcept;
135
136 /**
137 * @brief Copy-assignment operator.
138 *
139 * @param other The source timer.
140 * @return A reference to @c *this.
141 */
142 DeadlineTimer& operator=(const DeadlineTimer& other) noexcept;
143
144 /**
145 * @brief Move-assignment operator.
146 *
147 * @param other The source timer.
148 * @return A reference to @c *this.
149 */
150 DeadlineTimer& operator=(DeadlineTimer&& other) noexcept;
151
152 /**
153 * @brief Sets the deadline to @p interval time units from now.
154 *
155 * @details
156 * Reads the current monotonic timestamp and adds @p interval to produce the
157 * absolute deadline. A value of 0 or less clears the timer and makes it
158 * invalid.
159 *
160 * @param interval Duration until expiry in the configured accuracy units.
161 */
162 void set_deadline(int64_t interval) noexcept;
163
164 /**
165 * @brief Sets an explicit absolute deadline timestamp.
166 *
167 * @details
168 * The value must be in the same unit and time base as the @c Accuracy
169 * configured for this timer. Typically obtain it via
170 * @c ElapsedTimer::get_cpu_timestamp(accuracy).
171 *
172 * @param abs_deadline Absolute monotonic timestamp of the deadline.
173 */
174 void set_deadline_abs(uint64_t abs_deadline) noexcept;
175
176 /**
177 * @brief Resets the deadline to 0, making the timer invalid again.
178 *
179 * @details
180 * After @c reset(), @c is_valid() returns @c false.
181 */
182 void reset() noexcept;
183
184 /**
185 * @brief Returns the stored absolute deadline timestamp.
186 *
187 * @details
188 * A value of 0 means the timer has not been set (see @c is_valid()).
189 *
190 * @return Absolute deadline in the configured accuracy units.
191 */
192 [[nodiscard]] uint64_t deadline() const noexcept;
193
194 /**
195 * @brief Returns the time remaining until the deadline.
196 *
197 * @details
198 * Computed as @c (deadline - current_cpu_timestamp). Returns 0 when the
199 * timer is invalid or already expired.
200 *
201 * @return Remaining time in the configured accuracy units.
202 * A return value of 0 means invalid or expired.
203 */
204 [[nodiscard]] int64_t remaining_time() const noexcept;
205
206 /**
207 * @brief Returns @c true if the current time is past the stored deadline.
208 *
209 * @details
210 * Equivalent to @c remaining_time() <= 0 when @c is_valid() is @c true.
211 * Always returns @c false for an unset (invalid) timer.
212 *
213 * @return @c true if expired, @c false otherwise.
214 */
215 [[nodiscard]] bool has_expired() const noexcept;
216
217 /**
218 * @brief Returns @c true if the deadline has been explicitly set (non-zero).
219 *
220 * @return @c true if the timer is valid (deadline != 0).
221 */
222 [[nodiscard]] bool is_valid() const noexcept;
223
224 /**
225 * @brief Returns the time precision configured for this timer.
226 *
227 * @return The @c Accuracy value set at construction.
228 */
229 [[nodiscard]] Accuracy get_accuracy() const noexcept;
230
231 private:
232 alignas(64) std::atomic<uint64_t> deadline_{0};
233 Accuracy accuracy_{ElapsedTimer::kMilli};
234};
235
236} // namespace vlink
High-resolution elapsed-time measurement with configurable clock source and precision.
Platform-independent macro definitions for the VLink library.
#define VLINK_EXPORT
定义 macros.h:85
STL namespace