Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions include/mp/proxy-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class EventLoop

//! Run event loop. Does not return until shutdown. This should only be
//! called once from the m_thread_id thread. This will block until
//! the m_num_clients reference count is 0.
//! the m_num_refs reference count is 0.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In "refactor: rename EventLoop::m_num_clients to m_num_refs" d499830

nit: "the m_num_refs reference count" is a bit redundant now because "refs" already means references. Could be renamed to "the m_num_refs count is 0" (unrelated but this condition is also incomplete: loop() exits on done(), m_num_refs == 0 and m_async_fns is empty).

void loop();

//! Run function on event loop thread. Does not return until function completes.
Expand Down Expand Up @@ -314,9 +314,13 @@ class EventLoop
//! Pipe write handle used to wake up the event loop thread.
int m_post_fd = -1;

//! Number of clients holding references to ProxyServerBase objects that
//! reference this event loop.
int m_num_clients MP_GUARDED_BY(m_mutex) = 0;
//! Number of EventLoopRef instances referencing this event loop. This is a
//! sum of the number of client and server objects (Connection, ProxyClient,
//! ProxyServer) using the loop, plus temporary references held while
//! posting functions to the loop, plus any references held by external code
//! to keep the loop running. The loop() method exits when this count drops
//! to 0 (and m_async_fns is empty).
int m_num_refs MP_GUARDED_BY(m_mutex) = 0;

//! Mutex and condition variable used to post tasks to event loop and async
//! thread.
Expand Down
2 changes: 1 addition & 1 deletion include/mp/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inline void CleanupRun(CleanupList& fns) {
}
}

//! Event loop smart pointer automatically managing m_num_clients.
//! Event loop smart pointer automatically managing m_num_refs.
//! If a lock pointer argument is passed, the specified lock will be used,
//! otherwise EventLoop::m_mutex will be locked when needed.
class EventLoopRef
Expand Down
12 changes: 6 additions & 6 deletions src/mp/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ EventLoopRef::EventLoopRef(EventLoop& loop, Lock* lock) : m_loop(&loop), m_lock(
{
auto loop_lock{PtrOrValue{m_lock, m_loop->m_mutex}};
loop_lock->assert_locked(m_loop->m_mutex);
m_loop->m_num_clients += 1;
m_loop->m_num_refs += 1;
}

// Due to the conditionals in this function, MP_NO_TSA is required to avoid
Expand All @@ -63,8 +63,8 @@ void EventLoopRef::reset(bool relock) MP_NO_TSA
m_loop = nullptr;
auto loop_lock{PtrOrValue{m_lock, loop->m_mutex}};
loop_lock->assert_locked(loop->m_mutex);
assert(loop->m_num_clients > 0);
loop->m_num_clients -= 1;
assert(loop->m_num_refs > 0);
loop->m_num_refs -= 1;
if (loop->done()) {
loop->m_cv.notify_all();
int post_fd{loop->m_post_fd};
Expand Down Expand Up @@ -218,7 +218,7 @@ EventLoop::~EventLoop()
KJ_ASSERT(!m_async_fns);
KJ_ASSERT(m_wait_fd == -1);
KJ_ASSERT(m_post_fd == -1);
KJ_ASSERT(m_num_clients == 0);
KJ_ASSERT(m_num_refs == 0);

// Spin event loop. wait for any promises triggered by RPC shutdown.
// auto cleanup = kj::evalLater([]{});
Expand Down Expand Up @@ -320,8 +320,8 @@ void EventLoop::startAsyncThread()

bool EventLoop::done() const
{
assert(m_num_clients >= 0);
return m_num_clients == 0 && m_async_fns->empty();
assert(m_num_refs >= 0);
return m_num_refs == 0 && m_async_fns->empty();
}

std::tuple<ConnThread, bool> SetThread(GuardedRef<ConnThreads> threads, Connection* connection, const std::function<Thread::Client()>& make_thread)
Expand Down
Loading