Skip to content

Commit 865a10c

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 c29384c commit 865a10c

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
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: 12 additions & 20 deletions
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)