Skip to content

Commit 5b317c9

Browse files
authored
Scope the Func object and check for empty funcs_ queue (#112)
1 parent 17acd34 commit 5b317c9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

trantor/net/EventLoop.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,15 @@ void EventLoop::doRunInLoopFuncs()
285285
{
286286
callingFuncs_ = true;
287287
{
288-
Func func;
289-
while (funcs_.dequeue(func))
288+
// the destructor for the Func may itself insert a new entry into the
289+
// queue
290+
while (!funcs_.empty())
290291
{
291-
func();
292+
Func func;
293+
while (funcs_.dequeue(func))
294+
{
295+
func();
296+
}
292297
}
293298
}
294299
callingFuncs_ = false;

trantor/utils/LockFreeQueue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ class MpscQueue : public NonCopyable
8686
return true;
8787
}
8888

89+
bool empty()
90+
{
91+
BufferNode *tail = tail_.load(std::memory_order_relaxed);
92+
BufferNode *next = tail->next_.load(std::memory_order_acquire);
93+
return next == nullptr;
94+
}
95+
8996
private:
9097
struct BufferNode
9198
{

0 commit comments

Comments
 (0)