luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SingletonTimer Class Reference

#include <SingletonTimer.h>

Public Member Functions

 SingletonTimer (GMainLoop *loop)
 
 ~SingletonTimer ()
 
TimerHandlecreate (TimerCallback callback, void *userArg)
 
void fire (TimerHandle *timer, uint64_t timeInMs)
 
void ref (TimerHandle *timer)
 
void deref (TimerHandle *timer)
 

Static Public Member Functions

static uint64_t currentTime ()
 
static gboolean timerPrepare (GSource *source, gint *timeout)
 
static gboolean timerCheck (GSource *source)
 
static gboolean timerDispatch (GSource *source, GSourceFunc callback, gpointer userData)
 

Detailed Description

Timer management utility class

Allows for creation and management of timers which call a callback after a specified time. Also allows for querying of the system clock.

Despite the name, this class is not designed as a true singleton. It's designed to be instantiated once per GLib main loop object to generate timers associated with it.

Constructor & Destructor Documentation

SingletonTimer::SingletonTimer ( GMainLoop *  loop)

Constructs a new timer generator

SingletonTimer is largely implemented using GLib functionality. As such, it needs a GLib main loop structure to tie GLib's timer functions to. That's where this constructor comes in. This ties the SingletonTimer object to GLib so other timer users don't need to worry about GLib.

Parameters
loopThe GLib main loop structure to tie the timers to
SingletonTimer::~SingletonTimer ( )

Clean up resources allocated for timers

Currently unused, but declared nonetheless.

Member Function Documentation

TimerHandle * SingletonTimer::create ( TimerCallback  callback,
void *  userArg 
)

Sets up a new timer callback

Timers are set up in a two-stage process. Firstm you set up the function to call, then you set up the timing. This method sets up the callback, which can then be passed into SingletonTimer::fire() to activate that callback to run at a given time.

Parameters
callbackPointer to the function to call.
userArgPointer to data to pass to the callback function.
Returns
A TimerHandle pointer representing the callback function.
uint64_t SingletonTimer::currentTime ( )
static

Gets the number of milliseconds since some system-wide unknown time

Returns the number of milliseconds since some unspecified point in time as given by the system clock. While the "zero" point of this value is arbitrary, it's consistent between every call to it while the program is running. Because of this, it can be used as a very accurate clock for calculating time differences but not absolute time/date values.

Implemented as static since it uses the system-wide clock rather than using GLib.

void SingletonTimer::deref ( TimerHandle timer)

Indicates the a timer callback that a previous pointer to it has been destroyed

Primitive form of reference counting. Keeps track of how many things are using a TimerHandle and automatically deletes it when nothing needs it any longer.

See Also
SingletonTimer::ref()
RefCounted
sptr
Parameters
timerThe timer callback that has had a pointer to it destroyed.
void SingletonTimer::fire ( TimerHandle timer,
uint64_t  timeInMs 
)

Activate a timer callback to be called at after a given time period elapses

This method is what actually sets up the timer to run. It takes a callback set up by SingletonTimer::create() and runs it after the given number of milliseconds have passed.

Parameters
timerA TimerHandle pointer from SingletonTimer::create() containing information about the code to run.
timeInMsHow many milliseconds to wait before calling the function from the "timer" parameter.
void SingletonTimer::ref ( TimerHandle timer)

Indicates to a timer callback that there is a new pointer to it

TimerHandle objects are reference counted like in the sptr system. SingletonTimer::ref() and SingletonTimer::deref() are the correct way to indicate how many pointers there are to a timer calkback so they an be automatically cleaned up from memory when everything is done using them.

See Also
sptr
Todo:
Change this to have it use actual smart pointers instead of duplicating the functionality here.
Parameters
timerThe timer callback that has a new pointer to it.
gboolean SingletonTimer::timerCheck ( GSource *  source)
static

Event source function for GLib timers

DO NOT USE. This function should only be called by the GLib functionality within this class.

Todo:

Clean this up - having non-callable public methods is a messy way to do things.

Figure out how/why this works - GLib documentation on the subject seems to be pretty sparse.

gboolean SingletonTimer::timerDispatch ( GSource *  source,
GSourceFunc  callback,
gpointer  userData 
)
static

Event source function for GLib timers

DO NOT USE. This function should only be called by the GLib functionality within this class.

Todo:

Clean this up - having non-callable public methods is a messy way to do things.

Figure out how/why this works - GLib documentation on the subject seems to be pretty sparse.

gboolean SingletonTimer::timerPrepare ( GSource *  source,
gint *  timeout 
)
static

Event source function for GLib timers

DO NOT USE. This function should only be called by the GLib functionality within this class.

Todo:

Clean this up - having non-callable public methods is a messy way to do things.

Figure out how/why this works - GLib documentation on the subject seems to be pretty sparse.


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