@@ -54,56 +54,56 @@ const int kPollTimeMs = 10000;
54
54
thread_local EventLoop *t_loopInThisThread = nullptr ;
55
55
56
56
EventLoop::EventLoop ()
57
- : _looping (false ),
58
- _threadId (std::this_thread::get_id()),
59
- _quit (false ),
60
- _poller (Poller::newPoller(this )),
61
- _currentActiveChannel( NULL ),
62
- _eventHandling (false ),
63
- _timerQueue (new TimerQueue(this ))
57
+ : looping_ (false ),
58
+ threadId_ (std::this_thread::get_id()),
59
+ quit_ (false ),
60
+ poller_ (Poller::newPoller(this )),
61
+ currentActiveChannel_( nullptr ),
62
+ eventHandling_ (false ),
63
+ timerQueue_ (new TimerQueue(this ))
64
64
#ifdef __linux__
65
65
,
66
- _wakeupFd (createEventfd()),
67
- _wakeupChannelPtr (new Channel(this , _wakeupFd ))
66
+ wakeupFd_ (createEventfd()),
67
+ wakeupChannelPtr_ (new Channel(this , wakeupFd_ ))
68
68
#endif
69
69
{
70
70
assert (t_loopInThisThread == 0 );
71
71
t_loopInThisThread = this ;
72
72
#ifdef __linux__
73
73
#else
74
- auto r = pipe (_wakeupFd );
74
+ auto r = pipe (wakeupFd_ );
75
75
(void )r;
76
76
assert (!r);
77
- fcntl (_wakeupFd [0 ], F_SETFL, O_NONBLOCK | O_CLOEXEC);
78
- fcntl (_wakeupFd [1 ], F_SETFL, O_NONBLOCK | O_CLOEXEC);
79
- _wakeupChannelPtr =
80
- std::unique_ptr<Channel>(new Channel (this , _wakeupFd [0 ]));
77
+ fcntl (wakeupFd_ [0 ], F_SETFL, O_NONBLOCK | O_CLOEXEC);
78
+ fcntl (wakeupFd_ [1 ], F_SETFL, O_NONBLOCK | O_CLOEXEC);
79
+ wakeupChannelPtr_ =
80
+ std::unique_ptr<Channel>(new Channel (this , wakeupFd_ [0 ]));
81
81
82
82
#endif
83
- _wakeupChannelPtr ->setReadCallback (std::bind (&EventLoop::wakeupRead, this ));
84
- _wakeupChannelPtr ->enableReading ();
83
+ wakeupChannelPtr_ ->setReadCallback (std::bind (&EventLoop::wakeupRead, this ));
84
+ wakeupChannelPtr_ ->enableReading ();
85
85
}
86
86
#ifdef __linux__
87
87
void EventLoop::resetTimerQueue ()
88
88
{
89
89
assertInLoopThread ();
90
- assert (!_looping );
91
- _timerQueue ->reset ();
90
+ assert (!looping_ );
91
+ timerQueue_ ->reset ();
92
92
}
93
93
#endif
94
94
void EventLoop::resetAfterFork ()
95
95
{
96
- _poller ->resetAfterFork ();
96
+ poller_ ->resetAfterFork ();
97
97
}
98
98
EventLoop::~EventLoop ()
99
99
{
100
- assert (!_looping );
100
+ assert (!looping_ );
101
101
t_loopInThisThread = nullptr ;
102
102
#ifdef __linux__
103
- close (_wakeupFd );
103
+ close (wakeupFd_ );
104
104
#else
105
- close (_wakeupFd [0 ]);
106
- close (_wakeupFd [1 ]);
105
+ close (wakeupFd_ [0 ]);
106
+ close (wakeupFd_ [1 ]);
107
107
#endif
108
108
}
109
109
EventLoop *EventLoop::getEventLoopOfCurrentThread ()
@@ -114,25 +114,25 @@ void EventLoop::updateChannel(Channel *channel)
114
114
{
115
115
assert (channel->ownerLoop () == this );
116
116
assertInLoopThread ();
117
- _poller ->updateChannel (channel);
117
+ poller_ ->updateChannel (channel);
118
118
}
119
119
void EventLoop::removeChannel (Channel *channel)
120
120
{
121
121
assert (channel->ownerLoop () == this );
122
122
assertInLoopThread ();
123
- if (_eventHandling )
123
+ if (eventHandling_ )
124
124
{
125
- assert (_currentActiveChannel == channel ||
126
- std::find (_activeChannels .begin (),
127
- _activeChannels .end (),
128
- channel) == _activeChannels .end ());
125
+ assert (currentActiveChannel_ == channel ||
126
+ std::find (activeChannels_ .begin (),
127
+ activeChannels_ .end (),
128
+ channel) == activeChannels_ .end ());
129
129
}
130
- _poller ->removeChannel (channel);
130
+ poller_ ->removeChannel (channel);
131
131
}
132
132
void EventLoop::quit ()
133
133
{
134
- _quit = true ;
135
- // There is a chance that loop() just executes while(!_quit ) and exits,
134
+ quit_ = true ;
135
+ // There is a chance that loop() just executes while(!quit_ ) and exits,
136
136
// then EventLoop destructs, then we are accessing an invalid object.
137
137
// Can be fixed using mutex_ in both places.
138
138
if (!isInLoopThread ())
@@ -142,35 +142,35 @@ void EventLoop::quit()
142
142
}
143
143
void EventLoop::loop ()
144
144
{
145
- assert (!_looping );
145
+ assert (!looping_ );
146
146
assertInLoopThread ();
147
- _looping = true ;
148
- _quit = false ;
147
+ looping_ = true ;
148
+ quit_ = false ;
149
149
150
- while (!_quit )
150
+ while (!quit_ )
151
151
{
152
- _activeChannels .clear ();
152
+ activeChannels_ .clear ();
153
153
#ifdef __linux__
154
- _poller ->poll (kPollTimeMs , &_activeChannels );
154
+ poller_ ->poll (kPollTimeMs , &activeChannels_ );
155
155
#else
156
- _poller ->poll (_timerQueue ->getTimeout (), &_activeChannels );
157
- _timerQueue ->processTimers ();
156
+ poller_ ->poll (timerQueue_ ->getTimeout (), &activeChannels_ );
157
+ timerQueue_ ->processTimers ();
158
158
#endif
159
159
// TODO sort channel by priority
160
160
// std::cout<<"after ->poll()"<<std::endl;
161
- _eventHandling = true ;
162
- for (auto it = _activeChannels .begin (); it != _activeChannels .end ();
161
+ eventHandling_ = true ;
162
+ for (auto it = activeChannels_ .begin (); it != activeChannels_ .end ();
163
163
++it)
164
164
{
165
- _currentActiveChannel = *it;
166
- _currentActiveChannel ->handleEvent ();
165
+ currentActiveChannel_ = *it;
166
+ currentActiveChannel_ ->handleEvent ();
167
167
}
168
- _currentActiveChannel = NULL ;
169
- _eventHandling = false ;
168
+ currentActiveChannel_ = NULL ;
169
+ eventHandling_ = false ;
170
170
// std::cout << "looping" << endl;
171
171
doRunInLoopFuncs ();
172
172
}
173
- _looping = false ;
173
+ looping_ = false ;
174
174
}
175
175
void EventLoop::abortNotInLoopThread ()
176
176
{
@@ -202,28 +202,28 @@ void EventLoop::runInLoop(Func &&cb)
202
202
}
203
203
void EventLoop::queueInLoop (const Func &cb)
204
204
{
205
- _funcs .enqueue (cb);
206
- if (!isInLoopThread () || !_looping )
205
+ funcs_ .enqueue (cb);
206
+ if (!isInLoopThread () || !looping_ )
207
207
{
208
208
wakeup ();
209
209
}
210
210
}
211
211
void EventLoop::queueInLoop (Func &&cb)
212
212
{
213
- _funcs .enqueue (std::move (cb));
214
- if (!isInLoopThread () || !_looping )
213
+ funcs_ .enqueue (std::move (cb));
214
+ if (!isInLoopThread () || !looping_ )
215
215
{
216
216
wakeup ();
217
217
}
218
218
}
219
219
220
220
TimerId EventLoop::runAt (const Date &time, const Func &cb)
221
221
{
222
- return _timerQueue ->addTimer (cb, time, 0 );
222
+ return timerQueue_ ->addTimer (cb, time, 0 );
223
223
}
224
224
TimerId EventLoop::runAt (const Date &time, Func &&cb)
225
225
{
226
- return _timerQueue ->addTimer (std::move (cb), time, 0 );
226
+ return timerQueue_ ->addTimer (std::move (cb), time, 0 );
227
227
}
228
228
TimerId EventLoop::runAfter (double delay, const Func &cb)
229
229
{
@@ -235,40 +235,40 @@ TimerId EventLoop::runAfter(double delay, Func &&cb)
235
235
}
236
236
TimerId EventLoop::runEvery (double interval, const Func &cb)
237
237
{
238
- return _timerQueue ->addTimer (cb, Date::date ().after (interval), interval);
238
+ return timerQueue_ ->addTimer (cb, Date::date ().after (interval), interval);
239
239
}
240
240
TimerId EventLoop::runEvery (double interval, Func &&cb)
241
241
{
242
- return _timerQueue ->addTimer (std::move (cb),
242
+ return timerQueue_ ->addTimer (std::move (cb),
243
243
Date::date ().after (interval),
244
244
interval);
245
245
}
246
246
void EventLoop::invalidateTimer (TimerId id)
247
247
{
248
- if (isRunning () && _timerQueue )
249
- _timerQueue ->invalidateTimer (id);
248
+ if (isRunning () && timerQueue_ )
249
+ timerQueue_ ->invalidateTimer (id);
250
250
}
251
251
void EventLoop::doRunInLoopFuncs ()
252
252
{
253
- _callingFuncs = true ;
253
+ callingFuncs_ = true ;
254
254
{
255
255
Func func;
256
- while (_funcs .dequeue (func))
256
+ while (funcs_ .dequeue (func))
257
257
{
258
258
func ();
259
259
}
260
260
}
261
- _callingFuncs = false ;
261
+ callingFuncs_ = false ;
262
262
}
263
263
void EventLoop::wakeup ()
264
264
{
265
- // if (!_looping )
265
+ // if (!looping_ )
266
266
// return;
267
267
uint64_t tmp = 1 ;
268
268
#ifdef __linux__
269
- int ret = write (_wakeupFd , &tmp, sizeof (tmp));
269
+ int ret = write (wakeupFd_ , &tmp, sizeof (tmp));
270
270
#else
271
- int ret = write (_wakeupFd [1 ], &tmp, sizeof (tmp));
271
+ int ret = write (wakeupFd_ [1 ], &tmp, sizeof (tmp));
272
272
#endif
273
273
if (ret < 0 )
274
274
{
@@ -280,9 +280,9 @@ void EventLoop::wakeupRead()
280
280
uint64_t tmp;
281
281
ssize_t ret = 0 ;
282
282
#ifdef __linux__
283
- ret = read (_wakeupFd , &tmp, sizeof (tmp));
283
+ ret = read (wakeupFd_ , &tmp, sizeof (tmp));
284
284
#else
285
- ret = read (_wakeupFd [0 ], &tmp, sizeof (tmp));
285
+ ret = read (wakeupFd_ [0 ], &tmp, sizeof (tmp));
286
286
#endif
287
287
if (ret < 0 )
288
288
LOG_SYSERR << " wakeup read error" ;
0 commit comments