![]() |
Bitcoin Core 30.99.0
P2P Digital Currency
|
Event loop implementation. More...
#include <proxy-io.h>
Public Member Functions | |
| EventLoop (const char *exe_name, LogFn log_fn, void *context=nullptr) | |
| Construct event loop object with default logging options. More... | |
| EventLoop (const char *exe_name, LogOptions log_opts, void *context=nullptr) | |
| Construct event loop object with specified logging options. More... | |
| 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. More... | |
| ~EventLoop () | |
| void | loop () |
| Run event loop. More... | |
| void | post (kj::Function< void()> fn) |
| Run function on event loop thread. More... | |
| template<typename Callable > | |
| void | sync (Callable &&callable) |
| Wrapper around EventLoop::post that takes advantage of the fact that callable will not go out of scope to avoid requirement that it be copyable. More... | |
| void | addAsyncCleanup (std::function< void()> fn) |
| Register cleanup function to run on asynchronous worker thread without blocking the event loop thread. More... | |
| void | startAsyncThread () MP_REQUIRES(m_mutex) |
| Start asynchronous worker thread if necessary. More... | |
| bool | done () const MP_REQUIRES(m_mutex) |
| Check if loop should exit. More... | |
| kj::Function< void()> *m_post_fn | MP_GUARDED_BY (m_mutex) |
| Callback function to run on event loop thread during post() or sync() call. More... | |
| std::optional< CleanupList > m_async_fns | MP_GUARDED_BY (m_mutex) |
| Callback functions to run on async thread. More... | |
| int m_num_clients | MP_GUARDED_BY (m_mutex)=0 |
| Number of clients holding references to ProxyServerBase objects that reference this event loop. More... | |
Public Attributes | |
| const char * | m_exe_name |
| Process name included in thread names so combined debug output from multiple processes is easier to understand. More... | |
| std::thread::id | m_thread_id = std::this_thread::get_id() |
| ID of the event loop thread. More... | |
| std::thread | m_async_thread |
| Handle of an async worker thread. More... | |
| int | m_wait_fd = -1 |
| Pipe read handle used to wake up the event loop thread. More... | |
| int | m_post_fd = -1 |
| Pipe write handle used to wake up the event loop thread. More... | |
| Mutex | m_mutex |
| Mutex and condition variable used to post tasks to event loop and async thread. More... | |
| std::condition_variable | m_cv |
| kj::AsyncIoContext | m_io_context |
| Capnp IO context. More... | |
| LoggingErrorHandler | m_error_handler {*this} |
| Capnp error handler. Needs to outlive m_task_set. More... | |
| std::unique_ptr< kj::TaskSet > | m_task_set |
| Capnp list of pending promises. More... | |
| std::list< Connection > | m_incoming_connections |
| List of connections. More... | |
| LogOptions | m_log_opts |
| Logging options. More... | |
| void * | m_context |
| External context pointer. More... | |
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 EventLoop::post() and EventLoop::sync() methods.
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 213 of file proxy-io.h.
|
inline |
Construct event loop object with default logging options.
Definition at line 217 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 224 of file proxy-io.h.
| void mp::EventLoop::addAsyncCleanup | ( | std::function< void()> | fn | ) |
| bool mp::EventLoop::done | ( | ) | const |
| void mp::EventLoop::loop | ( | ) |
| kj::Function< void()> *m_post_fn mp::EventLoop::MP_GUARDED_BY | ( | m_mutex | ) |
| std::optional< CleanupList > m_async_fns mp::EventLoop::MP_GUARDED_BY | ( | m_mutex | ) |
Callback functions to run on async thread.
|
pure virtual |
Number of clients holding references to ProxyServerBase objects that reference this event loop.
| void mp::EventLoop::post | ( | kj::Function< void()> | fn | ) |
| 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 285 of file proxy.cpp.
|
inline |
Wrapper around EventLoop::post that takes advantage of the fact that callable will not go out of scope to avoid requirement that it be copyable.
Definition at line 244 of file proxy-io.h.
| 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 278 of file proxy-io.h.
| void* mp::EventLoop::m_context |
External context pointer.
Definition at line 317 of file proxy-io.h.
| std::condition_variable mp::EventLoop::m_cv |
Definition at line 299 of file proxy-io.h.
| LoggingErrorHandler mp::EventLoop::m_error_handler {*this} |
Capnp error handler. Needs to outlive m_task_set.
Definition at line 305 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 271 of file proxy-io.h.
| std::list<Connection> mp::EventLoop::m_incoming_connections |
List of connections.
Definition at line 311 of file proxy-io.h.
| kj::AsyncIoContext mp::EventLoop::m_io_context |
Capnp IO context.
Definition at line 302 of file proxy-io.h.
| LogOptions mp::EventLoop::m_log_opts |
Logging options.
Definition at line 314 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 298 of file proxy-io.h.
| int mp::EventLoop::m_post_fd = -1 |
Pipe write handle used to wake up the event loop thread.
Definition at line 290 of file proxy-io.h.
| std::unique_ptr<kj::TaskSet> mp::EventLoop::m_task_set |
Capnp list of pending promises.
Definition at line 308 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 274 of file proxy-io.h.
| int mp::EventLoop::m_wait_fd = -1 |
Pipe read handle used to wake up the event loop thread.
Definition at line 287 of file proxy-io.h.