-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtime_wheel.cpp
70 lines (60 loc) · 2.03 KB
/
time_wheel.cpp
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
#include "time_wheel.h"
TimeWheel::TimeWheel(uint32_t scales, uint32_t scale_unit_ms, const std::string& name)
: name_(name)
, current_index_(0)
, scales_(scales)
, scale_unit_ms_(scale_unit_ms)
, slots_(scales)
, greater_level_tw_(nullptr)
, less_level_tw_(nullptr) {
}
int64_t TimeWheel::GetCurrentTime() const {
int64_t time = current_index_ * scale_unit_ms_;
if (less_level_tw_ != nullptr) {
time += less_level_tw_->GetCurrentTime();
}
return time;
}
void TimeWheel::AddTimer(TimerPtr timer) {
int64_t less_tw_time = 0;
if (less_level_tw_ != nullptr) {
less_tw_time = less_level_tw_->GetCurrentTime();
}
int64_t diff = timer->when_ms() + less_tw_time - GetNowTimestamp();
// If the difference is greater than scale unit, the timer can be added into the current time wheel.
if (diff >= scale_unit_ms_) {
size_t n = (current_index_ + diff / scale_unit_ms_) % scales_;
slots_[n].push_back(timer);
return;
}
// If the difference is less than scale uint, the timer should be added into less level time wheel.
if (less_level_tw_ != nullptr) {
less_level_tw_->AddTimer(timer);
return;
}
// If the current time wheel is the least level, the timer can be added into the current time wheel.
slots_[current_index_].push_back(timer);
}
void TimeWheel::Increase() {
// Increase the time wheel.
++current_index_;
if (current_index_ < scales_) {
return;
}
// If the time wheel is full, the greater level time wheel should be increased.
// The timers in the current slot of the greater level time wheel should be moved into
// the less level time wheel.
current_index_ = current_index_ % scales_;
if (greater_level_tw_ != nullptr) {
greater_level_tw_->Increase();
std::list<TimerPtr> slot = std::move(greater_level_tw_->GetAndClearCurrentSlot());
for (TimerPtr timer : slot) {
AddTimer(timer);
}
}
}
std::list<TimerPtr> TimeWheel::GetAndClearCurrentSlot() {
std::list<TimerPtr> slot;
slot = std::move(slots_[current_index_]);
return slot;
}