Timer: new implementation (needs merge)

This commit is contained in:
o9000 2017-12-28 21:54:14 +01:00
parent c41d75e54e
commit 0911dcaed1
2 changed files with 434 additions and 586 deletions

File diff suppressed because it is too large Load diff

View file

@ -21,45 +21,46 @@
#include <glib.h>
#include <time.h>
#include <sys/time.h>
#include "bool.h"
// Single shot timers (i.e. timers with interval_msec == 0) are deleted automatically as soon as they expire,
// i.e. you do not need to stop them, however it is safe to call stop_timeout for these timers.
// You can pass the address of the variable storing the pointer to the timer as 'self' in add_timeout, in which
// case it is used to clear the pointer if the timer is destroyed automatically. This enforces the timeout pointers
// to be either valid or NULL.
// Periodic timeouts are aligned to each other whenever possible, i.e. one interval_msec is an
// integral multiple of the other.
typedef void TimerCallback(void *arg);
typedef struct _timeout timeout;
typedef struct {
char name_[64];
bool enabled_;
double expiration_time_;
int period_ms_;
TimerCallback *callback_;
void *arg_;
bool handled_;
} Timer;
// Initializes default global data.
void default_timeout();
// Initialize the timer module.
void default_timers();
// Cleans up: stops all timers and frees memory.
void cleanup_timeout();
// Destroy the timer module.
void cleanup_timers();
// Installs a timer with the first timeout after 'value_msec' and then an optional periodic timeout every
// 'interval_msec' (set it to 0 to prevent periodic timeouts).
// '_callback' is the function called when the timer reaches the timeout.
// 'arg' is the argument passed to the callback function.
// 'self' is an optional pointer to a timeout* variable. If non-NULL, the variable is set to NULL when the timer
// is destroyed (with stop_timeout, cleanup_timeout or when the timer expires and it is single-shot).
// Returns a pointer to the timer, which is needed for stopping/changing it.
timeout *add_timeout(int value_msec, int interval_msec, void (*_callback)(void *), void *arg, timeout **self);
// Initialize a timer. Caller keeps ownership.
void init_timer(Timer *timer, const char *name);
// Changes timer 't'. If it does not exist, a new timer is created, with self set to 't'.
void change_timeout(timeout **t, int value_msec, int interval_msec, void (*_callback)(void *), void *arg);
// Destroy a timer. Does not free() the pointer.
void destroy_timer(Timer *timer);
// Stops the timer 't'
void stop_timeout(timeout *t);
// Modify a timer.
void change_timer(Timer *timer, bool enabled, int delay_ms, int period_ms, TimerCallback *callback, void *arg);
// Get the time when the next installed timer will expire, or NULL if there is no timer.
// Do not free the pointer; but it is safe to change its contents.
struct timeval *get_next_timeout();
void stop_timer(Timer *timer);
// Get the time duration to the next expiration time, or NULL if there is no active timer.
// Do not free the pointer; it is harmless to change its contents.
struct timeval *get_duration_to_next_timer_expiration();
// Callback of all expired timeouts
void handle_expired_timers();
// Time helper functions.
// Returns -1 if t1 < t2, 0 if t1 == t2, 1 if t1 > t2
gint compare_timespecs(const struct timespec *t1, const struct timespec *t2);
@ -69,6 +70,7 @@ struct timespec add_msec_to_timespec(struct timespec ts, int msec);
// At the first call returns zero.
double profiling_get_time();
// Get current time in seconds, from an unspecified origin.
double get_time();
#endif // TIMER_H