Skip to content

Commit 276eb8f

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 025a77e commit 276eb8f

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

include/mp/proxy-io.h

-4
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,6 @@ class EventLoop
187187
//! other IPC calls.
188188
void startAsyncThread(std::unique_lock<std::mutex>& lock);
189189

190-
//! Add/remove remote client reference counts. Can use EventLoopRef
191-
//! below to avoid calling these directly.
192-
void addClient(std::unique_lock<std::mutex>& lock);
193-
bool removeClient(std::unique_lock<std::mutex>& lock);
194190
//! Check if loop should exit.
195191
bool done(std::unique_lock<std::mutex>& lock);
196192

include/mp/proxy.h

+1-1
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

+12-20
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,24 @@ 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(std::unique_lock<std::mutex>* lock)
5858
{
5959
bool done = false;
6060
if (m_loop) {
6161
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+
}
6372
m_loop = nullptr;
6473
}
6574
return done;
@@ -221,7 +230,7 @@ void EventLoop::loop()
221230
m_cv.notify_all();
222231
} else if (done(lock)) {
223232
// 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)
225234
// call always succeeds and the loop does not exit between the time
226235
// that the done condition is set and the write call is made.
227236
break;
@@ -255,23 +264,6 @@ void EventLoop::post(const std::function<void()>& fn)
255264
m_cv.wait(lock, [this, &fn] { return m_post_fn != &fn; });
256265
}
257266

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

0 commit comments

Comments
 (0)