-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathydx_timer_set.cpp
More file actions
234 lines (195 loc) · 5.39 KB
/
Copy pathydx_timer_set.cpp
File metadata and controls
234 lines (195 loc) · 5.39 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#include "logging.h"
#include "ydx_timer.h"
#include "ydx_timerid.h"
#include "ydx_timer_set.h"
#include "epoller.h"
#include <boost/bind.hpp>
#include <sys/timerfd.h>
namespace ydx
{
int createTimerfd()
{
int timerfd = ::timerfd_create(CLOCK_MONOTONIC,
TFD_NONBLOCK | TFD_CLOEXEC);
if(timerfd < 0)
{
LOG_SYSFATAL << "Failed in timerfd_create";
}
return timerfd;
}
//返回一个时间距离目前时间的值 timespec表示
struct timespec howMuchTimeFromNow(Timestamp when)
{
int64_t microseconds = when.get_micro_second()
- Timestamp::now().get_micro_second();
if (microseconds < 100)
{
microseconds = 100;
}
struct timespec ts;
ts.tv_sec = static_cast<time_t>(
microseconds / Timestamp::micro_seconds_per_sec);
ts.tv_nsec = static_cast<long>(
(microseconds % Timestamp::micro_seconds_per_sec) * 1000);
return ts;
}
void readTimerfd(int timerfd, Timestamp now)
{
uint64_t howmany;
ssize_t n = ::read(timerfd, &howmany, sizeof howmany);
//LOG_INFO << "TimerQueue::handleRead() " << howmany << " at " << now.to_string();
if (n != sizeof howmany)
{
LOG_ERROR << "TimerQueue::handleRead() reads " << n << " bytes instead of 8";
}
}
//重新设点timerfd的超时值,周期超时由程序timerset自己控制,不会写入itimerspec it_interval值
void resetTimerfd(int timerfd, Timestamp expiration)
{
// wake up loop by timerfd_settime()
struct itimerspec newValue;
struct itimerspec oldValue;
bzero(&newValue, sizeof newValue);
bzero(&oldValue, sizeof oldValue);
newValue.it_value = howMuchTimeFromNow(expiration);
int ret = ::timerfd_settime(timerfd, 0, &newValue, &oldValue);
if (ret)
{
LOG_SYSERR << "timerfd_settime()";
}
}
}
using namespace ydx;
TimerSet::TimerSet(EPollPoller *epoller)
:epoller_(epoller),
timerfd_(createTimerfd()),
timerfd_channel_(epoller, timerfd_),
timer_container_()
{
timerfd_channel_.setReadCallback(
boost::bind(&TimerSet::handleRead, this));
timerfd_channel_.enableReading();
}
TimerSet::~TimerSet()
{
timerfd_channel_.disableAll();
timerfd_channel_.remove();
::close(timerfd_);
for(TimerContainer::iterator it = timer_container_.begin();
it != timer_container_.end(); ++it )
{
delete it->second;
}
}
TimerId TimerSet::addTimer(const TimerCallback& cb,
Timestamp when,
double interval)
{
Timer* timer = new Timer(cb, when, interval);
epoller_->runInLoop(
boost::bind(&TimerSet::addTimerInLoop, this, timer));
return TimerId(timer, timer->sequence());
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
TimerId TimerSet::addTimer(TimerCallback&& cb,
Timestamp when,
double interval)
{
Timer* timer = new Timer(std::move(cb), when, interval);
epoller_->runInLoop(
boost::bind(&TimerSet::addTimerInLoop, this, timer));
return TimerId(timer, timer->sequence());
}
#endif
void TimerSet::cancel(TimerId timerId)
{
epoller_->runInLoop(
boost::bind(&TimerSet::cancelInLoop, this, timerId));
}
void TimerSet::addTimerInLoop(Timer* timer)
{
//epoller_->assertInLoopThread();
bool earliestChanged = insert(timer);
if (earliestChanged)
{
resetTimerfd(timerfd_, timer->expiration());
}
}
void TimerSet::cancelInLoop(TimerId timerId)
{
// epoller_->assertInLoopThread();
//ActiveTimer timer(timerId.timer_, timerId.sequence_);
timer_container_.erase(Entry(timerId.timer_->expiration(), timerId.timer_));
delete timerId.timer_; // FIXME: no delete please
}
void TimerSet::handleRead()
{
// epoller_->assertInLoopThread();
Timestamp now(Timestamp::now());
readTimerfd(timerfd_, now);
std::vector<Entry> expired = getExpired(now);
// safe to callback outside critical section
for (std::vector<Entry>::iterator it = expired.begin();
it != expired.end(); ++it)
{
it->second->run();
}
reset(expired, now);
}
std::vector<TimerSet::Entry> TimerSet::getExpired(Timestamp now)
{
std::vector<Entry> expired;
Entry sentry(now, reinterpret_cast<Timer*>(UINTPTR_MAX));
TimerContainer::iterator end = timer_container_.lower_bound(sentry);
assert(end == timer_container_.end() || now < end->first);
std::copy(timer_container_.begin(), end, back_inserter(expired));
timer_container_.erase(timer_container_.begin(), end);
return expired;
}
//对已经超时的结点,如果不是周期性调用,则删除结点,如果周期性调用,则重新加入到时间容器
void TimerSet::reset(const std::vector<Entry>& expired, Timestamp now)
{
Timestamp nextExpire;
for (std::vector<Entry>::const_iterator it = expired.begin();
it != expired.end(); ++it)
{
if (it->second->repeat())
{
it->second->restart(now);
insert(it->second);
}
else
{
// FIXME move to a free list
delete it->second; // FIXME: no delete please
}
}
if (!timer_container_.empty())
{
nextExpire = timer_container_.begin()->second->expiration();
}
if (nextExpire.valid())
{
resetTimerfd(timerfd_, nextExpire);
}
}
bool TimerSet::insert(Timer* timer)
{
// epoller_->assertInLoopThread();
bool earliestChanged = false;
Timestamp when = timer->expiration();
TimerContainer::iterator it = timer_container_.begin();
if (it == timer_container_.end() || when < it->first)
{
earliestChanged = true;
}
{
std::pair<TimerContainer::iterator, bool> result
= timer_container_.insert(Entry(when, timer));
assert(result.second); (void)result;
}
return earliestChanged;
}