Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: EventLoop locking cleanups #160

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ryanofsky
Copy link
Collaborator

This change introduces RAII reference counting to the EventLoop class to simplify a recent bugfix in #159. It also deletes DestructorCatcher and AsyncCallable helper classes as discussed in #144 (comment) and it adds clang thread safety annotations as discussed in #129.

ryanofsky and others added 5 commits February 9, 2025 23:25
Use EventLoopRef to avoid reference counting bugs and be more exception safe
Now that they are only called in one place by EventLoopRef class, they can be
inlined.
Use kj::Function instead of std::function to avoid the need for AsyncCallable
and DestructorCatcher classes, which were used to work around the requirement
that std::function objects need to be copyable. kj::Function does not have this
requirement.

Change is from bitcoin-core#144 (comment)

Co-authored-by: Ryan Ofsky <[email protected]>
Add basic thread safety annotations to EventLoop. Use could be expanded to
other functions. Use can be expanded and deepened but this puts basic
functionality in place.

Use of annotations was discussed in
bitcoin-core#129 (comment)
@ryanofsky
Copy link
Collaborator Author

Because this change removes EventLoop addClient and removeClient methods it will require an update to Bitcoin core if it is merged.

--- a/src/ipc/capnp/protocol.cpp
+++ b/src/ipc/capnp/protocol.cpp
@@ -41,10 +41,7 @@ class CapnpProtocol : public Protocol
 public:
     ~CapnpProtocol() noexcept(true)
     {
-        if (m_loop) {
-            std::unique_lock<std::mutex> lock(m_loop->m_mutex);
-            m_loop->removeClient(lock);
-        }
+        m_loop_ref.reset();
         if (m_loop_thread.joinable()) m_loop_thread.join();
         assert(!m_loop);
     };
@@ -83,10 +80,7 @@ public:
         m_loop_thread = std::thread([&] {
             util::ThreadRename("capnp-loop");
             m_loop.emplace(exe_name, &IpcLogFn, &m_context);
-            {
-                std::unique_lock<std::mutex> lock(m_loop->m_mutex);
-                m_loop->addClient(lock);
-            }
+            m_loop_ref.emplace(*m_loop);
             promise.set_value();
             m_loop->loop();
             m_loop.reset();
@@ -96,6 +90,7 @@ public:
     Context m_context;
     std::thread m_loop_thread;
     std::optional<mp::EventLoop> m_loop;
+    std::optional<mp::EventLoopRef> m_loop_ref;
 };
 } // namespace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants