@@ -51,7 +51,7 @@ void LoggingErrorHandler::taskFailed(kj::Exception&& exception)
51
51
EventLoopRef::EventLoopRef (EventLoop& loop, std::unique_lock<std::mutex>* lock) : m_loop(&loop), m_lock(lock)
52
52
{
53
53
auto loop_lock{PtrOrValue{m_lock, m_loop->m_mutex }};
54
- m_loop->addClient (*loop_lock) ;
54
+ m_loop->m_num_clients += 1 ;
55
55
}
56
56
57
57
bool EventLoopRef::reset ()
@@ -60,7 +60,18 @@ bool EventLoopRef::reset()
60
60
if (auto * loop{m_loop}) {
61
61
m_loop = nullptr ;
62
62
auto loop_lock{PtrOrValue{m_lock, loop->m_mutex }};
63
- done = loop->removeClient (*loop_lock);
63
+ assert (loop->m_num_clients > 0 );
64
+ loop->m_num_clients -= 1 ;
65
+ if (loop->done (*loop_lock)) {
66
+ done = true ;
67
+ loop->m_cv .notify_all ();
68
+ int post_fd{loop->m_post_fd };
69
+ loop_lock->unlock ();
70
+ char buffer = 0 ;
71
+ KJ_SYSCALL (write (post_fd, &buffer, 1 )); // NOLINT(bugprone-suspicious-semicolon)
72
+ // Do not try to relock `loop_lock` after writing, because the event loop
73
+ // could wake up and destroy itself and the mutex might no longer exist.
74
+ }
64
75
}
65
76
return done;
66
77
}
@@ -220,7 +231,7 @@ void EventLoop::loop()
220
231
m_cv.notify_all ();
221
232
} else if (done (lock)) {
222
233
// Intentionally do not break if m_post_fn was set, even if done()
223
- // would return true, to ensure that the removeClient write(post_fd)
234
+ // would return true, to ensure that the EventLoopRef write(post_fd)
224
235
// call always succeeds and the loop does not exit between the time
225
236
// that the done condition is set and the write call is made.
226
237
break ;
@@ -254,25 +265,6 @@ void EventLoop::post(const std::function<void()>& fn)
254
265
m_cv.wait (lock, [this , &fn] { return m_post_fn != &fn; });
255
266
}
256
267
257
- void EventLoop::addClient (std::unique_lock<std::mutex>& lock) { m_num_clients += 1 ; }
258
-
259
- bool EventLoop::removeClient (std::unique_lock<std::mutex>& lock)
260
- {
261
- assert (m_num_clients > 0 );
262
- m_num_clients -= 1 ;
263
- if (done (lock)) {
264
- m_cv.notify_all ();
265
- int post_fd{m_post_fd};
266
- lock.unlock ();
267
- char buffer = 0 ;
268
- KJ_SYSCALL (write (post_fd, &buffer, 1 )); // NOLINT(bugprone-suspicious-semicolon)
269
- // Do not try to relock `lock` after writing, because the event loop
270
- // could wake up and destroy itself and the mutex might no longer exist.
271
- return true ;
272
- }
273
- return false ;
274
- }
275
-
276
268
void EventLoop::startAsyncThread (std::unique_lock<std::mutex>& lock)
277
269
{
278
270
if (m_async_thread.joinable ()) {
0 commit comments