-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathHHWheelTimer.h
57 lines (44 loc) · 1.29 KB
/
HHWheelTimer.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
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright © 2022 [email protected] All rights reserved.
// See accompanying files LICENSE
#pragma once
#include "TimerBase.h"
#include "timer_list.h"
#include <unordered_map>
// Hashed and Hierarchical Timing Wheels
// see https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/kernel/timer.c?h=linux-3.10.y
//
// timer scheduler implemented by hashed & hierachical wheels
// complexity:
// StartTimer CancelTimer PerTick
// O(1) O(1) O(1)
//
struct TimerNode;
class HHWheelTimer : public TimerBase
{
public:
HHWheelTimer();
~HHWheelTimer();
TimerSchedType Type() const override
{
return TimerSchedType::TIMER_HH_WHEEL;
}
// start a timer after `duration` milliseconds
int Start(uint32_t duration, TimeoutAction action) override;
// cancel a timer
bool Cancel(int timer_id) override;
// we assume 1 tick per ms
int Update(int64_t ticks) override;
int Size() const override
{
return (int)ref_.size();
}
TimeoutAction findAndDelAction(int id);
private:
void clear();
static void handleTimerExpired(timer_list*);
private:
int64_t started_at_ = 0;
tvec_base base_;
std::unordered_map<int, timer_list*> ref_;
std::unordered_map<int, TimeoutAction> actions_;
};