Repository navigation
Expand file tree
/
Copy pathTimerQueue.h
More file actions
303 lines (257 loc) · 11.7 KB
/
Copy pathTimerQueue.h
File metadata and controls
303 lines (257 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright (c) 2017 Ambroz Bizjak
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef AIPSTACK_TIMER_QUEUE_H
#define AIPSTACK_TIMER_QUEUE_H
#include <type_traits>
#include <aipstack/misc/Assert.h>
#include <aipstack/misc/Use.h>
#include <aipstack/misc/Hints.h>
#include <aipstack/misc/MinMax.h>
#include <aipstack/structure/TreeCompare.h>
#include <aipstack/structure/Accessor.h>
namespace AIpStack {
template<typename, typename, typename, typename, typename>
class TimerQueue;
template<
typename TimersStructureService,
typename LinkModel,
typename TimeType,
typename NodeUserData
>
class TimerQueueNode : public NodeUserData
{
template<typename, typename, typename, typename, typename>
friend class TimerQueue;
// Get the Node type for the data structure.
using TimersStructureNode = typename TimersStructureService::template Node<LinkModel>;
private:
// Node in the data structure.
TimersStructureNode timers_structure_node;
// Expiration time, relevant only if inserted to the data structure.
TimeType time;
};
template<
typename TimersStructureService,
typename LinkModel,
typename Accessor,
typename TimeType,
typename NodeUserData
>
class TimerQueue
{
// TimeType must be an unsigned integer type.
static_assert(std::is_arithmetic_v<TimeType>);
static_assert(std::is_unsigned_v<TimeType>);
// Get the State and Ref types from the link model.
AIPSTACK_USE_TYPES(LinkModel, (State, Ref))
// Get the TimerQueueNode type.
using Node = TimerQueueNode<TimersStructureService, LinkModel, TimeType, NodeUserData>;
// Create the accessor type for TimerQueueNode::timers_structure_node.
struct TimersStructureNodeAccessor : public ComposedAccessor<
Accessor, MemberAccessor<Node, typename Node::TimersStructureNode,
&Node::timers_structure_node>> {};
// Define the comparator for comparing timers, based on TreeCompare.
class KeyFuncs;
struct TimerCompare : public TreeCompare<LinkModel, KeyFuncs> {};
// Get the data structure type for finding the earliest timers (e.g. LinkedHeap).
using TimersStructure = typename TimersStructureService::template Structure<
TimersStructureNodeAccessor, TimerCompare, LinkModel>;
// Get the TimeType value for the most singificant bit.
// This is also exactly half of the TimeType type range.
inline static constexpr TimeType TimeMsb = (TypeMax<TimeType> / 2) + 1;
// Maximum time in the future (relative to the reference time) that
// getFirstTime returns. This limit avoids problems if we have timers
// expiring near the end of the future span of the reference time.
inline static constexpr TimeType TimeMaxFutureInterval = TimeMsb / 2 + TimeMsb / 4;
private:
// Data structure instance.
TimersStructure m_timers_structure;
// Time relative to which all timers in the data structure are in the future.
// That is, for all timers !time_less(timer_time, reference_time) holds.
// Generally undefined/irrelevant if the data structure is empty, except just
// after updateReferenceTime or prepareForRemovingExpired which is required
// before inserting timers.
TimeType m_referenece_time;
public:
void init ()
{
// Initialize the data structure.
m_timers_structure.init();
// The reference time is left uninitialized, we would not read it
// uninitialized if the API is used correctly.
}
void updateReferenceTime (TimeType now, State st = State())
{
// If the timers structure is empty, set the reference time to now,
// This is the optimal thing to in this case as we do not have any
// active timers to worry about.
if (m_timers_structure.isEmpty()) {
m_referenece_time = now;
}
// Otherwise, if the current time is in the past range of the reference
// time (due to a sudden clock jump), we cannot do anything.
// The next prepareForRemovingExpired will resolve the situation.
else if (time_less(now, m_referenece_time)) {
// Nothing.
}
// Otherwise, set the reference time to the smaller of the current time
// and the first timeout. Limiting the bump bump to the first timer prevents
// any timer from falling behind the reference time.
else {
Ref first_timer = m_timers_structure.first(st);
TimeType first_time = ac(first_timer).time;
m_referenece_time = time_less(now, first_time) ? now : first_time;
}
}
// NOTE: updateReferenceTime or prepareForRemovingExpiredmmust have been called
// soon before insert before any significant time could have passed (it is okay
// to call it once then do multiple insertions).
void insert (Ref entry, TimeType time, State st = State())
{
// If the time time is in the past relative to the reference time, bump
// it up to the reference time. This is needed because time comparisons
// work correctly only when the active timers span less than half of the
// TimeType range.
if (time_less(time, m_referenece_time)) {
time = m_referenece_time;
}
// Store the expiration time.
ac(entry).time = time;
// Insert the entry to the data structure.
m_timers_structure.insert(entry, st);
}
void remove (Ref entry, State st = State())
{
// Remove the entry from the data structure.
m_timers_structure.remove(entry, st);
}
void prepareForRemovingExpired (TimeType now, State st = State())
{
// Timer updates are only needed if there are any timers, and we should
// not read the reference time in this case as it may be uninitialized.
if (!m_timers_structure.isEmpty()) {
// Determine the time based on which we consider timers expired.
// The first case will dispatches all timers, after the clock jumped into
// past relative to the reference time, because in this case comparing
// timers to "now" the usual way may yield incorrect results.
TimeType dispatch_time;
if (AIPSTACK_UNLIKELY(time_less(now, m_referenece_time))) {
dispatch_time = m_referenece_time + (TimeMsb - 1);
} else {
dispatch_time = now;
}
// Set the time of expired timers to now. This allows us to safely update
// the reference time before dispatching any timer, and effectively allows
// new timers to be started in the full future relative to now.
// NOTE: While we are doing this, comparisons of timers to each other may
// return wrong results (in case we decided to dispatch all timers above).
// But the data structure must be designed so that it does not compare
// timers between each other in this process.
m_timers_structure.findAllLesserOrEqual(dispatch_time, [&](Ref entry) {
ac(entry).time = now;
}, st);
// Call assertValidHeap so the data structure can now verify
// self-consistency if configured to do so.
m_timers_structure.assertValidHeap(st);
}
// Update the reference time to 'now'. This is safe because the above
// updates ensured that all active timers (including ones about to be
// dispatched) belong to the future range relative to 'now'.
m_referenece_time = now;
}
// NOTE: prepareForRemovingExpired must be called before calling this.
// It is okay to call that once then call removeExpired multiple times
// (typically until null is returned).
Ref removeExpired (State st = State())
{
// Get the first timer if any.
Ref entry = m_timers_structure.first(st);
// If there are no timers or the first timer is not expired, return null.
if (entry.isNull() || ac(entry).time != m_referenece_time) {
return Ref::null();
}
// Remove the timer from the data structure.
m_timers_structure.remove(entry, st);
// Return the timer which was removed.
return entry;
}
inline bool getFirstTime (TimeType &out_time, State st = State())
{
// If there are no active timers, return false.
Ref entry = m_timers_structure.first(st);
if (entry.isNull()) {
return false;
}
// Get the expiration time of the earliest time.
TimeType time = ac(entry).time;
// If this is greater than TimeMaxFutureInterval after the reference time,
// bump it down to avoid returning times too far in the future.
TimeType max_time = m_referenece_time + TimeMaxFutureInterval;
if (time_less(max_time, time)) {
time = max_time;
}
// Return this time.
out_time = time;
return true;
}
private:
// Get the Node of an entry given by a Ref.
inline static Node & ac (Ref ref)
{
return Accessor::access(*ref);
}
// Compare to timers to determine if one is less than the other.
// Note that in a set of times, this only gives consistent results
// if the times span less than half of the TimeType range.
inline static bool time_less (TimeType time1, TimeType time2)
{
return TimeType(time1 - time2) >= TimeMsb;
}
class KeyFuncs
{
public:
// Get the key (timer time) of an entry.
template<typename EntryType>
static TimeType GetKeyOfEntry (EntryType &entry)
{
return Accessor::access(entry).time;
}
// Compare two keys (times).
static int CompareKeys (TimeType time1, TimeType time2)
{
return time_less(time1, time2) ? -1 : (time1 == time2) ? 0 : 1;
}
};
};
template<typename TimersStructureService>
struct TimerQueueService {
template<typename LinkModel, typename TimeType, typename NodeUserData>
using Node = TimerQueueNode<TimersStructureService, LinkModel, TimeType, NodeUserData>;
template<typename LinkModel, typename Accessor,
typename TimeType, typename NodeUserData>
using Queue = TimerQueue<TimersStructureService, LinkModel, Accessor,
TimeType, NodeUserData>;
};
}
#endif