@@ -51,15 +51,24 @@ 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 (std::unique_lock<std::mutex>* lock)
58
58
{
59
59
bool done = false ;
60
60
if (m_loop) {
61
61
auto loop_lock{PtrOrValue{lock ? lock : m_lock, m_loop->m_mutex }};
62
- done = m_loop->removeClient (*loop_lock);
62
+ assert (m_loop->m_num_clients > 0 );
63
+ m_loop->m_num_clients -= 1 ;
64
+ if (m_loop->done (*loop_lock)) {
65
+ done = true ;
66
+ m_loop->m_cv .notify_all ();
67
+ int post_fd{m_loop->m_post_fd };
68
+ loop_lock->unlock ();
69
+ char buffer = 0 ;
70
+ KJ_SYSCALL (write (post_fd, &buffer, 1 )); // NOLINT(bugprone-suspicious-semicolon)
71
+ }
63
72
m_loop = nullptr ;
64
73
}
65
74
return done;
@@ -221,7 +230,7 @@ void EventLoop::loop()
221
230
m_cv.notify_all ();
222
231
} else if (done (lock)) {
223
232
// Intentionally do not break if m_post_fn was set, even if done()
224
- // would return true, to ensure that the removeClient write(post_fd)
233
+ // would return true, to ensure that the EventLoopRef write(post_fd)
225
234
// call always succeeds and the loop does not exit between the time
226
235
// that the done condition is set and the write call is made.
227
236
break ;
@@ -255,23 +264,6 @@ void EventLoop::post(const std::function<void()>& fn)
255
264
m_cv.wait (lock, [this , &fn] { return m_post_fn != &fn; });
256
265
}
257
266
258
- void EventLoop::addClient (std::unique_lock<std::mutex>& lock) { m_num_clients += 1 ; }
259
-
260
- bool EventLoop::removeClient (std::unique_lock<std::mutex>& lock)
261
- {
262
- assert (m_num_clients > 0 );
263
- m_num_clients -= 1 ;
264
- if (done (lock)) {
265
- m_cv.notify_all ();
266
- int post_fd{m_post_fd};
267
- lock.unlock ();
268
- char buffer = 0 ;
269
- KJ_SYSCALL (write (post_fd, &buffer, 1 )); // NOLINT(bugprone-suspicious-semicolon)
270
- return true ;
271
- }
272
- return false ;
273
- }
274
-
275
267
void EventLoop::startAsyncThread (std::unique_lock<std::mutex>& lock)
276
268
{
277
269
if (m_async_thread.joinable ()) {
0 commit comments