libamxp
1.4.0
Patterns C Implementation
|
Timers can be used to do a job at regular intervals or once some time later. More...
Data Structures | |
struct | _amxp_timer |
The timer type. More... | |
Typedefs | |
typedef void(* | amxp_timer_cb_t) (amxp_timer_t *timer, void *priv) |
Timer timeout callback function. More... | |
typedef enum _amxp_timer_state | amxp_timer_state_t |
The timer states. More... | |
Enumerations | |
enum | _amxp_timer_state { amxp_timer_off , amxp_timer_started , amxp_timer_running , amxp_timer_expired , amxp_timer_deleted } |
The timer states. More... | |
Functions | |
void | amxp_timers_calculate (void) |
Caclulates the remaining time of all timers. More... | |
void | amxp_timers_check (void) |
Check all timers and call the callback function when the timer is in Timer expired state. More... | |
void | amxp_timers_enable (bool enable) |
Enable or disable all timers. More... | |
int | amxp_timer_new (amxp_timer_t **timer, amxp_timer_cb_t cb, void *priv) |
Allocate and initializes a new timer. More... | |
void | amxp_timer_delete (amxp_timer_t **timer) |
Deletes a timer. More... | |
int | amxp_timer_set_interval (amxp_timer_t *timer, unsigned int msec) |
Sets the interval of a timer in milli seconds. More... | |
unsigned int | amxp_timer_remaining_time (amxp_timer_t *timer) |
Get the remaining time of the timer. More... | |
int | amxp_timer_start (amxp_timer_t *timer, unsigned int timeout_msec) |
Starts or resets a timer. More... | |
int | amxp_timer_stop (amxp_timer_t *timer) |
Stops the timer. More... | |
amxp_timer_state_t | amxp_timer_get_state (amxp_timer_t *timer) |
Get the timer's state. More... | |
Timers can be used to do a job at regular intervals or once some time later.
To have timers fully functional an event loop must be started. The timers are using the SIGALRM signal. Multiple timers can be started, but only one timer will be the next to trigger the SIGALRM.
To create a timer, use the amxp_timer_new function. Some arbitrary private data can be attached to the timer. This private data is a pointer to memory allocated by the caller and must be managed by the caller. This private data can be changed at any time by modifying the priv member of the amxp_timer_t structure. The pointer to the private data will be passed to the callback function. The callback function will be called each time the timer expires.
To start a timer, use the amxp_timer_start function.
By default a timer will only expire once. To have the timer repeat, an interval must be set before starting the timer, an interval can be set using amxp_timer_set_interval. The interval and the initial expire time can be different. While the timer is running the interval can be changed and will take effect on the next time the timer expires.
A timer can be stopped at any time by calling amxp_timer_stop, to restart the time call amxp_timer_start again.
A running timer can be reset to it's initial value by calling amxp_timer_start again.
All timers can be disabled or enabled again by callling amxp_timers_enable.
A timer that is not needed any more can be removed by calling amxp_timer_delete.
The current state of a timer can be fetched using amxp_timer_get_state.
To make the timer implementation work, an eventloop must be available that monitors SIG_ALRM system signal. When the SIG_ALRM is triggered the eventloop must call the functions
The first one will calculate the remaining time for each timer, if the remaining time is 0 or lower the time is expired, the state of the timer will be set to expired state. Timers in started state will become active when this function is called.
The amxp_timers_check will call the callback function for each expired timer and reset the state of the timer if needed. This function will remove the timers in deleted state from memory.
typedef void(* amxp_timer_cb_t) (amxp_timer_t *timer, void *priv) |
Timer timeout callback function.
Definition of the timer timeout callback function. This function is called whenever a timer expires. The private data attached to the timer when creating the timer using amxp_timer_new is passed as is to this callback function.
It is allowed to delete the timer in the callback function, it is not allowed to delete other timers in the callback function.
Definition at line 139 of file amxp_timer.h.
typedef enum _amxp_timer_state amxp_timer_state_t |
The timer states.
This enum holds all possible timer states
enum _amxp_timer_state |
The timer states.
This enum holds all possible timer states
Definition at line 148 of file amxp_timer.h.
void amxp_timer_delete | ( | amxp_timer_t ** | timer | ) |
Deletes a timer.
Removes the timer timers list and cleanup allocated memory.
timer | pointer to the timer. |
Definition at line 247 of file amxp_timer.c.
amxp_timer_state_t amxp_timer_get_state | ( | amxp_timer_t * | timer | ) |
Get the timer's state.
Returns the current state of the timer.
timer | pointer to the timer. |
Definition at line 334 of file amxp_timer.c.
int amxp_timer_new | ( | amxp_timer_t ** | timer, |
amxp_timer_cb_t | cb, | ||
void * | priv | ||
) |
Allocate and initializes a new timer.
Allocates and initializes a new timer structure. The new timer will be added to the list of timers.
The *timer must be initialized to NULL before calling this function.
timer | will receive the pointer to the new timer. |
cb | the time callback function, see amxp_timer_cb_t |
priv | pointer to private data, this pointer is passed to the callback function when the timer expires. |
Definition at line 229 of file amxp_timer.c.
unsigned int amxp_timer_remaining_time | ( | amxp_timer_t * | timer | ) |
Get the remaining time of the timer.
Returns the remaining time of the timer in milliseconds. If a timer is not running, this function will always return 0.
timer | pointer to the timer. |
Definition at line 281 of file amxp_timer.c.
int amxp_timer_set_interval | ( | amxp_timer_t * | timer, |
unsigned int | msec | ||
) |
Sets the interval of a timer in milli seconds.
Assigns an interval to the timer. When the initial timeout of the timer occurs, this interval time will be used to restart the timer.
A timer with an interval set will keep on running until amxp_timer_stop is called.
timer | pointer to the timer. |
msec | the interval specified in milliseconds |
Definition at line 259 of file amxp_timer.c.
int amxp_timer_start | ( | amxp_timer_t * | timer, |
unsigned int | timeout_msec | ||
) |
Starts or resets a timer.
The timer will be started, the initial time out value is must be provided in milliseconds.
If an interval time was set, the interval will start after the initial timeout.
If the timer was already started, the timer will be reset and restarted using the new timeout value.
timer | pointer to the timer. |
timeout_msec | initial timeout value. |
Definition at line 296 of file amxp_timer.c.
int amxp_timer_stop | ( | amxp_timer_t * | timer | ) |
Stops the timer.
This function will stop the timer. After calling this function the timeout callback function of the timer will not be called again until the timer is restarted using amxp_timer_start
timer | pointer to the timer. |
Definition at line 319 of file amxp_timer.c.
void amxp_timers_calculate | ( | void | ) |
Caclulates the remaining time of all timers.
Updates all timers with the time passed since the last update by subtracting the passed time from each timer. If a timer reaches zero or becomes negative it is expired and the state is changed to amxp_timer_expired.
The timer with the smallest remaining time is used to set the SIGALRM signal.
Definition at line 144 of file amxp_timer.c.
void amxp_timers_check | ( | void | ) |
Check all timers and call the callback function when the timer is in Timer expired state.
Loops over all timers and for each expired timer the timeout callback function is called. If the timer has an interval set, the state is reset to amxp_timer_running, if no interval is availble the timer state is reset to amxp_timer_off.
Definition at line 183 of file amxp_timer.c.
void amxp_timers_enable | ( | bool | enable | ) |
Enable or disable all timers.
With this function all timers can be disable or enabled. When the timers are disabled they can still expire, but the callback functions are not called. The next time the timers are enabled all callback function of the expired timers will be called in the next iteration in the event loop.
enable | True to enable timer handling or false to disable timer handling |
Definition at line 225 of file amxp_timer.c.