Skip to content

Commit 255976d

Browse files
authored
Add runOnQuit (#153)
1 parent 698ad72 commit 255976d

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

trantor/net/EventLoop.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void EventLoop::resetAfterFork()
111111
}
112112
EventLoop::~EventLoop()
113113
{
114+
quit();
114115
assert(!looping_);
115116
t_loopInThisThread = nullptr;
116117
#ifdef __linux__
@@ -147,6 +148,13 @@ void EventLoop::removeChannel(Channel *channel)
147148
void EventLoop::quit()
148149
{
149150
quit_ = true;
151+
152+
Func f;
153+
while (funcsOnQuit_.dequeue(f))
154+
{
155+
f();
156+
}
157+
150158
// There is a chance that loop() just executes while(!quit_) and exits,
151159
// then EventLoop destructs, then we are accessing an invalid object.
152160
// Can be fixed using mutex_ in both places.
@@ -351,4 +359,14 @@ void EventLoop::moveToCurrentThread()
351359
threadId_ = std::this_thread::get_id();
352360
}
353361

362+
void EventLoop::runOnQuit(Func &&cb)
363+
{
364+
funcsOnQuit_.enqueue(std::move(cb));
365+
}
366+
367+
void EventLoop::runOnQuit(const Func &cb)
368+
{
369+
funcsOnQuit_.enqueue(cb);
370+
}
371+
354372
} // namespace trantor

trantor/net/EventLoop.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ class TRANTOR_EXPORT EventLoop : NonCopyable
274274
return callingFuncs_;
275275
}
276276

277+
/**
278+
* @brief Run functions when the event loop quits
279+
*
280+
* @param cb the function to run
281+
* @note the function runs on the thread that quits the EventLoop
282+
*/
283+
void runOnQuit(Func &&cb);
284+
void runOnQuit(const Func &cb);
285+
277286
private:
278287
void abortNotInLoopThread();
279288
void wakeup();
@@ -289,6 +298,7 @@ class TRANTOR_EXPORT EventLoop : NonCopyable
289298
bool eventHandling_;
290299
MpscQueue<Func> funcs_;
291300
std::unique_ptr<TimerQueue> timerQueue_;
301+
MpscQueue<Func> funcsOnQuit_;
292302
bool callingFuncs_{false};
293303
#ifdef __linux__
294304
int wakeupFd_;

trantor/tests/CMakeLists.txt

100755100644
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_executable(kickoff_test KickoffTest.cc)
1818
add_executable(dns_test DnsTest.cc)
1919
add_executable(delayed_ssl_server_test DelayedSSLServerTest.cc)
2020
add_executable(delayed_ssl_client_test DelayedSSLClientTest.cc)
21+
add_executable(run_on_quit_test RunOnQuitTest.cc)
2122
set(targets_list
2223
ssl_server_test
2324
ssl_client_test
@@ -38,7 +39,8 @@ set(targets_list
3839
kickoff_test
3940
dns_test
4041
delayed_ssl_server_test
41-
delayed_ssl_client_test)
42+
delayed_ssl_client_test
43+
run_on_quit_test)
4244

4345
set_property(TARGET ${targets_list} PROPERTY CXX_STANDARD 14)
4446
set_property(TARGET ${targets_list} PROPERTY CXX_STANDARD_REQUIRED ON)

trantor/tests/RunOnQuitTest.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <trantor/net/EventLoopThread.h>
2+
#include <iostream>
3+
#include <atomic>
4+
#include <future>
5+
#ifndef _WIN32
6+
#include <unistd.h>
7+
#endif
8+
9+
int main()
10+
{
11+
std::atomic<bool> flag(false);
12+
{
13+
trantor::EventLoopThread thr;
14+
thr.getLoop()->runOnQuit([&]() { flag = true; });
15+
thr.run();
16+
thr.getLoop()->quit();
17+
}
18+
19+
if (flag == false)
20+
{
21+
std::cerr << "Test failed\n";
22+
}
23+
else
24+
{
25+
std::cout << "Success\n";
26+
}
27+
}

0 commit comments

Comments
 (0)