Mercurial > MadButterfly
annotate src/mb_timer.h @ 43:6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 09 Aug 2008 08:27:56 +0800 |
parents | 400b4b5db0dc |
children | 46b77c92d118 |
rev | line source |
---|---|
39 | 1 #ifndef __MB_TIMER_H_ |
2 #define __MB_TIMER_H_ | |
3 | |
41 | 4 #include <sys/time.h> |
5 | |
39 | 6 typedef uint32_t mbsec_t; |
7 typedef uint32_t mbusec_t; | |
8 typedef struct _mb_timer mb_timer_t; | |
9 typedef struct _mb_tman mb_tman_t; | |
41 | 10 typedef struct timeval mb_timeval_t; |
39 | 11 |
41 | 12 |
13 typedef void (*mb_tmo_hdlr)(const mb_timeval_t *tmo, | |
14 const mb_timeval_t *now, | |
39 | 15 void *arg); |
16 | |
17 extern mb_tman_t *mb_tman_new(void); | |
18 extern void mb_tman_free(mb_tman_t *tman); | |
19 extern mb_timer_t *mb_tman_timeout(mb_tman_t *tman, | |
41 | 20 const mb_timeval_t *tmo, |
39 | 21 mb_tmo_hdlr hdlr, void *arg); |
22 extern int mb_tman_remove(mb_tman_t *tman, mb_timer_t *timer); | |
23 extern int mb_tman_next_timeout(mb_tman_t *tman, | |
41 | 24 const mb_timeval_t *now, |
25 mb_timeval_t *tmo_after); | |
26 extern int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now); | |
39 | 27 |
41 | 28 #define MB_TIMEVAL_SET(_tv, _sec, _usec) \ |
29 do { \ | |
30 (_tv)->tv_sec = _sec; \ | |
31 (_tv)->tv_usec = _usec; \ | |
32 } while(0) | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
33 #define MB_TIMEVAL_CP(_tv1, _tv2) \ |
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
34 do { \ |
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
35 (_tv1)->tv_sec = (_tv2)->tv_sec; \ |
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
36 (_tv1)->tv_usec = (_tv2)->tv_usec; \ |
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
37 } while(0) |
41 | 38 #define MB_TIMEVAL_SEC(_tv) ((_tv)->tv_sec) |
39 #define MB_TIMEVAL_USEC(_tv) ((_tv)->tv_usec) | |
40 #define MB_TIMEVAL_LATER(a, b) \ | |
41 ((a)->tv_sec > (b)->tv_sec || \ | |
42 ((a)->tv_sec == (b)->tv_sec && \ | |
43 (a)->tv_usec > (b)->tv_usec)) | |
44 #define MB_TIMEVAL_DIFF(a, b) \ | |
45 do { \ | |
46 (a)->tv_sec -= (b)->tv_sec; \ | |
47 if((a)->tv_usec < (b)->tv_usec) { \ | |
48 (a)->tv_sec--; \ | |
49 (a)->tv_usec += 1000000; \ | |
50 } \ | |
51 (a)->tv_usec -= (b)->tv_usec; \ | |
52 } while(0) | |
53 #define MB_TIMEVAL_ADD(a, b) \ | |
54 do { \ | |
55 (a)->tv_sec += (b)->tv_sec; \ | |
56 (a)->tv_usec += (b)->tv_usec; \ | |
57 if((a)->tv_usec >= 1000000) { \ | |
58 (a)->tv_sec++; \ | |
59 (a)->tv_usec -= 1000000; \ | |
60 } \ | |
61 } while(0) | |
39 | 62 |
63 | |
64 #endif /* __MB_TIMER_H_ */ |