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

Utility object to handle installing / removing dbus watches from the poll loop. More...

#include <DbusWatches.h>

Collaboration diagram for AI_IPC::DbusWatches:
Collaboration graph
[legend]

Classes

struct  _WatchEntry
 

Public Member Functions

 DbusWatches (DBusConnection *conn)
 
int fd () const
 Returns the epoll fd that the dispatcher should poll on.
 
void processEvent (unsigned int pollEvents)
 Called when something has happened on the epoll event loop. More...
 

Private Types

typedef struct AI_IPC::DbusWatches::_WatchEntry WatchEntry
 

Private Member Functions

dbus_bool_t addWatch (DBusWatch *watch)
 Callback from dbus to add a new watch to poll on. More...
 
void toggleWatch (DBusWatch *watch)
 Toggles the watch flags on the given watch. More...
 
void removeWatch (DBusWatch *watch)
 Removes the watch from the epoll loop and our local array. More...
 
uint64_t createWatch (DBusWatch *watch, int duppedFd)
 Tries to find a slot to put the watch into. More...
 
void deleteWatch (uint64_t tag)
 Removes a watch from our internal array. More...
 

Static Private Member Functions

static dbus_bool_t addWatchCb (DBusWatch *watch, void *userData)
 
static void toggleWatchCb (DBusWatch *watch, void *userData)
 
static void removeWatchCb (DBusWatch *watch, void *userData)
 

Private Attributes

DBusConnection *const mDbusConnection
 
int mEpollFd
 
uint64_t mTagCounter
 
WatchEntry mWatches [mMaxWatches]
 
struct epoll_event mEpollEvents [mMaxWatches]
 
const std::thread::id mExpectedThreadId
 

Static Private Attributes

static const unsigned mMaxWatches = 128
 

Detailed Description

Utility object to handle installing / removing dbus watches from the poll loop.

Warning
This class is not thread safe, it is designed to only be called from one thread which is the same thread the libdbus callbacks will be called from. On debug builds an error will be reported if called from an invalid thread.

Internally it creates an epoll object that has watches (which are just fds) added to / removed from it. The epoll fd is returned by this object, and the dispatch poll loop will poll on this. So in effect this is a second level of poll file descriptors.

This code automatically installs the dbus callbacks on the connection at construction time, and removes the callbacks at destruction.

When watches are added we dup the file descriptors, and add the dupped fd to epoll. The reason for this is that libdbus tends to have more than one watch associated with a single fd and you can't add the same fd more once to an epoll event loop. By dup'ing the file descriptors it means we can have one epoll event entry per dbus watch.

Member Function Documentation

◆ addWatch()

dbus_bool_t DbusWatches::addWatch ( DBusWatch *  watch)
private

Callback from dbus to add a new watch to poll on.

libdus should call this function from the dispatch loop thread. We are responsible for adding the watch file descriptor to the poll loop with the given watch flags. When we're woken by one of these we then need to call the watch's dispatch function (called in processEvent()).

Parameters
[in]watchThe dbus watch object to add
Returns
TRUE on success and FALSE on failure.

◆ createWatch()

uint64_t DbusWatches::createWatch ( DBusWatch *  watch,
int  duppedFd 
)
private

Tries to find a slot to put the watch into.

We store the watches in an internal array, each watch has a unique 64-bit tag number, this tag is added to the epoll event so we can quickly find the watch that triggered the event.

The lower bits of the tag number are the index into the internal array.

Parameters
[in]watchThe dbus watch object
[in]duppedFdThe dup'ed file descriptor for the watch
Returns
on success a non-zero tag number, on failure 0.

◆ deleteWatch()

void DbusWatches::deleteWatch ( uint64_t  tag)
private

Removes a watch from our internal array.

Parameters
[in]tagThe tag number of the watch to remove

◆ processEvent()

void DbusWatches::processEvent ( unsigned int  pollEvents)

Called when something has happened on the epoll event loop.

The main disaptcher loop polls on the fd we supply, when anything changes then this method is called.

Parameters
[in]pollEventsBitmask of the poll events that woke the main loop.

◆ removeWatch()

void DbusWatches::removeWatch ( DBusWatch *  watch)
private

Removes the watch from the epoll loop and our local array.

libdus should call this function from the dispatch loop thread. We check whether the watch has been added, and if so we remove it from epoll.

Parameters
[in]watchThe dbus watch object to remove

◆ toggleWatch()

void DbusWatches::toggleWatch ( DBusWatch *  watch)
private

Toggles the watch flags on the given watch.

libdus should call this function from the dispatch loop thread. This just changes the epoll event flags to match the new dbus watch flags for the given watch.

Parameters
[in]watchThe dbus watch object to add

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