00001 #ifndef __MB_TIMER_H_
00002 #define __MB_TIMER_H_
00003
00004 #include <sys/time.h>
00005 #include <stdint.h>
00006
00007 typedef uint32_t mbsec_t;
00008 typedef uint32_t mbusec_t;
00009 typedef struct _mb_timer mb_timer_t;
00010 typedef struct _mb_tman mb_tman_t;
00011 typedef struct timeval mb_timeval_t;
00012
00013
00014 typedef void (*mb_tmo_hdlr)(const mb_timeval_t *tmo,
00015 const mb_timeval_t *now,
00016 void *arg);
00017
00018 extern mb_tman_t *mb_tman_new(void);
00019 extern void mb_tman_free(mb_tman_t *tman);
00020 extern mb_timer_t *mb_tman_timeout(mb_tman_t *tman,
00021 const mb_timeval_t *tmo,
00022 mb_tmo_hdlr hdlr, void *arg);
00023 extern int mb_tman_remove(mb_tman_t *tman, mb_timer_t *timer);
00024 extern int mb_tman_next_timeout(mb_tman_t *tman,
00025 const mb_timeval_t *now,
00026 mb_timeval_t *tmo_after);
00027 extern int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now);
00028
00029 #define MB_TIMEVAL_SET(_tv, _sec, _usec) \
00030 do { \
00031 (_tv)->tv_sec = _sec; \
00032 (_tv)->tv_usec = _usec; \
00033 } while(0)
00034 #define MB_TIMEVAL_CP(_tv1, _tv2) \
00035 do { \
00036 (_tv1)->tv_sec = (_tv2)->tv_sec; \
00037 (_tv1)->tv_usec = (_tv2)->tv_usec; \
00038 } while(0)
00039 #define MB_TIMEVAL_SEC(_tv) ((_tv)->tv_sec)
00040 #define MB_TIMEVAL_USEC(_tv) ((_tv)->tv_usec)
00041 #define MB_TIMEVAL_LATER(a, b) \
00042 ((a)->tv_sec > (b)->tv_sec || \
00043 ((a)->tv_sec == (b)->tv_sec && \
00044 (a)->tv_usec > (b)->tv_usec))
00045 #define MB_TIMEVAL_LATER_INC(a, b) \
00046 ((a)->tv_sec > (b)->tv_sec || \
00047 ((a)->tv_sec == (b)->tv_sec && \
00048 (a)->tv_usec >= (b)->tv_usec))
00049 #define MB_TIMEVAL_EQ(a, b) \
00050 ((a)->tv_sec == (b)->tv_sec && \
00051 (a)->tv_usec == (b)->tv_usec)
00052 #define MB_TIMEVAL_DIFF(a, b) \
00053 do { \
00054 (a)->tv_sec -= (b)->tv_sec; \
00055 if((a)->tv_usec < (b)->tv_usec) { \
00056 (a)->tv_sec--; \
00057 (a)->tv_usec += 1000000; \
00058 } \
00059 (a)->tv_usec -= (b)->tv_usec; \
00060 } while(0)
00061 #define MB_TIMEVAL_ADD(a, b) \
00062 do { \
00063 (a)->tv_sec += (b)->tv_sec; \
00064 (a)->tv_usec += (b)->tv_usec; \
00065 if((a)->tv_usec >= 1000000) { \
00066 (a)->tv_sec++; \
00067 (a)->tv_usec -= 1000000; \
00068 } \
00069 } while(0)
00070 #define MB_TIMEVAL_DIV(a, b) \
00071 (((a)->tv_sec * 1000000.0 + (a)->tv_usec) / \
00072 ((b)->tv_sec * 1000000.0 + (b)->tv_usec))
00073
00074
00075 extern void get_now(mb_timeval_t *tmo);
00076
00077
00078 #endif