-
Notifications
You must be signed in to change notification settings - Fork 11
/
time_wheel_scheduler.cpp
123 lines (97 loc) · 3.01 KB
/
time_wheel_scheduler.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
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
#include "time_wheel_scheduler.h"
static uint32_t s_inc_id = 1;
TimeWheelScheduler::TimeWheelScheduler(uint32_t timer_step_ms)
: timer_step_ms_(timer_step_ms)
, stop_(false) {
}
bool TimeWheelScheduler::Start() {
if (timer_step_ms_ < 50) {
return false;
}
if (time_wheels_.empty()) {
return false;
}
thread_ = std::thread(std::bind(&TimeWheelScheduler::Run, this));
return true;
}
void TimeWheelScheduler::Run() {
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(timer_step_ms_));
std::lock_guard<std::mutex> lock(mutex_);
if (stop_) {
break;
}
TimeWheelPtr least_time_wheel = GetLeastTimeWheel();
least_time_wheel->Increase();
std::list<TimerPtr> slot = std::move(least_time_wheel->GetAndClearCurrentSlot());
for (const TimerPtr& timer : slot) {
auto it = cancel_timer_ids_.find(timer->id());
if (it != cancel_timer_ids_.end()) {
cancel_timer_ids_.erase(it);
continue;
}
timer->Run();
if (timer->repeated()) {
timer->UpdateWhenTime();
GetGreatestTimeWheel()->AddTimer(timer);
}
}
}
}
void TimeWheelScheduler::Stop() {
{
std::lock_guard<std::mutex> lock(mutex_);
stop_ = true;
}
thread_.join();
}
TimeWheelPtr TimeWheelScheduler::GetGreatestTimeWheel() {
if (time_wheels_.empty()) {
return TimeWheelPtr();
}
return time_wheels_.front();
}
TimeWheelPtr TimeWheelScheduler::GetLeastTimeWheel() {
if (time_wheels_.empty()) {
return TimeWheelPtr();
}
return time_wheels_.back();
}
void TimeWheelScheduler::AppendTimeWheel(uint32_t scales, uint32_t scale_unit_ms, const std::string& name) {
TimeWheelPtr time_wheel = std::make_shared<TimeWheel>(scales, scale_unit_ms, name);
if (time_wheels_.empty()) {
time_wheels_.push_back(time_wheel);
return;
}
TimeWheelPtr greater_time_wheel = time_wheels_.back();
greater_time_wheel->set_less_level_tw(time_wheel.get());
time_wheel->set_greater_level_tw(greater_time_wheel.get());
time_wheels_.push_back(time_wheel);
}
uint32_t TimeWheelScheduler::CreateTimerAt(int64_t when_ms, const TimerTask& task) {
if (time_wheels_.empty()) {
return 0;
}
std::lock_guard<std::mutex> lock(mutex_);
++s_inc_id;
GetGreatestTimeWheel()->AddTimer(std::make_shared<Timer>(s_inc_id, when_ms, 0, task));
return s_inc_id;
}
uint32_t TimeWheelScheduler::CreateTimerAfter(int64_t delay_ms, const TimerTask& task) {
int64_t when = GetNowTimestamp() + delay_ms;
return CreateTimerAt(when, task);
}
uint32_t TimeWheelScheduler::CreateTimerEvery(int64_t interval_ms, const TimerTask& task) {
if (time_wheels_.empty()) {
return 0;
}
std::lock_guard<std::mutex> lock(mutex_);
++s_inc_id;
int64_t when = GetNowTimestamp() + interval_ms;
GetGreatestTimeWheel()->AddTimer(std::make_shared<Timer>(s_inc_id, when, interval_ms, task));
return s_inc_id;
}
void TimeWheelScheduler::CancelTimer(uint32_t timer_id) {
std::lock_guard<std::mutex> lock(mutex_);
cancel_timer_ids_.insert(timer_id);
}