-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerQueue.cpp
133 lines (116 loc) · 3.42 KB
/
TimerQueue.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
124
125
126
127
128
129
130
131
132
133
//=====================================================================
//
// TimerQueue.cpp -
//
// Created by wwq on 2022/08/23
// Last Modified: 2022/08/23 22:05:09
//
//=====================================================================
#include "TimerQueue.h"
#include "Callbacks.h"
#include "EventLoop.h"
#include "Timer.h"
#include <cassert>
#include <utility>
namespace simple_muduo{
TimerQueue::TimerQueue(EventLoop* loop)
: loop_(loop)
, callingTimesFunctor_(false)
{
}
TimerQueue::~TimerQueue()
{
}
TimerId TimerQueue::addTimer(const TimerCallback& cb, const Timestamp& when, double interval)
{
TimerId id = ++atomic_;
Timer* timer = new Timer(id, cb, when, interval);
loop_->runInLoop(std::bind(&TimerQueue::addTimerInLoop,this, timer));
return id;
}
void TimerQueue::cancelTimer(TimerId id)
{
loop_->runInLoop(std::bind(&TimerQueue::cancelTimerInLoop, this, id));
}
Timestamp TimerQueue::getNearestExpiration() const
{
if(timers_.empty())
return Timestamp::invalid();
return timers_.begin()->first;
}
void TimerQueue::cancelTimerInLoop(TimerId id)
{
loop_->assertInLoopThread();
TimerMap::iterator it = activeTimers_.find(id);
if(it != activeTimers_.end())
{
TimerList::iterator iter = timers_.find(std::make_pair(it->second->expires_at(), it->second));
assert(iter!= timers_.end());
Timer* timer = it->second;
delete timer;
activeTimers_.erase(it);
timers_.erase(iter);
}
else
{
if(callingTimesFunctor_)
{
cancelTimers_.insert(id);
}
}
assert(timers_.size() == activeTimers_.size());
}
void TimerQueue::addTimerInLoop(Timer* timer)
{
loop_->assertInLoopThread();
addTimer(timer);
}
void TimerQueue::addTimer(Timer* timer)
{
timers_.insert(std::make_pair(timer->expires_at(), timer));
activeTimers_.insert(std::make_pair(timer->id(), timer));
}
void TimerQueue::runTimer(const Timestamp& now)
{
if(timers_.empty())
return;
callingTimesFunctor_ = true;
cancelTimers_.clear(); // just for saving cancel timers when runTimer
std::vector<Entry> expired = getExpiredTimers(now);
for (std::vector<Entry>::iterator it = expired.begin(); it != expired.end(); ++it)
{
it->second->trigger();
}
callingTimesFunctor_ = false;
for (std::vector<Entry>::iterator it = expired.begin(); it != expired.end(); ++it)
{
Timer *timer = it->second;
if(timer->repeat() && cancelTimers_.find(timer->id()) == cancelTimers_.end())
{
timer->restart(now);
addTimer(timer);
}
else // timer just run once or has already being canceled
{
delete timer;
}
}
expired.clear();
assert(timers_.size() == activeTimers_.size());
}
std::vector<TimerQueue::Entry> TimerQueue::getExpiredTimers(const Timestamp& now)
{
std::vector<Entry> expired;
Entry piovt(now, reinterpret_cast<Timer*>(INT_MAX));
TimerList::iterator end = timers_.lower_bound(piovt); // return the pos of that not less than piovt
assert(end == timers_.end() || now < end->first);
std::copy(timers_.begin(), end, std::back_inserter(expired));
timers_.erase(timers_.begin(), end);
for (std::vector< Entry >::iterator it = expired.begin(); it != expired.end(); ++it)
{
activeTimers_.erase(it->second->id());
}
// LOG_INFO("TimerQueue::getExpiredTimers [%d][%d][%d]", expired.size(), timers_.size(), activeTimers_.size());
return expired;
}
} // namespace simple_muduo