![]() |
Bitcoin Core 32.99.0
P2P Digital Currency
|
Event loop implementation. More...
#include <proxy-io.h>
Public Types | |
| using | Connections = std::list< Connection > |
| Type of m_incoming_connections list. | |
Public Member Functions | |
| EventLoop (const char *exe_name, LogFn log_fn, void *context=nullptr) | |
| Construct event loop object with default logging options. | |
| EventLoop (const char *exe_name, LogOptions log_opts, void *context=nullptr) | |
| Construct event loop object with specified logging options. | |
| EventLoop (const char *exe_name, std::function< void(bool, std::string)> old_callback, void *context=nullptr) | |
| Backwards-compatible constructor for previous (deprecated) logging callback signature. | |
| ~EventLoop () | |
| void | loop () |
| Run event loop. | |
| void | sync (kj::FunctionParam< void()> fn) |
| Run function on event loop thread. | |
| void | addAsyncCleanup (std::function< void()> fn) |
| Register cleanup function to run on asynchronous worker thread without blocking the event loop thread. | |
| void | startAsyncThread () MP_REQUIRES(m_mutex) |
| Start asynchronous worker thread if necessary. | |
| bool | done () const MP_REQUIRES(m_mutex) |
| Check if loop should exit. | |
| auto | incomingConnections () |
| View of incoming connections yielding Connection& for each entry. | |
| kj::FunctionParam< void()> *m_sync_fn | MP_GUARDED_BY (m_mutex) |
| Callback function to run on event loop thread during sync() call. | |
| std::optional< CleanupList > m_async_fns | MP_GUARDED_BY (m_mutex) |
| Callback functions to run on async thread. | |
| int m_num_refs | MP_GUARDED_BY (m_mutex)=0 |
| Number of EventLoopRef instances referencing this event loop. | |
Public Attributes | |
| const char * | m_exe_name |
| Process name included in thread names so combined debug output from multiple processes is easier to understand. | |
| std::thread::id | m_thread_id = std::this_thread::get_id() |
| ID of the event loop thread. | |
| std::thread | m_async_thread |
| Handle of an async worker thread. | |
| kj::Own< kj::AsyncIoStream > | m_wait_stream |
| Socket pair used to post and wait for wakeups to the event loop thread. | |
| kj::Own< kj::AsyncIoStream > | m_post_stream |
| kj::Own< kj::OutputStream > | m_post_writer |
| Synchronous writer used to write to m_post_stream. | |
| Mutex | m_mutex |
| Mutex and condition variable used to post tasks to event loop and async thread. | |
| std::condition_variable | m_cv |
| kj::AsyncIoContext | m_io_context |
| Capnp IO context. | |
| LoggingErrorHandler | m_error_handler {*this} |
| Capnp error handler. Needs to outlive m_task_set. | |
| std::unique_ptr< kj::TaskSet > | m_task_set |
| Capnp list of pending promises. | |
| Connections | m_incoming_connections |
| List of connections. | |
| LogOptions | m_log_opts |
| Logging options. | |
| void * | m_context |
| External context pointer. | |
| std::function< void()> | testing_hook_makethread |
| Hook called when ProxyServer<ThreadMap>::makeThread() is called. | |
| std::function< void()> | testing_hook_makethread_created |
| Hook called on the worker thread inside makeThread(), after the thread context is set up and thread_context promise is fulfilled, but before it starts waiting for requests. | |
| std::function< void()> | testing_hook_async_request_start |
| Hook called on the worker thread when it starts to execute an async request. | |
| std::function< void()> | testing_hook_async_request_done |
| Hook called on the worker thread just before returning results. | |
| std::function< void()> | testing_hook_connected |
| Hook called on the event loop thread when a client has connected. | |
| std::function< void()> | testing_hook_disconnected |
| Hook called on the event loop thread when a client has disconnected. | |
| std::function< void(std::any)> | testing_hook_misc |
| Miscellaneous testing hook. | |
Event loop implementation.
Cap'n Proto threading model is very simple: all I/O operations are asynchronous and must be performed on a single thread. This includes:
All of this code needs to access shared state, and there is no mutex that can be acquired to lock this state because Cap'n Proto assumes it will only be accessed from one thread. So all this code needs to actually run on one thread, and the EventLoop::loop() method is the entry point for this thread. ProxyClient and ProxyServer objects that use other threads and need to perform I/O operations post to this thread using the EventLoop::sync() method.
Specifically, because ProxyClient methods can be called from arbitrary threads, and ProxyServer methods can run on arbitrary threads, ProxyClient methods use the EventLoop thread to send requests, and ProxyServer methods use the thread to return results.
Based on https://groups.google.com/d/msg/capnproto/TuQFF1eH2-M/g81sHaTAAQAJ
Definition at line 247 of file proxy-io.h.
| using mp::EventLoop::Connections = std::list<Connection> |
Type of m_incoming_connections list.
Definition at line 295 of file proxy-io.h.
Construct event loop object with default logging options.
Definition at line 251 of file proxy-io.h.
| mp::EventLoop::EventLoop | ( | const char * | exe_name, |
| LogOptions | log_opts, | ||
| void * | context = nullptr |
||
| ) |
|
inline |
Backwards-compatible constructor for previous (deprecated) logging callback signature.
Definition at line 258 of file proxy-io.h.
| mp::EventLoop::~EventLoop | ( | ) |
| bool mp::EventLoop::done | ( | ) | const |
|
inline |
View of incoming connections yielding Connection& for each entry.
Definition at line 298 of file proxy-io.h.
| void mp::EventLoop::loop | ( | ) |
Callback function to run on event loop thread during sync() call.
| std::optional< CleanupList > m_async_fns mp::EventLoop::MP_GUARDED_BY | ( | m_mutex | ) |
Callback functions to run on async thread.
|
pure virtual |
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).
| void mp::EventLoop::startAsyncThread | ( | ) |
Start asynchronous worker thread if necessary.
This is only done if there are ProxyServerBase::m_impl objects that need to be destroyed 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 dedicated thread. It can also happen whenever this is a broken connection and the client is no longer around to call the destructors and the server objects need to be garbage collected. In both cases, it is important that ProxyServer::m_impl destructors do not run on the eventloop thread because they may need it to do I/O if they perform other IPC calls.
Definition at line 353 of file proxy.cpp.
| std::thread mp::EventLoop::m_async_thread |
Handle of an async worker thread.
Joined on destruction. Unset if async method has not been called.
Definition at line 309 of file proxy-io.h.
| void* mp::EventLoop::m_context |
External context pointer.
Definition at line 353 of file proxy-io.h.
| std::condition_variable mp::EventLoop::m_cv |
Definition at line 335 of file proxy-io.h.
| LoggingErrorHandler mp::EventLoop::m_error_handler {*this} |
Capnp error handler. Needs to outlive m_task_set.
Definition at line 341 of file proxy-io.h.
| const char* mp::EventLoop::m_exe_name |
Process name included in thread names so combined debug output from multiple processes is easier to understand.
Definition at line 302 of file proxy-io.h.
| Connections mp::EventLoop::m_incoming_connections |
List of connections.
Definition at line 347 of file proxy-io.h.
| kj::AsyncIoContext mp::EventLoop::m_io_context |
Capnp IO context.
Definition at line 338 of file proxy-io.h.
| LogOptions mp::EventLoop::m_log_opts |
Logging options.
Definition at line 350 of file proxy-io.h.
| Mutex mp::EventLoop::m_mutex |
Mutex and condition variable used to post tasks to event loop and async thread.
Definition at line 334 of file proxy-io.h.
| kj::Own<kj::AsyncIoStream> mp::EventLoop::m_post_stream |
Definition at line 319 of file proxy-io.h.
| kj::Own<kj::OutputStream> mp::EventLoop::m_post_writer |
Synchronous writer used to write to m_post_stream.
Definition at line 322 of file proxy-io.h.
| std::unique_ptr<kj::TaskSet> mp::EventLoop::m_task_set |
Capnp list of pending promises.
Definition at line 344 of file proxy-io.h.
| std::thread::id mp::EventLoop::m_thread_id = std::this_thread::get_id() |
ID of the event loop thread.
Definition at line 305 of file proxy-io.h.
| kj::Own<kj::AsyncIoStream> mp::EventLoop::m_wait_stream |
Socket pair used to post and wait for wakeups to the event loop thread.
Definition at line 318 of file proxy-io.h.
| std::function<void()> mp::EventLoop::testing_hook_async_request_done |
Hook called on the worker thread just before returning results.
Definition at line 369 of file proxy-io.h.
| std::function<void()> mp::EventLoop::testing_hook_async_request_start |
Hook called on the worker thread when it starts to execute an async request.
Used by tests to control timing or inject behavior at this point in execution.
Definition at line 366 of file proxy-io.h.
| std::function<void()> mp::EventLoop::testing_hook_connected |
Hook called on the event loop thread when a client has connected.
Definition at line 372 of file proxy-io.h.
| std::function<void()> mp::EventLoop::testing_hook_disconnected |
Hook called on the event loop thread when a client has disconnected.
Definition at line 375 of file proxy-io.h.
| std::function<void()> mp::EventLoop::testing_hook_makethread |
Hook called when ProxyServer<ThreadMap>::makeThread() is called.
Definition at line 356 of file proxy-io.h.
| std::function<void()> mp::EventLoop::testing_hook_makethread_created |
Hook called on the worker thread inside makeThread(), after the thread context is set up and thread_context promise is fulfilled, but before it starts waiting for requests.
Definition at line 361 of file proxy-io.h.
| std::function<void(std::any)> mp::EventLoop::testing_hook_misc |
Miscellaneous testing hook.
Called from various places with an argument identifying the call site (typically a string literal), so tests can control timing or inject behavior at specific points without requiring a dedicated hook for each one.
Definition at line 381 of file proxy-io.h.