66 lines
1.9 KiB
C
66 lines
1.9 KiB
C
#ifndef UCORE_TIMERS_H_
|
|
#define UCORE_TIMERS_H_
|
|
|
|
#include <stddef.h>
|
|
#include <sys/queue.h>
|
|
#include <sys/time.h>
|
|
#include "ucore_rbtree.h"
|
|
|
|
|
|
struct UCTimer;
|
|
struct UCTimers;
|
|
|
|
//timer states
|
|
enum {
|
|
//not running
|
|
UC_TIMER_INACTIVE = 0,
|
|
//running
|
|
UC_TIMER_ACTIVE = 1,
|
|
//running and pending to be fired in this run of uc_timers_run
|
|
UC_TIMER_PENDING = 2
|
|
};
|
|
|
|
typedef void (*uc_timer_cb)(struct UCTimers *timers, struct UCTimer *timer);
|
|
struct UCTimer {
|
|
//Internal members
|
|
RBNode rb_node; //the RBNode will hold a data pointer to the
|
|
//the UCTimer it's a member of. We might get rid of that member, and use CONTAINER_OF
|
|
//to find the UCTimer from a RBNode
|
|
//
|
|
//absolute time when the timer should be fired
|
|
struct timeval timeout;
|
|
size_t seq; //needed to keep timeout unique
|
|
//as the rbtree can only handle unique values
|
|
unsigned char state;
|
|
TAILQ_ENTRY(UCTimer) ready_entry; //entry in ready_list in UCTimers
|
|
|
|
//External members
|
|
|
|
//callback function
|
|
uc_timer_cb callback;
|
|
|
|
//user data for the callback
|
|
void *cookie_ptr;
|
|
int cookie_int;
|
|
int cookie_int2;
|
|
};
|
|
|
|
|
|
struct UCTimers {
|
|
RBTree timers;
|
|
size_t seq; //used to generate unique timers
|
|
TAILQ_HEAD(, UCTimer) ready_list; //pending timers to be fired while inside uc_timers_run
|
|
};
|
|
|
|
|
|
void uc_timer_remove(struct UCTimers *timers, struct UCTimer *timer);
|
|
void uc_timers_init(struct UCTimers *t);
|
|
void uc_timer_add(struct UCTimers *timers, struct UCTimer *timer, int sec, int usec);
|
|
void uc_timer_remove(struct UCTimers *timers, struct UCTimer *timer);
|
|
int uc_timers_first(const struct UCTimers *timers, struct timeval *first);
|
|
size_t uc_timer_count(const struct UCTimers *timers);
|
|
int uc_timer_running(const struct UCTimer *timer);
|
|
int uc_timers_run(struct UCTimers *timers, const struct timeval *now);
|
|
|
|
#endif
|