Bitcoin Core 29.99.0
P2P Digital Currency
Public Member Functions | Public Attributes | List of all members
mp::EventLoop Class Reference

Event loop implementation. More...

#include <proxy-io.h>

Collaboration diagram for mp::EventLoop:
[legend]

Public Member Functions

 EventLoop (const char *exe_name, LogFn log_fn, void *context=nullptr)
 Construct event loop object. More...
 
 ~EventLoop ()
 
void loop ()
 Run event loop. More...
 
void post (const std::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 startAsyncThread (std::unique_lock< std::mutex > &lock)
 Start asynchronous worker thread if necessary. More...
 
void addClient (std::unique_lock< std::mutex > &lock)
 Add/remove remote client reference counts. More...
 
bool removeClient (std::unique_lock< std::mutex > &lock)
 
bool done (std::unique_lock< std::mutex > &lock)
 Check if loop should exit. More...
 
Logger log ()
 
Logger logPlain ()
 
Logger raise ()
 

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...
 
const std::function< void()> * m_post_fn = nullptr
 Callback function to run on event loop thread during post() or sync() call. More...
 
CleanupList m_async_fns
 Callback functions to run on async 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...
 
int m_num_clients = 0
 Number of clients holding references to ProxyServerBase objects that reference this event loop. More...
 
std::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< Connectionm_incoming_connections
 List of connections. More...
 
LogFn m_log_fn
 External logging callback. More...
 
void * m_context
 External context pointer. More...
 

Detailed Description

Event loop implementation.

Based on https://groups.google.com/d/msg/capnproto/TuQFF1eH2-M/g81sHaTAAQAJ

Definition at line 133 of file proxy-io.h.

Constructor & Destructor Documentation

◆ EventLoop()

mp::EventLoop::EventLoop ( const char *  exe_name,
LogFn  log_fn,
void *  context = nullptr 
)

Construct event loop object.

Definition at line 157 of file proxy.cpp.

◆ ~EventLoop()

mp::EventLoop::~EventLoop ( )

Definition at line 170 of file proxy.cpp.

Member Function Documentation

◆ addClient()

void mp::EventLoop::addClient ( std::unique_lock< std::mutex > &  lock)

Add/remove remote client reference counts.

Definition at line 240 of file proxy.cpp.

Here is the caller graph for this function:

◆ done()

bool mp::EventLoop::done ( std::unique_lock< std::mutex > &  lock)

Check if loop should exit.

Definition at line 280 of file proxy.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log()

Logger mp::EventLoop::log ( )
inline

Definition at line 177 of file proxy-io.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ logPlain()

Logger mp::EventLoop::logPlain ( )
inline

Definition at line 183 of file proxy-io.h.

◆ loop()

void mp::EventLoop::loop ( )

Run event loop.

Does not return until shutdown. This should only be called once from the m_thread_id thread. This will block until the m_num_clients reference count is 0.

Definition at line 185 of file proxy.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ post()

void mp::EventLoop::post ( const std::function< void()> &  fn)

Run function on event loop thread.

Does not return until function completes. Must be called while the loop() function is active.

Definition at line 221 of file proxy.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ raise()

Logger mp::EventLoop::raise ( )
inline

Definition at line 184 of file proxy-io.h.

◆ removeClient()

bool mp::EventLoop::removeClient ( std::unique_lock< std::mutex > &  lock)

Definition at line 242 of file proxy.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ startAsyncThread()

void mp::EventLoop::startAsyncThread ( std::unique_lock< std::mutex > &  lock)

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 256 of file proxy.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sync()

template<typename Callable >
void mp::EventLoop::sync ( Callable &&  callable)
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 153 of file proxy-io.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_async_fns

CleanupList mp::EventLoop::m_async_fns

Callback functions to run on async thread.

Definition at line 201 of file proxy-io.h.

◆ m_async_thread

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 195 of file proxy-io.h.

◆ m_context

void* mp::EventLoop::m_context

External context pointer.

Definition at line 234 of file proxy-io.h.

◆ m_cv

std::condition_variable mp::EventLoop::m_cv

Definition at line 216 of file proxy-io.h.

◆ m_error_handler

LoggingErrorHandler mp::EventLoop::m_error_handler {*this}

Capnp error handler. Needs to outlive m_task_set.

Definition at line 222 of file proxy-io.h.

◆ m_exe_name

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 188 of file proxy-io.h.

◆ m_incoming_connections

std::list<Connection> mp::EventLoop::m_incoming_connections

List of connections.

Definition at line 228 of file proxy-io.h.

◆ m_io_context

kj::AsyncIoContext mp::EventLoop::m_io_context

Capnp IO context.

Definition at line 219 of file proxy-io.h.

◆ m_log_fn

LogFn mp::EventLoop::m_log_fn

External logging callback.

Definition at line 231 of file proxy-io.h.

◆ m_mutex

std::mutex mp::EventLoop::m_mutex

Mutex and condition variable used to post tasks to event loop and async thread.

Definition at line 215 of file proxy-io.h.

◆ m_num_clients

int mp::EventLoop::m_num_clients = 0

Number of clients holding references to ProxyServerBase objects that reference this event loop.

Definition at line 211 of file proxy-io.h.

◆ m_post_fd

int mp::EventLoop::m_post_fd = -1

Pipe write handle used to wake up the event loop thread.

Definition at line 207 of file proxy-io.h.

◆ m_post_fn

const std::function<void()>* mp::EventLoop::m_post_fn = nullptr

Callback function to run on event loop thread during post() or sync() call.

Definition at line 198 of file proxy-io.h.

◆ m_task_set

std::unique_ptr<kj::TaskSet> mp::EventLoop::m_task_set

Capnp list of pending promises.

Definition at line 225 of file proxy-io.h.

◆ m_thread_id

std::thread::id mp::EventLoop::m_thread_id = std::this_thread::get_id()

ID of the event loop thread.

Definition at line 191 of file proxy-io.h.

◆ m_wait_fd

int mp::EventLoop::m_wait_fd = -1

Pipe read handle used to wake up the event loop thread.

Definition at line 204 of file proxy-io.h.


The documentation for this class was generated from the following files: