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 | Private Attributes | List of all members
DobbyTimer Class Reference

Utility object that can be used to register a callback function to execute in the future. More...

#include <DobbyTimer.h>

Collaboration diagram for DobbyTimer:
Collaboration graph
[legend]

Classes

struct  tagTimerEntry
 
class  TimerEntryCompare
 

Public Member Functions

void stop ()
 Stops the poll loop thread and cancels all timers.
 
int add (const std::chrono::milliseconds &timeout, bool oneShot, const std::function< bool()> &func)
 Adds a new timer to the timer queue. More...
 
bool remove (int timerId)
 Removes the given timer from the timer queue. More...
 

Private Types

typedef struct DobbyTimer::tagTimerEntry TimerEntry
 

Private Member Functions

void timerThread ()
 The thread function that runs the timer poll loop. More...
 
void updateTimerFd () const
 Writes the item on the head of the expiry queue into the timerfd for the next wake-up time.
 
struct timespec calcAbsTime (const struct timespec &now, const std::chrono::milliseconds &timeout) const
 Calculates the a new time value based on the time now and the supplied millisecond offset. More...
 

Private Attributes

std::multiset< TimerEntry, TimerEntryComparemTimersQueue
 
std::recursive_mutex mLock
 
std::thread mThread
 
int mTimerFd
 
int mEventFd
 
AICommon::IDGenerator< 6 > mIdGenerator
 

Detailed Description

Utility object that can be used to register a callback function to execute in the future.

Multiple callbacks can be registered via this object, internally it runs a thread with a single timerfd that wakes up at the correct time and then calls any handlers registered.

All callbacks are processed in the same thread, so obviously one timer handler can block all the others, clients should bear this in mind.

Warning
Currently if you try and call DobbyTimer::remove from inside the handler callback function it will return with an error. If you want to stop a repeating timer then return false from the handler.
This object currently only supports a maximum of 63 timers.

Member Function Documentation

◆ add()

int DobbyTimer::add ( const std::chrono::milliseconds &  timeout,
bool  oneShot,
const std::function< bool()> &  handler 
)

Adds a new timer to the timer queue.

Parameters
[in]timeoutThe time after which to call the supplied handler.
[in]oneShotIf true the timer is automatically removed after it times out the first time.
[in]handlerThe handler function to call when the timer times out.
Returns
on success returns a (greater than zero) timer id integer which identifies the timer, on failure -1 is returned.

◆ calcAbsTime()

struct timespec DobbyTimer::calcAbsTime ( const struct timespec &  base,
const std::chrono::milliseconds &  offset 
) const
private

Calculates the a new time value based on the time now and the supplied millisecond offset.

Parameters
[in]baseThe base time to calculate the new offset from
[in]offsetThe milliseconds offset
Returns
a timespec that is the base value plus the offset.

◆ remove()

bool DobbyTimer::remove ( int  timerId)

Removes the given timer from the timer queue.

Once this method returns (successfully) you are guaranteed that the timer handler will not be called, i.e. this is synchronisation point.

This method will fail if called from the context of a timer handler, if you want to cancel a repeating timer then just return false in the handler.

Parameters
[in]timerIdThe id of the timer to remove as returned by the add() method
Returns
true if the timer was found and was removed from the queue, otherwise false

◆ timerThread()

void DobbyTimer::timerThread ( )
private

The thread function that runs the timer poll loop.

This simply polls on an timerfd and eventfd. The timerfd is obviously for waking up and calling any installed timers at the right time. The eventfd is used to kill the poll loop at shutdown time.


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