@@ -77,13 +77,13 @@ void readTimerfd(int timerfd, const Date &now)
7777
7878void TimerQueue::handleRead ()
7979{
80- _loop ->assertInLoopThread ();
80+ loop_ ->assertInLoopThread ();
8181 const Date now = Date::date ();
82- readTimerfd (_timerfd , now);
82+ readTimerfd (timerfd_ , now);
8383
8484 std::vector<TimerPtr> expired = getExpired (now);
8585
86- _callingExpiredTimers = true ;
86+ callingExpiredTimers_ = true ;
8787 // cancelingTimers_.clear();
8888 // safe to callback outside critical section
8989 for (auto const &timerPtr : expired)
@@ -93,7 +93,7 @@ void TimerQueue::handleRead()
9393 timerPtr->run ();
9494 }
9595 }
96- _callingExpiredTimers = false ;
96+ callingExpiredTimers_ = false ;
9797
9898 reset (expired, now);
9999}
@@ -110,12 +110,12 @@ int howMuchTimeFromNow(const Date &when)
110110}
111111void TimerQueue::processTimers ()
112112{
113- _loop ->assertInLoopThread ();
113+ loop_ ->assertInLoopThread ();
114114 const Date now = Date::date ();
115115
116116 std::vector<TimerPtr> expired = getExpired (now);
117117
118- _callingExpiredTimers = true ;
118+ callingExpiredTimers_ = true ;
119119 // cancelingTimers_.clear();
120120 // safe to callback outside critical section
121121 for (auto const &timerPtr : expired)
@@ -125,55 +125,55 @@ void TimerQueue::processTimers()
125125 timerPtr->run ();
126126 }
127127 }
128- _callingExpiredTimers = false ;
128+ callingExpiredTimers_ = false ;
129129
130130 reset (expired, now);
131131}
132132#endif
133133// /////////////////////////////////////
134134TimerQueue::TimerQueue (EventLoop *loop)
135- : _loop (loop),
135+ : loop_ (loop),
136136#ifdef __linux__
137- _timerfd (createTimerfd()),
138- _timerfdChannelPtr (new Channel(loop, _timerfd )),
137+ timerfd_ (createTimerfd()),
138+ timerfdChannelPtr_ (new Channel(loop, timerfd_ )),
139139#endif
140- _timers (),
141- _callingExpiredTimers (false )
140+ timers_ (),
141+ callingExpiredTimers_ (false )
142142{
143143#ifdef __linux__
144- _timerfdChannelPtr ->setReadCallback (
144+ timerfdChannelPtr_ ->setReadCallback (
145145 std::bind (&TimerQueue::handleRead, this ));
146146 // we are always reading the timerfd, we disarm it with timerfd_settime.
147- _timerfdChannelPtr ->enableReading ();
147+ timerfdChannelPtr_ ->enableReading ();
148148#endif
149149}
150150#ifdef __linux__
151151void TimerQueue::reset ()
152152{
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 (
160160 std::bind (&TimerQueue::handleRead, this ));
161161 // 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 ())
164164 {
165- const Date nextExpire = _timers .top ()->when ();
166- resetTimerfd (_timerfd , nextExpire);
165+ const Date nextExpire = timers_ .top ()->when ();
166+ resetTimerfd (timerfd_ , nextExpire);
167167 }
168168 });
169169}
170170#endif
171171TimerQueue::~TimerQueue ()
172172{
173173#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]() {
177177 chlPtr->disableAll ();
178178 chlPtr->remove ();
179179 ::close (fd);
@@ -188,7 +188,7 @@ TimerId TimerQueue::addTimer(const TimerCallback &cb,
188188 std::shared_ptr<Timer> timerPtr =
189189 std::make_shared<Timer>(cb, when, interval);
190190
191- _loop ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
191+ loop_ ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
192192 return timerPtr->id ();
193193}
194194TimerId TimerQueue::addTimer (TimerCallback &&cb,
@@ -198,64 +198,64 @@ TimerId TimerQueue::addTimer(TimerCallback &&cb,
198198 std::shared_ptr<Timer> timerPtr =
199199 std::make_shared<Timer>(std::move (cb), when, interval);
200200
201- _loop ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
201+ loop_ ->runInLoop ([=]() { addTimerInLoop (timerPtr); });
202202 return timerPtr->id ();
203203}
204204void TimerQueue::addTimerInLoop (const TimerPtr &timer)
205205{
206- _loop ->assertInLoopThread ();
206+ loop_ ->assertInLoopThread ();
207207 timerIdSet_.insert (timer->id ());
208208 if (insert (timer))
209209 {
210210// the earliest timer changed
211211#ifdef __linux__
212- resetTimerfd (_timerfd , timer->when ());
212+ resetTimerfd (timerfd_ , timer->when ());
213213#endif
214214 }
215215}
216216
217217void TimerQueue::invalidateTimer (TimerId id)
218218{
219- _loop ->runInLoop ([=]() { timerIdSet_.erase (id); });
219+ loop_ ->runInLoop ([=]() { timerIdSet_.erase (id); });
220220}
221221
222222bool TimerQueue::insert (const TimerPtr &timerPtr)
223223{
224- _loop ->assertInLoopThread ();
224+ loop_ ->assertInLoopThread ();
225225 bool earliestChanged = false ;
226- if (_timers .size () == 0 || *timerPtr < *_timers .top ())
226+ if (timers_ .size () == 0 || *timerPtr < *timers_ .top ())
227227 {
228228 earliestChanged = true ;
229229 }
230- _timers .push (timerPtr);
230+ timers_ .push (timerPtr);
231231 // std::cout<<"after push new
232232 // timer:"<<timerPtr->when().microSecondsSinceEpoch()/1000000<<std::endl;
233233 return earliestChanged;
234234}
235235#ifndef __linux__
236236int TimerQueue::getTimeout () const
237237{
238- _loop ->assertInLoopThread ();
239- if (_timers .empty ())
238+ loop_ ->assertInLoopThread ();
239+ if (timers_ .empty ())
240240 {
241241 return 10000 ;
242242 }
243243 else
244244 {
245- return howMuchTimeFromNow (_timers .top ()->when ());
245+ return howMuchTimeFromNow (timers_ .top ()->when ());
246246 }
247247}
248248#endif
249249
250250std::vector<TimerPtr> TimerQueue::getExpired (const Date &now)
251251{
252252 std::vector<TimerPtr> expired;
253- while (!_timers .empty ())
253+ while (!timers_ .empty ())
254254 {
255- if (_timers .top ()->when () < now)
255+ if (timers_ .top ()->when () < now)
256256 {
257- expired.push_back (_timers .top ());
258- _timers .pop ();
257+ expired.push_back (timers_ .top ());
258+ timers_ .pop ();
259259 }
260260 else
261261 break ;
@@ -264,7 +264,7 @@ std::vector<TimerPtr> TimerQueue::getExpired(const Date &now)
264264}
265265void TimerQueue::reset (const std::vector<TimerPtr> &expired, const Date &now)
266266{
267- _loop ->assertInLoopThread ();
267+ loop_ ->assertInLoopThread ();
268268 for (auto const &timerPtr : expired)
269269 {
270270 if (timerPtr->isRepeat () &&
@@ -275,10 +275,10 @@ void TimerQueue::reset(const std::vector<TimerPtr> &expired, const Date &now)
275275 }
276276 }
277277#ifdef __linux__
278- if (!_timers .empty ())
278+ if (!timers_ .empty ())
279279 {
280- const Date nextExpire = _timers .top ()->when ();
281- resetTimerfd (_timerfd , nextExpire);
280+ const Date nextExpire = timers_ .top ()->when ();
281+ resetTimerfd (timerfd_ , nextExpire);
282282 }
283283#endif
284284}
0 commit comments