From c92e90ce0894ed64663172b65ab640d4e50490c5 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 28 Feb 2024 13:34:10 -0500 Subject: [PATCH 1/3] proxy-types Drop JoinPromises function Also add comments to explain code using it the PassField proxy.capnp Context overload --- include/mp/proxy-types.h | 44 ++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index f509b22b..b1ee804d 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -98,13 +98,6 @@ void CustomBuildField(TypeList<>, context.setCallbackThread(callback_thread->second.m_client); } -// Invoke promise1, then promise2, and return result of promise2. -template -kj::Promise JoinPromises(kj::Promise&& prom1, kj::Promise&& prom2) -{ - return prom1.then([prom2 = kj::mv(prom2)]() mutable { return kj::mv(prom2); }); -} - //! PassField override for mp.Context arguments. Return asynchronously and call //! function on other thread found in context. template @@ -181,27 +174,24 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& // be a local Thread::Server object, but it needs to be looked up // asynchronously with getLocalServer(). auto thread_client = context_arg.getThread(); - return JoinPromises(server.m_context.connection->m_threads.getLocalServer(thread_client) - .then([&server, invoke, req](const kj::Maybe& perhaps) { - // Assuming the thread object is found, pass it a pointer to the - // `invoke` lambda above which will invoke the function on that - // thread. - KJ_IF_MAYBE(thread_server, perhaps) - { - const auto& thread = static_cast&>(*thread_server); - server.m_context.connection->m_loop.log() << "IPC server post request #" << req << " {" - << thread.m_thread_context.thread_name << "}"; - thread.m_thread_context.waiter->post(std::move(invoke)); - } - else - { - server.m_context.connection->m_loop.log() << "IPC server error request #" << req - << ", missing thread to execute request"; - throw std::runtime_error("invalid thread handle"); - } - }), + return server.m_context.connection->m_threads.getLocalServer(thread_client) + .then([&server, invoke, req](const kj::Maybe& perhaps) { + // Assuming the thread object is found, pass it a pointer to the + // `invoke` lambda above which will invoke the function on that + // thread. + KJ_IF_MAYBE (thread_server, perhaps) { + const auto& thread = static_cast&>(*thread_server); + server.m_context.connection->m_loop.log() + << "IPC server post request #" << req << " {" << thread.m_thread_context.thread_name << "}"; + thread.m_thread_context.waiter->post(std::move(invoke)); + } else { + server.m_context.connection->m_loop.log() + << "IPC server error request #" << req << ", missing thread to execute request"; + throw std::runtime_error("invalid thread handle"); + } + }) // Wait for the invocation to finish before returning to the caller. - kj::mv(future.promise)); + .then([invoke_wait = kj::mv(future.promise)]() mutable { return kj::mv(invoke_wait); }); } // Destination parameter type that can be passed to ReadField function as an From 1f76880dbfca780a7ebdeaee9be4e532291c8245 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 28 Feb 2024 13:34:19 -0500 Subject: [PATCH 2/3] util: Get rid of unused Discard struct --- include/mp/util.h | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/include/mp/util.h b/include/mp/util.h index 6fc92778..06544485 100644 --- a/include/mp/util.h +++ b/include/mp/util.h @@ -114,17 +114,6 @@ struct Priority<0> { }; -//! Function parameter type for discarded argument. Useful to be able to write a -//! function overload that discards arguments as a normal function instead of a -//! template function. -struct Discard -{ - template - Discard(Args&&...) - { - } -}; - //! Return capnp type name with filename prefix removed. template const char* TypeName() From 6cbe56ef3b38129314960eadc0824393fec089d9 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Fri, 17 Nov 2023 13:23:52 -0500 Subject: [PATCH 3/3] refactor, moveonly: order lambda move captures first Followup for https://github.com/chaincodelabs/libmultiprocess/pull/88 --- include/mp/proxy-io.h | 2 +- include/mp/proxy-types.h | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/mp/proxy-io.h b/include/mp/proxy-io.h index b0deeb4b..a297e8c1 100644 --- a/include/mp/proxy-io.h +++ b/include/mp/proxy-io.h @@ -312,7 +312,7 @@ class Connection // to the EventLoop TaskSet to avoid "Promise callback destroyed itself" // error in cases where f deletes this Connection object. m_on_disconnect.add(m_network.onDisconnect().then( - [this, f = std::move(f)]() mutable { m_loop.m_task_set->add(kj::evalLater(kj::mv(f))); })); + [f = std::move(f), this]() mutable { m_loop.m_task_set->add(kj::evalLater(kj::mv(f))); })); } EventLoop& m_loop; diff --git a/include/mp/proxy-types.h b/include/mp/proxy-types.h index b1ee804d..dfcea432 100644 --- a/include/mp/proxy-types.h +++ b/include/mp/proxy-types.h @@ -112,9 +112,8 @@ auto PassField(Priority<1>, TypeList<>, ServerContext& server_context, const Fn& auto& server = server_context.proxy_server; int req = server_context.req; auto invoke = MakeAsyncCallable( - [&server, req, fn, args..., - fulfiller = kj::mv(future.fulfiller), - call_context = kj::mv(server_context.call_context)]() mutable { + [fulfiller = kj::mv(future.fulfiller), + call_context = kj::mv(server_context.call_context), &server, req, fn, args...]() mutable { const auto& params = call_context.getParams(); Context::Reader context_arg = Accessor::get(params); ServerContext server_context{server, call_context, req};