File tree Expand file tree Collapse file tree 4 files changed +58
-1
lines changed Expand file tree Collapse file tree 4 files changed +58
-1
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,7 @@ void EventLoop::resetAfterFork()
111
111
}
112
112
EventLoop::~EventLoop ()
113
113
{
114
+ quit ();
114
115
assert (!looping_);
115
116
t_loopInThisThread = nullptr ;
116
117
#ifdef __linux__
@@ -147,6 +148,13 @@ void EventLoop::removeChannel(Channel *channel)
147
148
void EventLoop::quit ()
148
149
{
149
150
quit_ = true ;
151
+
152
+ Func f;
153
+ while (funcsOnQuit_.dequeue (f))
154
+ {
155
+ f ();
156
+ }
157
+
150
158
// There is a chance that loop() just executes while(!quit_) and exits,
151
159
// then EventLoop destructs, then we are accessing an invalid object.
152
160
// Can be fixed using mutex_ in both places.
@@ -351,4 +359,14 @@ void EventLoop::moveToCurrentThread()
351
359
threadId_ = std::this_thread::get_id ();
352
360
}
353
361
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
+
354
372
} // namespace trantor
Original file line number Diff line number Diff line change @@ -274,6 +274,15 @@ class TRANTOR_EXPORT EventLoop : NonCopyable
274
274
return callingFuncs_;
275
275
}
276
276
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
+
277
286
private:
278
287
void abortNotInLoopThread ();
279
288
void wakeup ();
@@ -289,6 +298,7 @@ class TRANTOR_EXPORT EventLoop : NonCopyable
289
298
bool eventHandling_;
290
299
MpscQueue<Func> funcs_;
291
300
std::unique_ptr<TimerQueue> timerQueue_;
301
+ MpscQueue<Func> funcsOnQuit_;
292
302
bool callingFuncs_{false };
293
303
#ifdef __linux__
294
304
int wakeupFd_;
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ add_executable(kickoff_test KickoffTest.cc)
18
18
add_executable (dns_test DnsTest.cc )
19
19
add_executable (delayed_ssl_server_test DelayedSSLServerTest.cc )
20
20
add_executable (delayed_ssl_client_test DelayedSSLClientTest.cc )
21
+ add_executable (run_on_quit_test RunOnQuitTest.cc )
21
22
set (targets_list
22
23
ssl_server_test
23
24
ssl_client_test
@@ -38,7 +39,8 @@ set(targets_list
38
39
kickoff_test
39
40
dns_test
40
41
delayed_ssl_server_test
41
- delayed_ssl_client_test )
42
+ delayed_ssl_client_test
43
+ run_on_quit_test )
42
44
43
45
set_property (TARGET ${targets_list} PROPERTY CXX_STANDARD 14 )
44
46
set_property (TARGET ${targets_list} PROPERTY CXX_STANDARD_REQUIRED ON )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments