-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerQueue.cpp
More file actions
288 lines (246 loc) · 9.39 KB
/
Copy pathTimerQueue.cpp
File metadata and controls
288 lines (246 loc) · 9.39 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
#include <muduo/TimerQueue.h>
#include <muduo/Timer.h>
#include <memory>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <sys/timerfd.h>
using namespace muduo;
class TimerQueue::TimerMinHeap {
// non-copyable & non-moveable
TimerMinHeap(const TimerMinHeap&) = delete;
TimerMinHeap operator=(const TimerMinHeap) = delete;
#ifdef MUDUO_USE_MEMPOOL
private:
using TimerList = std::vector<std::unique_ptr<Timer>, base::allocator<std::unique_ptr<Timer>>>;
/// The index in @c TimerList of Timer, key=detail::TimerId_t value=index
using TimerMap = std::unordered_map<detail::TimerId_t, std::size_t, std::hash<detail::TimerId_t>, std::equal_to<detail::TimerId_t>, base::allocator<std::pair<const detail::TimerId_t, size_t>>>;
#else
private:
using TimerList = std::vector<std::unique_ptr<Timer>>;
using TimerMap = std::unordered_map<detail::TimerId_t, std::size_t>;
#endif
public:
TimerMinHeap(TimerQueue* q)
: owner_(q)
#ifdef MUDUO_USE_MEMPOOL
, timerList_(owner_->Owner()->GetMemoryPool())
, positions_(owner_->Owner()->GetMemoryPool())
#endif
{ }
/**
* @return Whether need update timerfd expiration-TimePoint
*/
bool Add(std::unique_ptr<Timer>&& t_p) {
detail::TimerId_t id = t_p->GetId();
std::size_t idx = timerList_.size();
timerList_.push_back(std::move(t_p));
positions_[id] = idx;
assert(timerList_.size() == positions_.size());
if (idx == 0) { return true; }
std::size_t parent_idx = (idx - 1) / 2;
while (timerList_[idx]->ExpirationTime() < timerList_[parent_idx]->ExpirationTime()) {
using namespace std;
swap(timerList_[idx], timerList_[parent_idx]);
// update positions
positions_[timerList_[idx]->GetId()] = idx;
positions_[timerList_[parent_idx]->GetId()] = parent_idx;
if (parent_idx != 0) {
idx = parent_idx;
parent_idx = (idx - 1) / 2;
continue;
} else {
return true;
}
}
return false;
}
bool Del(const detail::TimerId_t id) {
std::size_t idx = -1; // unsigned LONG maximum
try {
idx = positions_.at(id);
} catch(const std::out_of_range& e) {
// throw ?
return false;
}
if (idx == 0) {
this->Pop();
return true;
}
detail::TimerId_t backTimer_id = timerList_.back()->GetId();
using namespace std;
// tips: the implementation of a standard swap checks the self-swap
swap(timerList_[idx], timerList_.back());
timerList_.pop_back();
auto ret = positions_.erase(id);
assert(ret == 1); (void)ret;
assert(timerList_.size() == positions_.size());
if (backTimer_id != id) {
// the specified timer is not in the timerList's back, update the new position and adjust the structure
positions_[backTimer_id] = idx;
Adjust(idx);
}
return false;
}
const std::unique_ptr<Timer>& Top() const {
return timerList_.front();
}
std::unique_ptr<Timer>& Top() {
return timerList_.front();
}
void Adjust(std::size_t cur_mid) {
while (cur_mid * 2 + 1 < timerList_.size()) {
std::size_t left = cur_mid * 2 + 1, right = cur_mid * 2 + 2;
std::size_t candidate = cur_mid;
if (left < timerList_.size() && timerList_[left]->ExpirationTime() < timerList_[candidate]->ExpirationTime()) {
candidate = left;
}
if (right < timerList_.size() && timerList_[right]->ExpirationTime() < timerList_[candidate]->ExpirationTime()) {
candidate = right;
}
if (candidate != cur_mid) { // idx指向的定时器需要向下层调整
using namespace std;
// the implementation of a standard swap checks the self-swap
swap(timerList_[cur_mid], timerList_[candidate]);
// update positions
positions_[timerList_[candidate]->GetId()] = candidate;
positions_[timerList_[cur_mid]->GetId()] = cur_mid;
cur_mid = candidate;
} else { // adjustment is completed
break;
}
}
}
bool Empty() const { return timerList_.empty(); }
/**
* call by TimerQueue::GetExpiredTimers
*/
void PopMovedTimer(const detail::TimerId_t id) {
assert(!timerList_.empty());
if (timerList_.size() > 1) {
assert(positions_.find(id) != positions_.end());
size_t target_idx = positions_[id];
bool flag = timerList_.back()->GetId() != id;
using namespace std;
// tips: the implementation of a standard swap checks the self-swap
swap(timerList_[target_idx], timerList_.back());
timerList_.pop_back();
auto ret = positions_.erase(id);
assert(ret == 1); (void)ret;
assert(timerList_.size() == positions_.size());
if (flag) {
// the specified timer is not in the timerList's back, update the new position and adjust the structure
positions_[timerList_[target_idx]->GetId()] = target_idx;
Adjust(target_idx);
}
} else if (timerList_.size() == 1) {
assert(positions_.size() == 1);
timerList_.clear();
positions_.clear();
}
}
private:
/**
* call by TimerMinHeap::Del
*/
void Pop() {
PopMovedTimer(timerList_.front()->GetId());
}
private:
TimerQueue* const owner_;
TimerList timerList_;
TimerMap positions_;
};
/*************************************************************************************************/
TimerQueue::TimerQueue(EventLoop* owner)
: owner_(owner)
#ifdef MUDUO_USE_MEMPOOL
, watcher_(new (owner_->GetMemoryPool()) Watcher(this))
#else
, watcher_(std::make_unique<Watcher>(this))
#endif
, heap_(std::make_unique<TimerMinHeap>(this))
, nextTimerId_(0)
, latestTime_(TimePoint_t::max())
{
}
TimerQueue::~TimerQueue() noexcept = default;
detail::TimerId_t TimerQueue::AddTimer(const TimePoint_t& when, const Interval_t& interval, const TimeoutCb_t& cb)
{
assert(when != TimePoint_t::max());
// just need to ensuring the atomic
int cur_timer_id = nextTimerId_.fetch_add(1, std::memory_order::memory_order_relaxed);
owner_->RunInEventLoop([=]() { // Capture by value
std::unique_ptr<Timer> t_p = std::make_unique<Timer>(when, interval, cb, cur_timer_id);
this->AddTimerInLoop(t_p);
});
return cur_timer_id;
}
void TimerQueue::AddTimerInLoop(std::unique_ptr<Timer>& t_p) {
owner_->AssertInLoopThread();
auto timeout = t_p->ExpirationTime();
bool latest_need_update = heap_->Add(std::move(t_p));
if (latest_need_update) {
latestTime_ = timeout;
ResetTimerfd();
}
}
void TimerQueue::CancelTimer(const detail::TimerId_t id) {
owner_->RunInEventLoop(std::bind(&TimerQueue::CancelTimerInLoop, this, id));
}
void TimerQueue::CancelTimerInLoop(const detail::TimerId_t id) {
owner_->AssertInLoopThread();
bool latest_need_update = heap_->Del(id);
if (latest_need_update) {
if (heap_->Empty()) {
latestTime_ = TimePoint_t::max();
} else {
latestTime_ = heap_->Top()->ExpirationTime();
}
ResetTimerfd();
}
}
void TimerQueue::ResetTimerfd() {
using namespace std;
struct itimerspec old_t, new_t;
::bzero(&new_t, sizeof new_t);
if (latestTime_ != TimePoint_t::max()) {
auto dura = latestTime_.time_since_epoch();
auto sec = chrono::duration_cast<chrono::seconds>(dura);
auto nsec = chrono::duration_cast<chrono::nanoseconds>(dura - sec);
new_t.it_value.tv_sec = static_cast<decltype(itimerspec::it_value.tv_sec)>(sec.count());
new_t.it_value.tv_nsec = static_cast<decltype(itimerspec::it_value.tv_nsec)>(nsec.count());
}
//when _pioneer = Timer_t::max, new_ts = 0 will disarms the timer
watcher_->SetTimerfd(&old_t, &new_t);
}
void TimerQueue::HandleExpiredTimers() {
// owner_->AssertInLoopThread(); // Already asserted in watcher::HandleExpiredTimers
ExpiredTimerList expired_timers = GetExpiredTimers();
for (const auto& t : expired_timers) {
t->Run();
}
}
TimerQueue::ExpiredTimerList TimerQueue::GetExpiredTimers() {
ExpiredTimerList result;
using namespace std;
// return all expired timers so far
while (!heap_->Empty() && heap_->Top()->ExpirationTime() <= chrono::steady_clock::now()) {
result.emplace_back(std::move(heap_->Top()));
if (result.back()->Repeat()) {
TimePoint_t next_expiration = TimePoint_t::clock::now() + result.back()->Interval();
heap_->Top().reset(new Timer(next_expiration, result.back()->Interval(), result.back()->GetPendingCallback(), result.back()->GetId()));
heap_->Adjust(0);
} else {
heap_->PopMovedTimer(result.back()->GetId());
}
}
if (!heap_->Empty()) {
assert(latestTime_ < heap_->Top()->ExpirationTime());
latestTime_ = heap_->Top()->ExpirationTime();
} else {
latestTime_ = TimePoint_t::max();
}
ResetTimerfd();
return result; // RVO
}