Skip to content

Commit 828e46f

Browse files
authored
Modify some code styles (#58)
1 parent 07771f6 commit 828e46f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1373
-1431
lines changed

trantor/net/EventLoop.cc

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -54,56 +54,56 @@ const int kPollTimeMs = 10000;
5454
thread_local EventLoop *t_loopInThisThread = nullptr;
5555

5656
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))
6464
#ifdef __linux__
6565
,
66-
_wakeupFd(createEventfd()),
67-
_wakeupChannelPtr(new Channel(this, _wakeupFd))
66+
wakeupFd_(createEventfd()),
67+
wakeupChannelPtr_(new Channel(this, wakeupFd_))
6868
#endif
6969
{
7070
assert(t_loopInThisThread == 0);
7171
t_loopInThisThread = this;
7272
#ifdef __linux__
7373
#else
74-
auto r = pipe(_wakeupFd);
74+
auto r = pipe(wakeupFd_);
7575
(void)r;
7676
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]));
8181

8282
#endif
83-
_wakeupChannelPtr->setReadCallback(std::bind(&EventLoop::wakeupRead, this));
84-
_wakeupChannelPtr->enableReading();
83+
wakeupChannelPtr_->setReadCallback(std::bind(&EventLoop::wakeupRead, this));
84+
wakeupChannelPtr_->enableReading();
8585
}
8686
#ifdef __linux__
8787
void EventLoop::resetTimerQueue()
8888
{
8989
assertInLoopThread();
90-
assert(!_looping);
91-
_timerQueue->reset();
90+
assert(!looping_);
91+
timerQueue_->reset();
9292
}
9393
#endif
9494
void EventLoop::resetAfterFork()
9595
{
96-
_poller->resetAfterFork();
96+
poller_->resetAfterFork();
9797
}
9898
EventLoop::~EventLoop()
9999
{
100-
assert(!_looping);
100+
assert(!looping_);
101101
t_loopInThisThread = nullptr;
102102
#ifdef __linux__
103-
close(_wakeupFd);
103+
close(wakeupFd_);
104104
#else
105-
close(_wakeupFd[0]);
106-
close(_wakeupFd[1]);
105+
close(wakeupFd_[0]);
106+
close(wakeupFd_[1]);
107107
#endif
108108
}
109109
EventLoop *EventLoop::getEventLoopOfCurrentThread()
@@ -114,25 +114,25 @@ void EventLoop::updateChannel(Channel *channel)
114114
{
115115
assert(channel->ownerLoop() == this);
116116
assertInLoopThread();
117-
_poller->updateChannel(channel);
117+
poller_->updateChannel(channel);
118118
}
119119
void EventLoop::removeChannel(Channel *channel)
120120
{
121121
assert(channel->ownerLoop() == this);
122122
assertInLoopThread();
123-
if (_eventHandling)
123+
if (eventHandling_)
124124
{
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());
129129
}
130-
_poller->removeChannel(channel);
130+
poller_->removeChannel(channel);
131131
}
132132
void EventLoop::quit()
133133
{
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,
136136
// then EventLoop destructs, then we are accessing an invalid object.
137137
// Can be fixed using mutex_ in both places.
138138
if (!isInLoopThread())
@@ -142,35 +142,35 @@ void EventLoop::quit()
142142
}
143143
void EventLoop::loop()
144144
{
145-
assert(!_looping);
145+
assert(!looping_);
146146
assertInLoopThread();
147-
_looping = true;
148-
_quit = false;
147+
looping_ = true;
148+
quit_ = false;
149149

150-
while (!_quit)
150+
while (!quit_)
151151
{
152-
_activeChannels.clear();
152+
activeChannels_.clear();
153153
#ifdef __linux__
154-
_poller->poll(kPollTimeMs, &_activeChannels);
154+
poller_->poll(kPollTimeMs, &activeChannels_);
155155
#else
156-
_poller->poll(_timerQueue->getTimeout(), &_activeChannels);
157-
_timerQueue->processTimers();
156+
poller_->poll(timerQueue_->getTimeout(), &activeChannels_);
157+
timerQueue_->processTimers();
158158
#endif
159159
// TODO sort channel by priority
160160
// 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();
163163
++it)
164164
{
165-
_currentActiveChannel = *it;
166-
_currentActiveChannel->handleEvent();
165+
currentActiveChannel_ = *it;
166+
currentActiveChannel_->handleEvent();
167167
}
168-
_currentActiveChannel = NULL;
169-
_eventHandling = false;
168+
currentActiveChannel_ = NULL;
169+
eventHandling_ = false;
170170
// std::cout << "looping" << endl;
171171
doRunInLoopFuncs();
172172
}
173-
_looping = false;
173+
looping_ = false;
174174
}
175175
void EventLoop::abortNotInLoopThread()
176176
{
@@ -202,28 +202,28 @@ void EventLoop::runInLoop(Func &&cb)
202202
}
203203
void EventLoop::queueInLoop(const Func &cb)
204204
{
205-
_funcs.enqueue(cb);
206-
if (!isInLoopThread() || !_looping)
205+
funcs_.enqueue(cb);
206+
if (!isInLoopThread() || !looping_)
207207
{
208208
wakeup();
209209
}
210210
}
211211
void EventLoop::queueInLoop(Func &&cb)
212212
{
213-
_funcs.enqueue(std::move(cb));
214-
if (!isInLoopThread() || !_looping)
213+
funcs_.enqueue(std::move(cb));
214+
if (!isInLoopThread() || !looping_)
215215
{
216216
wakeup();
217217
}
218218
}
219219

220220
TimerId EventLoop::runAt(const Date &time, const Func &cb)
221221
{
222-
return _timerQueue->addTimer(cb, time, 0);
222+
return timerQueue_->addTimer(cb, time, 0);
223223
}
224224
TimerId EventLoop::runAt(const Date &time, Func &&cb)
225225
{
226-
return _timerQueue->addTimer(std::move(cb), time, 0);
226+
return timerQueue_->addTimer(std::move(cb), time, 0);
227227
}
228228
TimerId EventLoop::runAfter(double delay, const Func &cb)
229229
{
@@ -235,40 +235,40 @@ TimerId EventLoop::runAfter(double delay, Func &&cb)
235235
}
236236
TimerId EventLoop::runEvery(double interval, const Func &cb)
237237
{
238-
return _timerQueue->addTimer(cb, Date::date().after(interval), interval);
238+
return timerQueue_->addTimer(cb, Date::date().after(interval), interval);
239239
}
240240
TimerId EventLoop::runEvery(double interval, Func &&cb)
241241
{
242-
return _timerQueue->addTimer(std::move(cb),
242+
return timerQueue_->addTimer(std::move(cb),
243243
Date::date().after(interval),
244244
interval);
245245
}
246246
void EventLoop::invalidateTimer(TimerId id)
247247
{
248-
if (isRunning() && _timerQueue)
249-
_timerQueue->invalidateTimer(id);
248+
if (isRunning() && timerQueue_)
249+
timerQueue_->invalidateTimer(id);
250250
}
251251
void EventLoop::doRunInLoopFuncs()
252252
{
253-
_callingFuncs = true;
253+
callingFuncs_ = true;
254254
{
255255
Func func;
256-
while (_funcs.dequeue(func))
256+
while (funcs_.dequeue(func))
257257
{
258258
func();
259259
}
260260
}
261-
_callingFuncs = false;
261+
callingFuncs_ = false;
262262
}
263263
void EventLoop::wakeup()
264264
{
265-
// if (!_looping)
265+
// if (!looping_)
266266
// return;
267267
uint64_t tmp = 1;
268268
#ifdef __linux__
269-
int ret = write(_wakeupFd, &tmp, sizeof(tmp));
269+
int ret = write(wakeupFd_, &tmp, sizeof(tmp));
270270
#else
271-
int ret = write(_wakeupFd[1], &tmp, sizeof(tmp));
271+
int ret = write(wakeupFd_[1], &tmp, sizeof(tmp));
272272
#endif
273273
if (ret < 0)
274274
{
@@ -280,9 +280,9 @@ void EventLoop::wakeupRead()
280280
uint64_t tmp;
281281
ssize_t ret = 0;
282282
#ifdef __linux__
283-
ret = read(_wakeupFd, &tmp, sizeof(tmp));
283+
ret = read(wakeupFd_, &tmp, sizeof(tmp));
284284
#else
285-
ret = read(_wakeupFd[0], &tmp, sizeof(tmp));
285+
ret = read(wakeupFd_[0], &tmp, sizeof(tmp));
286286
#endif
287287
if (ret < 0)
288288
LOG_SYSERR << "wakeup read error";

trantor/net/EventLoop.h

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ namespace trantor
3333
class Poller;
3434
class TimerQueue;
3535
class Channel;
36-
typedef std::vector<Channel *> ChannelList;
37-
typedef std::function<void()> Func;
38-
typedef uint64_t TimerId;
36+
using ChannelList = std::vector<Channel *>;
37+
using Func = std::function<void()>;
38+
using TimerId = uint64_t;
3939
enum
4040
{
4141
InvalidTimerId = 0
@@ -61,7 +61,7 @@ class EventLoop : NonCopyable
6161
void resetAfterFork();
6262
bool isInLoopThread() const
6363
{
64-
return _threadId == std::this_thread::get_id();
64+
return threadId_ == std::this_thread::get_id();
6565
};
6666
static EventLoop *getEventLoopOfCurrentThread();
6767
void updateChannel(Channel *chl);
@@ -74,18 +74,13 @@ class EventLoop : NonCopyable
7474
void wakeupRead();
7575
size_t index()
7676
{
77-
return _index;
77+
return index_;
7878
}
7979
void setIndex(size_t index)
8080
{
81-
_index = index;
81+
index_ = index;
8282
}
83-
// template <typename T>
84-
// const std::shared_ptr<ObjectPool<T>> &getObjectPool()
85-
// {
86-
// static std::shared_ptr<ObjectPool<T>>
87-
// pool=std::make_shared<ObjectPool<T>>(); return pool;
88-
// }
83+
8984
///
9085
/// Runs callback at 'time'.
9186
/// Safe to call from other threads.
@@ -103,8 +98,10 @@ class EventLoop : NonCopyable
10398
/**
10499
* Users could use chrono literals to represent a time duration
105100
* For example:
106-
* runAfter(5s, task);
107-
* runAfter(10min, task);
101+
* @code
102+
runAfter(5s, task);
103+
runAfter(10min, task);
104+
@endcode
108105
*/
109106
TimerId runAfter(const std::chrono::duration<long double> &delay,
110107
const Func &cb)
@@ -126,9 +123,11 @@ class EventLoop : NonCopyable
126123
/**
127124
* Users could use chrono literals to represent a time duration
128125
* For example:
129-
* runEvery(5s, task);
130-
* runEvery(10min, task);
131-
* runEvery(0.1h, task);
126+
* @code
127+
runEvery(5s, task);
128+
runEvery(10min, task);
129+
runEvery(0.1h, task);
130+
@endcode
132131
*/
133132
TimerId runEvery(const std::chrono::duration<long double> &interval,
134133
const Func &cb)
@@ -146,39 +145,36 @@ class EventLoop : NonCopyable
146145

147146
bool isRunning()
148147
{
149-
return _looping && (!_quit);
148+
return looping_ && (!quit_);
150149
}
151150
bool isCallingFunctions()
152151
{
153-
return _callingFuncs;
152+
return callingFuncs_;
154153
}
155154

156155
private:
157156
void abortNotInLoopThread();
158-
bool _looping;
159-
const std::thread::id _threadId;
160-
bool _quit;
161-
std::unique_ptr<Poller> _poller;
162-
163-
ChannelList _activeChannels;
164-
Channel *_currentActiveChannel;
165-
166-
bool _eventHandling;
167-
168-
// std::mutex _funcsMutex;
169-
170-
MpscQueue<Func> _funcs;
171-
std::unique_ptr<TimerQueue> _timerQueue;
172-
bool _callingFuncs = false;
157+
bool looping_;
158+
const std::thread::id threadId_;
159+
bool quit_;
160+
std::unique_ptr<Poller> poller_;
161+
162+
ChannelList activeChannels_;
163+
Channel *currentActiveChannel_;
164+
165+
bool eventHandling_;
166+
MpscQueue<Func> funcs_;
167+
std::unique_ptr<TimerQueue> timerQueue_;
168+
bool callingFuncs_{false};
173169
#ifdef __linux__
174-
int _wakeupFd;
170+
int wakeupFd_;
175171
#else
176-
int _wakeupFd[2];
172+
int wakeupFd_[2];
177173
#endif
178-
std::unique_ptr<Channel> _wakeupChannelPtr;
174+
std::unique_ptr<Channel> wakeupChannelPtr_;
179175

180176
void doRunInLoopFuncs();
181-
size_t _index = std::numeric_limits<size_t>::max();
177+
size_t index_{std::numeric_limits<size_t>::max()};
182178
};
183179

184180
} // namespace trantor

0 commit comments

Comments
 (0)