9#include <mp/proxy.capnp.h>
14#include <capnp/capability.h>
15#include <capnp/common.h>
17#include <condition_variable>
21#include <kj/async-io.h>
22#include <kj/async-prelude.h>
25#include <kj/exception.h>
26#include <kj/function.h>
35#include <sys/socket.h>
48 KJ_LOG(ERROR,
"Uncaught exception in daemonized task.", exception);
66 auto loop_lock{
PtrOrValue{m_lock, loop->m_mutex}};
67 loop_lock->assert_locked(loop->m_mutex);
68 assert(loop->m_num_refs > 0);
69 loop->m_num_refs -= 1;
71 loop->m_cv.notify_all();
72 int post_fd{loop->m_post_fd};
75 KJ_SYSCALL(write(post_fd, &buffer, 1));
79 if (relock) loop_lock->lock();
95 m_canceler.cancel(
"Interrupted by disconnect");
198 m_async_fns->emplace_back(std::move(fn));
203 : m_exe_name(exe_name),
204 m_io_context(kj::setupAsyncIo()),
205 m_task_set(new kj::TaskSet(m_error_handler)),
206 m_log_opts(
std::move(log_opts)),
210 KJ_SYSCALL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
219 KJ_ASSERT(m_post_fn ==
nullptr);
220 KJ_ASSERT(!m_async_fns);
223 KJ_ASSERT(m_num_refs == 0);
239 m_async_fns.emplace();
242 kj::Own<kj::AsyncIoStream> wait_stream{
243 m_io_context.lowLevelProvider->wrapSocketFd(
m_wait_fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP)};
247 const size_t read_bytes = wait_stream->read(&buffer, 0, 1).wait(
m_io_context.waitScope);
248 if (read_bytes != 1)
throw std::logic_error(
"EventLoop wait_stream closed unexpectedly");
255 MP_LOG(*
this,
Log::Error) <<
"EventLoop: m_post_fn threw: " << kj::str(*exception).cStr();
267 MP_LOG(*
this,
Log::Info) <<
"EventLoop::loop done, cancelling event listeners.";
270 wait_stream =
nullptr;
271 KJ_SYSCALL(::close(post_fd));
292 KJ_SYSCALL(write(post_fd, &buffer, 1));
303 }
else if (!m_async_fns->empty()) {
306 while (m_async_fns) {
307 if (!m_async_fns->empty()) {
309 const std::function<void()> fn = std::move(m_async_fns->front());
310 m_async_fns->pop_front();
326 return m_num_refs == 0 && m_async_fns->empty();
336 std::tie(
thread, inserted) = threads.ref.try_emplace(connection);
339 thread->second.emplace(make_thread(), connection,
false);
349 thread->second->m_disconnect_cb.reset();
353 threads.ref.erase(
thread);
356 return {
thread, inserted};
364 if (m_disconnect_cb) {
370 if (m_disconnect_cb) {
371 m_context.connection->removeSyncCleanup(*m_disconnect_cb);
380 assert(m_thread_context.waiter.get() !=
nullptr);
385 if (!m_thread.joinable())
return;
391 assert(m_thread_context.waiter.get());
392 std::unique_ptr<Waiter> waiter;
394 const Lock lock(m_thread_context.waiter->m_mutex);
397 waiter = std::move(m_thread_context.waiter);
404 m_thread_context.request_threads.clear();
405 m_thread_context.callback_threads.clear();
407 waiter->m_cv.notify_all();
414 context.getResults().setResult(m_thread_context.thread_name);
415 return kj::READY_NOW;
423 throw std::runtime_error(
"makePool called on connection with existing pool");
426 const uint32_t
count = context.getParams().getCount();
427 for (uint32_t i = 0; i <
count; ++i) {
428 const std::string thread_name =
"pool/" + std::to_string(i);
429 std::promise<ThreadContext*> thread_context;
437 auto thread_server = kj::heap<ProxyServer<Thread>>(
m_connection, *thread_context.get_future().get(), std::move(
thread));
440 return kj::READY_NOW;
446 if (loop.testing_hook_makethread) loop.testing_hook_makethread();
447 const std::string from = context.getParams().getName();
448 std::promise<ThreadContext*> thread_context;
454 if (loop.testing_hook_makethread_created) loop.testing_hook_makethread_created();
459 auto thread_server = kj::heap<ProxyServer<Thread>>(
m_connection, *thread_context.get_future().get(), std::move(
thread));
460 auto thread_client =
m_connection.m_threads.add(kj::mv(thread_server));
461 context.getResults().setResult(kj::mv(thread_client));
462 return kj::READY_NOW;
Object holding network & rpc state associated with either an incoming server connection,...
CleanupIt addSyncCleanup(std::function< void()> fn)
Register synchronous cleanup function to run on event loop thread (with access to capnp thread local ...
~Connection()
Run cleanup functions.
kj::Canceler m_canceler
Canceler for canceling promises that we want to discard when the connection is destroyed.
CleanupList m_sync_cleanup_fns
Cleanup functions to run if connection is broken unexpectedly.
std::optional<::capnp::RpcSystem<::capnp::rpc::twoparty::VatId > > m_rpc_system
void removeSyncCleanup(CleanupIt it)
Event loop implementation.
kj::AsyncIoContext m_io_context
Capnp IO context.
void startAsyncThread() MP_REQUIRES(m_mutex)
Start asynchronous worker thread if necessary.
std::condition_variable m_cv
EventLoop(const char *exe_name, LogFn log_fn, void *context=nullptr)
Construct event loop object with default logging options.
void addAsyncCleanup(std::function< void()> fn)
Register cleanup function to run on asynchronous worker thread without blocking the event loop thread...
void loop()
Run event loop.
Mutex m_mutex
Mutex and condition variable used to post tasks to event loop and async thread.
bool done() const MP_REQUIRES(m_mutex)
Check if loop should exit.
int m_post_fd
Pipe write handle used to wake up the event loop thread.
std::unique_ptr< kj::TaskSet > m_task_set
Capnp list of pending promises.
int m_wait_fd
Pipe read handle used to wake up the event loop thread.
std::thread m_async_thread
Handle of an async worker thread.
std::thread::id m_thread_id
ID of the event loop thread.
void post(kj::Function< void()> fn)
Run function on event loop thread.
Event loop smart pointer automatically managing m_num_refs.
EventLoopRef(EventLoop &loop, Lock *lock=nullptr)
void reset(bool relock=false)
std::unique_lock< std::mutex > m_lock
void taskFailed(kj::Exception &&exception) override
btcsignals::scoped_connection m_connection
std::optional< mp::EventLoop > m_loop
EventLoop object which manages I/O events for all connections.
std::thread thread
Thread variable should be after other struct members so the thread does not start until the other mem...
Functions to serialize / deserialize common bitcoin types.
void Unlock(Lock &lock, Callback &&callback)
kj::StringPtr KJ_STRINGIFY(Log flags)
std::list< std::function< void()> > CleanupList
std::string ThreadName(const char *exe_name)
Format current thread name as "{exe_name}-{$pid}/{thread_name}-{$tid}".
std::atomic< int > server_reqs
std::tuple< ConnThread, bool > SetThread(GuardedRef< ConnThreads > threads, Connection *connection, const std::function< Thread::Client()> &make_thread)
thread_local ThreadContext g_thread_context
Log
Log flags. Update stringify function if changed!
std::string LongThreadName(const char *exe_name)
ConnThreads::iterator ConnThread
typename CleanupList::iterator CleanupIt
Mapping from capnp interface type to proxy client implementation (specializations are generated by pr...
ProxyContext(Connection *connection)
Mapping from capnp interface type to proxy server implementation (specializations are generated by pr...
Convenient wrapper around std::variant<T*, T>
The thread_local ThreadContext g_thread_context struct provides information about individual threads ...
std::unique_ptr< Waiter > waiter
Waiter object used to allow remote clients to execute code on this thread.
bool loop_thread
Whether this thread is a capnp event loop thread.
std::string thread_name
Identifying string for debug.