@@ -77,13 +77,13 @@ void readTimerfd(int timerfd, const Date &now)
77
77
78
78
void TimerQueue::handleRead ()
79
79
{
80
- _loop ->assertInLoopThread ();
80
+ loop_ ->assertInLoopThread ();
81
81
const Date now = Date::date ();
82
- readTimerfd (_timerfd , now);
82
+ readTimerfd (timerfd_ , now);
83
83
84
84
std::vector<TimerPtr> expired = getExpired (now);
85
85
86
- _callingExpiredTimers = true ;
86
+ callingExpiredTimers_ = true ;
87
87
// cancelingTimers_.clear();
88
88
// safe to callback outside critical section
89
89
for (auto const &timerPtr : expired)
@@ -93,7 +93,7 @@ void TimerQueue::handleRead()
93
93
timerPtr->run ();
94
94
}
95
95
}
96
- _callingExpiredTimers = false ;
96
+ callingExpiredTimers_ = false ;
97
97
98
98
reset (expired, now);
99
99
}
@@ -110,12 +110,12 @@ int howMuchTimeFromNow(const Date &when)
110
110
}
111
111
void TimerQueue::processTimers ()
112
112
{
113
- _loop ->assertInLoopThread ();
113
+ loop_ ->assertInLoopThread ();
114
114
const Date now = Date::date ();
115
115
116
116
std::vector<TimerPtr> expired = getExpired (now);
117
117
118
- _callingExpiredTimers = true ;
118
+ callingExpiredTimers_ = true ;
119
119
// cancelingTimers_.clear();
120
120
// safe to callback outside critical section
121
121
for (auto const &timerPtr : expired)
@@ -125,55 +125,55 @@ void TimerQueue::processTimers()
125
125
timerPtr->run ();
126
126
}
127
127
}
128
- _callingExpiredTimers = false ;
128
+ callingExpiredTimers_ = false ;
129
129
130
130
reset (expired, now);
131
131
}
132
132
#endif
133
133
// /////////////////////////////////////
134
134
TimerQueue::TimerQueue (EventLoop *loop)
135
- : _loop (loop),
135
+ : loop_ (loop),
136
136
#ifdef __linux__
137
- _timerfd (createTimerfd()),
138
- _timerfdChannelPtr (new Channel(loop, _timerfd )),
137
+ timerfd_ (createTimerfd()),
138
+ timerfdChannelPtr_ (new Channel(loop, timerfd_ )),
139
139
#endif
140
- _timers (),
141
- _callingExpiredTimers (false )
140
+ timers_ (),
141
+ callingExpiredTimers_ (false )
142
142
{
143
143
#ifdef __linux__
144
- _timerfdChannelPtr ->setReadCallback (
144
+ timerfdChannelPtr_ ->setReadCallback (
145
145
std::bind (&TimerQueue::handleRead, this ));
146
146
// we are always reading the timerfd, we disarm it with timerfd_settime.
147
- _timerfdChannelPtr ->enableReading ();
147
+ timerfdChannelPtr_ ->enableReading ();
148
148
#endif
149
149
}
150
150
#ifdef __linux__
151
151
void TimerQueue::reset ()
152
152
{
153
- _loop ->runInLoop ([this ]() {
154
- _timerfdChannelPtr ->disableAll ();
155
- _timerfdChannelPtr ->remove ();
156
- close (_timerfd );
157
- _timerfd = createTimerfd ();
158
- _timerfdChannelPtr = std::make_shared<Channel>(_loop, _timerfd );
159
- _timerfdChannelPtr ->setReadCallback (
153
+ loop_ ->runInLoop ([this ]() {
154
+ timerfdChannelPtr_ ->disableAll ();
155
+ timerfdChannelPtr_ ->remove ();
156
+ close (timerfd_ );
157
+ timerfd_ = createTimerfd ();
158
+ timerfdChannelPtr_ = std::make_shared<Channel>(loop_, timerfd_ );
159
+ timerfdChannelPtr_ ->setReadCallback (
160
160
std::bind (&TimerQueue::handleRead, this ));
161
161
// we are always reading the timerfd, we disarm it with timerfd_settime.
162
- _timerfdChannelPtr ->enableReading ();
163
- if (!_timers .empty ())
162
+ timerfdChannelPtr_ ->enableReading ();
163
+ if (!timers_ .empty ())
164
164
{
165
- const Date nextExpire = _timers .top ()->when ();
166
- resetTimerfd (_timerfd , nextExpire);
165
+ const Date nextExpire = timers_ .top ()->when ();
166
+ resetTimerfd (timerfd_ , nextExpire);
167
167
}
168
168
});
169
169
}
170
170
#endif
171
171
TimerQueue::~TimerQueue ()
172
172
{
173
173
#ifdef __linux__
174
- auto chlPtr = _timerfdChannelPtr ;
175
- auto fd = _timerfd ;
176
- _loop ->runInLoop ([chlPtr, fd]() {
174
+ auto chlPtr = timerfdChannelPtr_ ;
175
+ auto fd = timerfd_ ;
176
+ loop_ ->runInLoop ([chlPtr, fd]() {
177
177
chlPtr->disableAll ();
178
178
chlPtr->remove ();
179
179
::close (fd);
@@ -188,7 +188,7 @@ TimerId TimerQueue::addTimer(const TimerCallback &cb,
188
188
std::shared_ptr<Timer> timerPtr =
189
189
std::make_shared<Timer>(cb, when, interval);
190
190
191
- _loop ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
191
+ loop_ ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
192
192
return timerPtr->id ();
193
193
}
194
194
TimerId TimerQueue::addTimer (TimerCallback &&cb,
@@ -198,64 +198,64 @@ TimerId TimerQueue::addTimer(TimerCallback &&cb,
198
198
std::shared_ptr<Timer> timerPtr =
199
199
std::make_shared<Timer>(std::move (cb), when, interval);
200
200
201
- _loop ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
201
+ loop_ ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
202
202
return timerPtr->id ();
203
203
}
204
204
void TimerQueue::addTimerInLoop (const TimerPtr &timer)
205
205
{
206
- _loop ->assertInLoopThread ();
206
+ loop_ ->assertInLoopThread ();
207
207
timerIdSet_.insert (timer->id ());
208
208
if (insert (timer))
209
209
{
210
210
// the earliest timer changed
211
211
#ifdef __linux__
212
- resetTimerfd (_timerfd , timer->when ());
212
+ resetTimerfd (timerfd_ , timer->when ());
213
213
#endif
214
214
}
215
215
}
216
216
217
217
void TimerQueue::invalidateTimer (TimerId id)
218
218
{
219
- _loop ->runInLoop ([=]() { timerIdSet_.erase (id); });
219
+ loop_ ->runInLoop ([=]() { timerIdSet_.erase (id); });
220
220
}
221
221
222
222
bool TimerQueue::insert (const TimerPtr &timerPtr)
223
223
{
224
- _loop ->assertInLoopThread ();
224
+ loop_ ->assertInLoopThread ();
225
225
bool earliestChanged = false ;
226
- if (_timers .size () == 0 || *timerPtr < *_timers .top ())
226
+ if (timers_ .size () == 0 || *timerPtr < *timers_ .top ())
227
227
{
228
228
earliestChanged = true ;
229
229
}
230
- _timers .push (timerPtr);
230
+ timers_ .push (timerPtr);
231
231
// std::cout<<"after push new
232
232
// timer:"<<timerPtr->when().microSecondsSinceEpoch()/1000000<<std::endl;
233
233
return earliestChanged;
234
234
}
235
235
#ifndef __linux__
236
236
int TimerQueue::getTimeout () const
237
237
{
238
- _loop ->assertInLoopThread ();
239
- if (_timers .empty ())
238
+ loop_ ->assertInLoopThread ();
239
+ if (timers_ .empty ())
240
240
{
241
241
return 10000 ;
242
242
}
243
243
else
244
244
{
245
- return howMuchTimeFromNow (_timers .top ()->when ());
245
+ return howMuchTimeFromNow (timers_ .top ()->when ());
246
246
}
247
247
}
248
248
#endif
249
249
250
250
std::vector<TimerPtr> TimerQueue::getExpired (const Date &now)
251
251
{
252
252
std::vector<TimerPtr> expired;
253
- while (!_timers .empty ())
253
+ while (!timers_ .empty ())
254
254
{
255
- if (_timers .top ()->when () < now)
255
+ if (timers_ .top ()->when () < now)
256
256
{
257
- expired.push_back (_timers .top ());
258
- _timers .pop ();
257
+ expired.push_back (timers_ .top ());
258
+ timers_ .pop ();
259
259
}
260
260
else
261
261
break ;
@@ -264,7 +264,7 @@ std::vector<TimerPtr> TimerQueue::getExpired(const Date &now)
264
264
}
265
265
void TimerQueue::reset (const std::vector<TimerPtr> &expired, const Date &now)
266
266
{
267
- _loop ->assertInLoopThread ();
267
+ loop_ ->assertInLoopThread ();
268
268
for (auto const &timerPtr : expired)
269
269
{
270
270
if (timerPtr->isRepeat () &&
@@ -275,10 +275,10 @@ void TimerQueue::reset(const std::vector<TimerPtr> &expired, const Date &now)
275
275
}
276
276
}
277
277
#ifdef __linux__
278
- if (!_timers .empty ())
278
+ if (!timers_ .empty ())
279
279
{
280
- const Date nextExpire = _timers .top ()->when ();
281
- resetTimerfd (_timerfd , nextExpire);
280
+ const Date nextExpire = timers_ .top ()->when ();
281
+ resetTimerfd (timerfd_ , nextExpire);
282
282
}
283
283
#endif
284
284
}
0 commit comments