-
Notifications
You must be signed in to change notification settings - Fork 11
/
time_wheel_scheduler.h
45 lines (31 loc) · 1.01 KB
/
time_wheel_scheduler.h
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
#ifndef TIME_WHEEL_SCHEDULER_H_
#define TIME_WHEEL_SCHEDULER_H_
#include <mutex>
#include <vector>
#include <thread>
#include <unordered_set>
#include "time_wheel.h"
class TimeWheelScheduler {
public:
explicit TimeWheelScheduler(uint32_t timer_step_ms = 50);
// Return timer id. Return 0 if the timer creation fails.
uint32_t CreateTimerAt(int64_t when_ms, const TimerTask& task);
uint32_t CreateTimerAfter(int64_t delay_ms, const TimerTask& task);
uint32_t CreateTimerEvery(int64_t interval_ms, const TimerTask& task);
void CancelTimer(uint32_t timer_id);
bool Start();
void Stop();
void AppendTimeWheel(uint32_t scales, uint32_t scale_unit_ms, const std::string& name = "");
private:
void Run();
TimeWheelPtr GetGreatestTimeWheel();
TimeWheelPtr GetLeastTimeWheel();
private:
std::mutex mutex_;
std::thread thread_;
bool stop_;
std::unordered_set<uint32_t> cancel_timer_ids_;
uint32_t timer_step_ms_;
std::vector<TimeWheelPtr> time_wheels_;
};
#endif // TIME_WHEEL_SCHEDULER_H_