Dobby
3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
|
Utility object that can be used to register a callback function to execute in the future. More...
#include <DobbyTimer.h>
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, TimerEntryCompare > | mTimersQueue |
std::recursive_mutex | mLock |
std::thread | mThread |
int | mTimerFd |
int | mEventFd |
AICommon::IDGenerator< 6 > | mIdGenerator |
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.
int DobbyTimer::add | ( | const std::chrono::milliseconds & | timeout, |
bool | oneShot, | ||
const std::function< bool()> & | handler | ||
) |
Adds a new timer to the timer queue.
[in] | timeout | The time after which to call the supplied handler. |
[in] | oneShot | If true the timer is automatically removed after it times out the first time. |
[in] | handler | The handler function to call when the timer times out. |
|
private |
Calculates the a new time value based on the time now and the supplied millisecond offset.
[in] | base | The base time to calculate the new offset from |
[in] | offset | The milliseconds offset |
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.
[in] | timerId | The id of the timer to remove as returned by the add() method |
|
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.