Skip to content

Commit f248947

Browse files
committed
refactor: Drop addClient/removeClient methods
Now that they are only called in one place by EventLoopRef class, they can be inlined.
1 parent 2b830e5 commit f248947

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

include/mp/proxy-io.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ class EventLoop
190190
//! other IPC calls.
191191
void startAsyncThread(std::unique_lock<std::mutex>& lock);
192192

193-
//! Add/remove remote client reference counts. Can use EventLoopRef
194-
//! below to avoid calling these directly.
195-
void addClient(std::unique_lock<std::mutex>& lock);
196-
bool removeClient(std::unique_lock<std::mutex>& lock);
197193
//! Check if loop should exit.
198194
bool done(std::unique_lock<std::mutex>& lock) const;
199195

include/mp/proxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ inline void CleanupRun(CleanupList& fns) {
4848
}
4949
}
5050

51-
//! Event loop smart pointer automatically calling addClient and removeClient.
51+
//! Event loop smart pointer automatically managing m_num_clients.
5252
//! If a lock pointer argument is passed, the specified lock will be used,
5353
//! otherwise EventLoop::m_mutex will be locked when needed.
5454
class EventLoopRef

src/mp/proxy.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void LoggingErrorHandler::taskFailed(kj::Exception&& exception)
5151
EventLoopRef::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

5757
bool 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-
276268
void EventLoop::startAsyncThread(std::unique_lock<std::mutex>& lock)
277269
{
278270
if (m_async_thread.joinable()) {

0 commit comments

Comments
 (0)