@@ -51,7 +51,7 @@ void LoggingErrorHandler::taskFailed(kj::Exception&& exception)
5151EventLoopRef::EventLoopRef (EventLoop& loop, std::unique_lock<std::mutex>* lock) : m_loop(&loop), m_lock(lock)
5252{
5353 auto loop_lock{PtrOrValue{m_lock, m_loop->m_mutex }};
54- m_loop->addClient (*loop_lock) ;
54+ m_loop->m_num_clients += 1 ;
5555}
5656
5757bool EventLoopRef::reset ()
@@ -60,7 +60,18 @@ bool EventLoopRef::reset()
6060 if (auto * loop{m_loop}) {
6161 m_loop = nullptr ;
6262 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+ }
6475 }
6576 return done;
6677}
@@ -220,7 +231,7 @@ void EventLoop::loop()
220231 m_cv.notify_all ();
221232 } else if (done (lock)) {
222233 // 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)
224235 // call always succeeds and the loop does not exit between the time
225236 // that the done condition is set and the write call is made.
226237 break ;
@@ -254,25 +265,6 @@ void EventLoop::post(const std::function<void()>& fn)
254265 m_cv.wait (lock, [this , &fn] { return m_post_fn != &fn; });
255266}
256267
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-
276268void EventLoop::startAsyncThread (std::unique_lock<std::mutex>& lock)
277269{
278270 if (m_async_thread.joinable ()) {
0 commit comments