Skip to content
Draft
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
98 changes: 52 additions & 46 deletions include/mp/proxy-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ struct ProxyClient<Thread> : public ProxyClientBase<Thread, ::capnp::Void>
//! map. It will also reset m_disconnect_cb so the destructor does not
//! access it. In the normal case where there is no sudden disconnect, the
//! destructor will unregister m_disconnect_cb so the callback is never run.
//! Since this variable is accessed from multiple threads, accesses should
//! be guarded with the associated Waiter::m_mutex.
//! Registration and removal of this callback run on the associated event
//! loop thread.
std::optional<CleanupIt> m_disconnect_cb;
};

Expand Down Expand Up @@ -271,11 +271,12 @@ class EventLoop
void sync(kj::FunctionParam<void()> fn);

//! Register cleanup function to run on asynchronous worker thread without
//! blocking the event loop thread.
void addAsyncCleanup(std::function<void()> fn);
//! blocking the event loop thread. May be called from any thread while loop()
//! is active. Does not wait for cleanup or for the async thread to start.
void addAsyncCleanup(kj::Function<void()> fn);

//! Start asynchronous worker thread if necessary. This is only done if
//! there are ProxyServerBase::m_impl objects that need to be destroyed
//! there are thread clients or ProxyServerBase::m_impl objects to destroy
//! asynchronously, without tying up the event loop thread. This can happen
//! when an interface does not declare a destroy() method that would allow
//! the client to wait for the destructor to finish and run it on a
Expand Down Expand Up @@ -305,7 +306,7 @@ class EventLoop
kj::FunctionParam<void()>* m_sync_fn MP_GUARDED_BY(m_mutex) = nullptr;

//! Callback functions to run on async thread.
std::optional<CleanupList> m_async_fns MP_GUARDED_BY(m_mutex);
std::optional<AsyncCleanupList> m_async_fns MP_GUARDED_BY(m_mutex);

//! Socket pair used to post and wait for wakeups to the event loop thread.
kj::Own<kj::AsyncIoStream> m_wait_stream;
Expand Down Expand Up @@ -418,13 +419,8 @@ struct Waiter
});
}

//! Mutex mainly used internally by waiter class, but also used externally
//! to guard access to related state. Specifically, since the thread_local
//! ThreadContext struct owns a Waiter, the Waiter::m_mutex is used to guard
//! access to other parts of the struct to avoid needing to deal with more
//! mutexes than necessary. This mutex can be held at the same time as
//! EventLoop::m_mutex as long as Waiter::mutex is locked first and
//! EventLoop::m_mutex is locked second.
//! Mutex guarding the waiter's pending function and shutdown signal.
//! May be locked before ThreadClients::mutex or EventLoop::m_mutex.
Mutex m_mutex;
std::condition_variable m_cv MP_GUARDED_BY(m_mutex);
std::optional<kj::Function<void()>> m_fn MP_GUARDED_BY(m_mutex);
Expand Down Expand Up @@ -704,7 +700,7 @@ void ProxyServerBase<Interface, Impl>::invokeDestroy()
//! contain multiple if a single thread makes IPC calls over multiple
//! connections. A std::optional value type is used to avoid the map needing to
//! be locked while ProxyClient<Thread> objects are constructed, see
//! ThreadContext "Synchronization note" below.
//! ThreadContext::clients synchronization note below.
using ConnThreads = std::map<Connection*, std::optional<ProxyClient<Thread>>>;
using ConnThread = ConnThreads::iterator;

Expand All @@ -713,6 +709,31 @@ using ConnThread = ConnThreads::iterator;
// inserted bool.
std::tuple<ConnThread, bool> SetThread(GuardedRef<ConnThreads> threads, Connection* connection, const std::function<Thread::Client()>& make_thread);

//! Heap-owned thread clients and their mutex. Disconnect callbacks refer to
//! these maps, so they must outlive ThreadContext when cleanup is deferred.
struct ThreadClients
{
//! Release all clients on their respective event loops without holding the
//! map mutex while waiting. May race with disconnect callbacks.
void clear();

//! May be locked before EventLoop::m_mutex, never after it.
Mutex mutex;
//! Local thread handles passed as Context.callbackThread so the server can
//! call back into the waiting caller, keyed by connection.
ConnThreads callback_threads MP_GUARDED_BY(mutex);
//! Thread handles selecting the server execution thread (Context.thread),
//! keyed by connection: remote workers created by ThreadMap.makeThread, or
//! the caller's callbackThread while this thread handles a server request.
ConnThreads request_threads MP_GUARDED_BY(mutex);

private:
//! Pin an event loop with remaining clients, or return nullopt if empty.
std::optional<EventLoopRef> pinLoop(const ConnThreads& threads);
//! Extract this loop's clients under the mutex, returning them after unlocking.
ConnThreads extractClients(ConnThreads& threads, const EventLoop& loop);
};

//! The thread_local ThreadContext struct (see CurrentThread()) provides information
//! about individual threads and a way of communicating between them. Because
//! it's a thread local struct, each ThreadContext instance is initialized by
Expand All @@ -728,6 +749,8 @@ std::tuple<ConnThread, bool> SetThread(GuardedRef<ConnThreads> threads, Connecti
//! with local and remote ProxyClient<Thread> objects.
struct ThreadContext
{
~ThreadContext();

//! Identifying string for debug.
std::string thread_name;

Expand All @@ -748,35 +771,18 @@ struct ThreadContext
//! destroyed once for the lifetime of the thread.
std::unique_ptr<Waiter> waiter = nullptr;

//! When client is making a request to a server, this is the
//! `callbackThread` argument it passes in the request, used by the server
//! in case it needs to make callbacks into the client that need to execute
//! while the client is waiting. This will be set to a local thread object.
//! Per-connection thread handles and their mutex.
//!
//! Synchronization note: The callback_thread and request_thread maps are
//! only ever accessed internally by this thread's destructor and externally
//! by Cap'n Proto event loop threads. Since it's possible for IPC client
//! threads to make calls over different connections that could have
//! different event loops, these maps are guarded by Waiter::m_mutex in case
//! different event loop threads add or remove map entries simultaneously.
//! However, individual ProxyClient<Thread> objects in the maps will only be
//! associated with one event loop and guarded by EventLoop::m_mutex. So
//! Waiter::m_mutex does not need to be held while accessing individual
//! ProxyClient<Thread> instances, and may even need to be released to
//! respect lock order and avoid locking Waiter::m_mutex before
//! EventLoop::m_mutex.
ConnThreads callback_threads MP_GUARDED_BY(waiter->m_mutex);

//! When client is making a request to a server, this is the `thread`
//! argument it passes in the request, used to control which thread on
//! server will be responsible for executing it. If client call is being
//! made from a local thread, this will be a remote thread object returned
//! by makeThread. If a client call is being made from a thread currently
//! handling a server request, this will be set to the `callbackThread`
//! request thread argument passed in that request.
//! Synchronization note: Maps are accessed by cleanup code and by event loops
//! from potentially different connections, so ThreadClients::mutex guards
//! shared map access. Each client's Cap'n Proto state is accessed only on its
//! own event loop. The mutex must not be held while waiting for an event
//! loop, since disconnect callbacks lock it too. Deferred cleanup extracts
//! clients under the map mutex and destroys them after unlocking it.
//!
//! Synchronization note: \ref callback_threads note applies here as well.
ConnThreads request_threads MP_GUARDED_BY(waiter->m_mutex);
//! Heap storage lets the destructor hand cleanup to an async thread without
//! invalidating the map and mutex references captured by SetThread().
std::unique_ptr<ThreadClients> clients = std::make_unique<ThreadClients>();

//! Whether this thread is a capnp event loop thread. Not really used except
//! to assert false if there's an attempt to execute a blocking operation
Expand Down Expand Up @@ -830,10 +836,10 @@ kj::Promise<T> ProxyServer<Thread>::post(Fn&& fn)
result_value.reset();
}
result_fulfiller = nullptr;
// Use evalLater to destroy the ProxyServer<Thread> self
// reference, if it is the last reference, because the
// ProxyServer<Thread> destructor needs to join the thread,
// which can't happen until this sync() block has exited.
// Defer releasing self to keep the server alive until this
// result-delivery callback returns, keeping shutdown outside
// the callback. This is a lifetime precaution; the asynchronous
// join does not require the deferral.
m_loop->m_task_set->add(kj::evalLater([self = kj::mv(self)] {}));
});
});
Expand Down Expand Up @@ -963,7 +969,7 @@ extern thread_local ThreadContext g_thread_context; // NOLINT(bitcoin-nontrivial
//! process boundary and callbacks from the server run on the originating
//! client thread. The client-side handles for those dedicated server threads
//! (the ProxyClient<Thread> objects returned by ThreadMap.makeThread, stored
//! per connection in the request_threads / callback_threads maps below) are
//! per connection in the request_threads / callback_threads maps) are
//! state that must be keyed implicitly by the calling thread, and must be
//! released when the client thread exits so the corresponding server threads
//! are freed. A thread_local object is the C++ mechanism that provides both
Expand Down
2 changes: 2 additions & 0 deletions include/mp/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <cassert>
#include <functional>
#include <kj/function.h>
#include <list>
#include <memory>
#include <stddef.h>
Expand All @@ -33,6 +34,7 @@ template <typename Struct> struct ProxyStruct; // IWYU pragma: export
//! Mapping from local c++ type to capnp type and traits (specializations are generated by proxy-codegen.cpp).
template <typename Type> struct ProxyType; // IWYU pragma: export

using AsyncCleanupList = std::list<kj::Function<void()>>;
using CleanupList = std::list<std::function<void()>>;
using CleanupIt = typename CleanupList::iterator;

Expand Down
12 changes: 6 additions & 6 deletions include/mp/type-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void CustomBuildField(TypeList<>,
// Also store the Thread::Client reference in the callback_threads map so
// future calls over this connection can reuse it.
auto [callback_thread, _]{SetThread(
GuardedRef{thread_context.waiter->m_mutex, thread_context.callback_threads}, &connection,
GuardedRef{thread_context.clients->mutex, thread_context.clients->callback_threads}, &connection,
[&] { return connection.m_threads.add(kj::heap<ProxyServer<Thread>>(connection, thread_context, std::thread{})); })};

// Call remote ThreadMap.makeThread function so server will create a
Expand All @@ -45,7 +45,7 @@ void CustomBuildField(TypeList<>,
return request.send().getResult(); // Nonblocking due to capnp request pipelining.
}};
auto [request_thread, _1]{SetThread(
GuardedRef{thread_context.waiter->m_mutex, thread_context.request_threads},
GuardedRef{thread_context.clients->mutex, thread_context.clients->request_threads},
&connection, make_request_thread)};

auto context = output.init();
Expand Down Expand Up @@ -94,7 +94,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
// to the same thread already in the map, so there is no
// need to update the map.
auto& thread_context = CurrentThread();
auto& request_threads = thread_context.request_threads;
auto& request_threads = thread_context.clients->request_threads;
ConnThread request_thread;
bool inserted{false};
Mutex cancel_mutex;
Expand Down Expand Up @@ -129,7 +129,7 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
// cancel_monitor.m_canceled was checked above and this
// code is running on the event loop thread.
std::tie(request_thread, inserted) = SetThread(
GuardedRef{thread_context.waiter->m_mutex, request_threads}, server.m_context.connection,
GuardedRef{thread_context.clients->mutex, request_threads}, server.m_context.connection,
[&] { return Accessor::get(call_context.getParams()).getCallbackThread(); });
// Initialize the request's results struct here on the event loop
// thread, so later getResults() calls on the execution thread
Expand Down Expand Up @@ -180,12 +180,12 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn&
// Look up the thread again without using existing
// iterator since entry may no longer be there after
// a disconnect. Destroy node after releasing
// Waiter::m_mutex, so the ProxyClient<Thread>
// ThreadClients::mutex, so the ProxyClient<Thread>
// destructor is able to use EventLoop::mutex
// without violating lock order.
ConnThreads::node_type removed;
{
Lock lock(thread_context.waiter->m_mutex);
Lock lock(thread_context.clients->mutex);
removed = request_threads.extract(server.m_context.connection);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/mp/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ static void Generate(kj::StringPtr src_prefix,
cpp_types << "#include <" << include_path << ".h> // IWYU pragma: keep\n";
cpp_types << "#include <" << include_path << ".proxy.h>\n";
cpp_types << "#include <" << include_path << ".proxy-types.h> // IWYU pragma: keep\n";
cpp_types << "#include <kj/common.h>\n";
cpp_types << "#include <" << PROXY_TYPES << ">\n\n";
cpp_types << "namespace mp {\n";

Expand Down
Loading
Loading