Skip to content

Commit 28694d8

Browse files
authored
Give EventLoopThread::loop_ static lifetime (#212)
1 parent c1e57a0 commit 28694d8

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

trantor/net/EventLoop.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,6 @@ void EventLoop::quit()
165165
{
166166
quit_.store(true, std::memory_order_release);
167167

168-
Func f;
169-
while (funcsOnQuit_.dequeue(f))
170-
{
171-
f();
172-
}
173-
174-
// There is a chance that loop() just executes while(!quit_) and exits,
175-
// then EventLoop destructs, then we are accessing an invalid object.
176-
// Can be fixed using mutex_ in both places.
177168
if (!isInLoopThread())
178169
{
179170
wakeup();
@@ -205,12 +196,18 @@ void EventLoop::loop()
205196
currentActiveChannel_ = *it;
206197
currentActiveChannel_->handleEvent();
207198
}
208-
currentActiveChannel_ = NULL;
199+
currentActiveChannel_ = nullptr;
209200
eventHandling_ = false;
210201
// std::cout << "looping" << endl;
211202
doRunInLoopFuncs();
212203
}
213204
looping_.store(false, std::memory_order_release);
205+
206+
Func f;
207+
while (funcsOnQuit_.dequeue(f))
208+
{
209+
f();
210+
}
214211
}
215212
void EventLoop::abortNotInLoopThread()
216213
{

trantor/net/EventLoopThread.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ EventLoopThread::EventLoopThread(const std::string &threadName)
2727
auto f = promiseForLoopPointer_.get_future();
2828
loop_ = f.get();
2929
}
30+
3031
EventLoopThread::~EventLoopThread()
3132
{
3233
run();
@@ -39,28 +40,27 @@ EventLoopThread::~EventLoopThread()
3940
thread_.join();
4041
}
4142
}
42-
// void EventLoopThread::stop() {
43-
// if(loop_)
44-
// loop_->quit();
45-
//}
43+
4644
void EventLoopThread::wait()
4745
{
4846
thread_.join();
4947
}
48+
5049
void EventLoopThread::loopFuncs()
5150
{
5251
#ifdef __linux__
5352
::prctl(PR_SET_NAME, loopThreadName_.c_str());
5453
#endif
55-
EventLoop loop;
54+
thread_local static EventLoop loop;
5655
loop.queueInLoop([this]() { promiseForLoop_.set_value(1); });
5756
promiseForLoopPointer_.set_value(&loop);
5857
auto f = promiseForRun_.get_future();
5958
(void)f.get();
6059
loop.loop();
6160
// LOG_DEBUG << "loop out";
62-
loop_ = NULL;
61+
loop_ = nullptr;
6362
}
63+
6464
void EventLoopThread::run()
6565
{
6666
std::call_once(once_, [this]() {

0 commit comments

Comments
 (0)