Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
AI_IPC::DbusEventDispatcher Class Reference

Event dispatcher loop, runs the thread that polls on the dbus fds. More...

#include <DbusEventDispatcher.h>

Public Member Functions

void startEventDispatcher (DBusConnection *connection)
 Starts the event dispatch thread for the given dbus connection. More...
 
void stopEventDispatcher ()
 Stops the event dispatch thread and cleans up all file descriptors. More...
 
template<class Function , class... Args>
bool callInEventLoop (Function &&f, Args &&... args)
 
template<class Function >
bool callInEventLoop (Function func)
 

Private Member Functions

void eventDispatcherThreadFn ()
 Thread function that processes the events. More...
 
void cleanupAllEvents ()
 Closes the three eventfd's used to wake up and trigger events in the poll loop. More...
 
bool callInEventLoopImpl (const std::function< void()> &func)
 Calls the supplied function in the context of the dispatch thread. More...
 

Static Private Member Functions

static void dispatchStatusCb (DBusConnection *connection, DBusDispatchStatus status, void *userData)
 libdbus callback when dispatch status changes. More...
 
static void wakeUpCb (void *userData)
 libdbus callback request to wake-up the event loop. More...
 

Private Attributes

DBusConnection * mDbusConnection
 
std::thread mEventDispatcherThread
 
int mDeathEventFd
 
int mWakeupEventFd
 
int mDispatchEventFd
 
uint64_t mCallCounter
 
std::mutex mCallLock
 
std::condition_variable mCallCondVar
 
std::queue< std::pair< uint64_t, std::function< void()> > > mCallQueue
 

Detailed Description

Event dispatcher loop, runs the thread that polls on the dbus fds.

This class installs handlers for the four types of dbus events; watches, timeouts, wake-ups and dispatches. Watches and timeouts are a bit tricky and are handled by the DbusWatches and DbusTimeouts objects respectively, however those objects still have a corresponding fd that are added to the poll loop managed by this dispatchers.

In addition the dispatcher has an API to allow you to call arbitrary functions in the context of it's dispatcher thread. This is used by the higher level DbusConnection object to make libdbus API calls on the connection from the dispatch thread, and thereby avoid the many varied race conditions in the dbus library.

Member Function Documentation

◆ callInEventLoopImpl()

bool DbusEventDispatcher::callInEventLoopImpl ( const std::function< void()> &  func)
private

Calls the supplied function in the context of the dispatch thread.

Warning
Because the method is called from the context of the dispatch thread, avoid calling from an dbus callback with locks held otherwise you could end up deadlocked.

It is safe to call this method from inside or outside the dispatch thread; if called from within, the function is directly executed, if not in the dispatcher thread then the function is queued up and the method will block until the function has been processed within the thread.

Parameters
[in]funcThe function to run in the dispatcher thread
Returns
true if the function was executed, otherwise false.

◆ cleanupAllEvents()

void DbusEventDispatcher::cleanupAllEvents ( )
private

Closes the three eventfd's used to wake up and trigger events in the poll loop.

Upon return all eventfds will be closed.

◆ dispatchStatusCb()

void DbusEventDispatcher::dispatchStatusCb ( DBusConnection *  connection,
DBusDispatchStatus  status,
void *  userData 
)
staticprivate

libdbus callback when dispatch status changes.

This simply triggers the eventfd which wakes the poll loop and results in dbus_connection_dispatch() being called.

Parameters
[in]connectionThe dbus connection object.
[in]statusThe new status of the dbus loop.
[in]userDataPointer to this.

◆ eventDispatcherThreadFn()

void DbusEventDispatcher::eventDispatcherThreadFn ( )
private

Thread function that processes the events.

The main poll thread that processes all dbus events.

◆ startEventDispatcher()

void DbusEventDispatcher::startEventDispatcher ( DBusConnection *  connection)

Starts the event dispatch thread for the given dbus connection.

This creates the the eventfds for the wakeup, dispatch and death events. It then registers the libdbus callback functions before finally spawning the thread that runs the poll event loop.

Parameters
[in]connectionThe dbus connection to run the event loop for.

◆ stopEventDispatcher()

void DbusEventDispatcher::stopEventDispatcher ( )

Stops the event dispatch thread and cleans up all file descriptors.

Starts by removing all the libdbus callbacks and then ask the thread to terminate by triggering the death eventfd.

◆ wakeUpCb()

void DbusEventDispatcher::wakeUpCb ( void *  userData)
staticprivate

libdbus callback request to wake-up the event loop.

This simply triggers the eventfd which wakes the poll loop and results in dbus_connection_dispatch() being called.

Parameters
[in]userDataPointer to this.

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