comparison include/mb_timer.h @ 186:530bb7728546 include_mb_test

Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'. This is the solution that I dicussed with FourDollars, last night.
author Thinker K.F. Li <thinker@branda.to>
date Wed, 05 Nov 2008 15:24:01 +0800
parents include/mb/mb_timer.h@c7e5b8779bb5
children 586e50f82c1f
comparison
equal deleted inserted replaced
185:c7e5b8779bb5 186:530bb7728546
1 #ifndef __MB_TIMER_H_
2 #define __MB_TIMER_H_
3
4 #include <sys/time.h>
5 #include <stdint.h>
6
7 typedef uint32_t mbsec_t;
8 typedef uint32_t mbusec_t;
9 typedef struct _mb_timer mb_timer_t;
10 typedef struct _mb_tman mb_tman_t;
11 typedef struct timeval mb_timeval_t;
12
13
14 typedef void (*mb_tmo_hdlr)(const mb_timeval_t *tmo,
15 const mb_timeval_t *now,
16 void *arg);
17
18 extern mb_tman_t *mb_tman_new(void);
19 extern void mb_tman_free(mb_tman_t *tman);
20 extern mb_timer_t *mb_tman_timeout(mb_tman_t *tman,
21 const mb_timeval_t *tmo,
22 mb_tmo_hdlr hdlr, void *arg);
23 extern int mb_tman_remove(mb_tman_t *tman, mb_timer_t *timer);
24 extern int mb_tman_next_timeout(mb_tman_t *tman,
25 const mb_timeval_t *now,
26 mb_timeval_t *tmo_after);
27 extern int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now);
28
29 #define MB_TIMEVAL_SET(_tv, _sec, _usec) \
30 do { \
31 (_tv)->tv_sec = _sec; \
32 (_tv)->tv_usec = _usec; \
33 } while(0)
34 #define MB_TIMEVAL_CP(_tv1, _tv2) \
35 do { \
36 (_tv1)->tv_sec = (_tv2)->tv_sec; \
37 (_tv1)->tv_usec = (_tv2)->tv_usec; \
38 } while(0)
39 #define MB_TIMEVAL_SEC(_tv) ((_tv)->tv_sec)
40 #define MB_TIMEVAL_USEC(_tv) ((_tv)->tv_usec)
41 #define MB_TIMEVAL_LATER(a, b) \
42 ((a)->tv_sec > (b)->tv_sec || \
43 ((a)->tv_sec == (b)->tv_sec && \
44 (a)->tv_usec > (b)->tv_usec))
45 #define MB_TIMEVAL_LATER_INC(a, b) \
46 ((a)->tv_sec > (b)->tv_sec || \
47 ((a)->tv_sec == (b)->tv_sec && \
48 (a)->tv_usec >= (b)->tv_usec))
49 #define MB_TIMEVAL_EQ(a, b) \
50 ((a)->tv_sec == (b)->tv_sec && \
51 (a)->tv_usec == (b)->tv_usec)
52 #define MB_TIMEVAL_DIFF(a, b) \
53 do { \
54 (a)->tv_sec -= (b)->tv_sec; \
55 if((a)->tv_usec < (b)->tv_usec) { \
56 (a)->tv_sec--; \
57 (a)->tv_usec += 1000000; \
58 } \
59 (a)->tv_usec -= (b)->tv_usec; \
60 } while(0)
61 #define MB_TIMEVAL_ADD(a, b) \
62 do { \
63 (a)->tv_sec += (b)->tv_sec; \
64 (a)->tv_usec += (b)->tv_usec; \
65 if((a)->tv_usec >= 1000000) { \
66 (a)->tv_sec++; \
67 (a)->tv_usec -= 1000000; \
68 } \
69 } while(0)
70 #define MB_TIMEVAL_DIV(a, b) \
71 (((a)->tv_sec * 1000000.0 + (a)->tv_usec) / \
72 ((b)->tv_sec * 1000000.0 + (b)->tv_usec))
73
74
75 extern void get_now(mb_timeval_t *tmo);
76
77
78 #endif /* __MB_TIMER_H_ */