Mercurial > MadButterfly
annotate src/timer.c @ 688:cadebc13be2f
Mark stext changed when its style being changed
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 07 Aug 2010 22:28:17 +0800 |
parents | 530bb7728546 |
children | 586e50f82c1f |
rev | line source |
---|---|
39 | 1 #include <stdio.h> |
2 #include <stdint.h> | |
3 #include <stdlib.h> | |
41 | 4 #include <string.h> |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
5 #include "mb_timer.h" |
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
6 #include "mb_tools.h" |
39 | 7 |
8 | |
9 #define OK 0 | |
10 #define ERR -1 | |
11 | |
12 struct _mb_timer { | |
41 | 13 mb_timeval_t tmo; |
39 | 14 mb_tmo_hdlr hdlr; |
15 void *arg; | |
16 mb_timer_t *next; | |
17 }; | |
18 | |
19 struct _mb_tman { | |
20 STAILQ(mb_timer_t) timers; | |
21 elmpool_t *timer_pool; | |
22 }; | |
23 | |
24 mb_tman_t *mb_tman_new(void) { | |
25 mb_tman_t *tman; | |
26 | |
27 tman = (mb_tman_t *)malloc(sizeof(mb_tman_t)); | |
28 if(tman == NULL) | |
29 return NULL; | |
30 | |
31 tman->timer_pool = elmpool_new(sizeof(mb_timer_t), 32); | |
32 if(tman->timer_pool == NULL) { | |
33 free(tman); | |
34 return NULL; | |
35 } | |
36 | |
37 STAILQ_INIT(tman->timers); | |
38 | |
39 return tman; | |
40 } | |
41 | |
42 void mb_tman_free(mb_tman_t *tman) { | |
43 elmpool_free(tman->timer_pool); | |
44 free(tman); | |
45 } | |
46 | |
47 mb_timer_t *mb_tman_timeout(mb_tman_t *tman, | |
41 | 48 const mb_timeval_t *tmo, |
39 | 49 mb_tmo_hdlr hdlr, void *arg) { |
50 mb_timer_t *timer, *visit, *last; | |
51 | |
52 timer = elmpool_elm_alloc(tman->timer_pool); | |
53 if(timer == NULL) | |
54 return NULL; | |
55 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
56 MB_TIMEVAL_CP(&timer->tmo, tmo); |
39 | 57 timer->hdlr = hdlr; |
58 timer->arg = arg; | |
59 | |
60 last = NULL; | |
61 for(visit = STAILQ_HEAD(tman->timers); | |
62 visit != NULL; | |
63 visit = STAILQ_NEXT(mb_timer_t, next, visit)) { | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
50
diff
changeset
|
64 if(MB_TIMEVAL_LATER(&visit->tmo, tmo) || |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
50
diff
changeset
|
65 MB_TIMEVAL_EQ(&visit->tmo, tmo)) |
39 | 66 break; |
67 last = visit; | |
68 } | |
69 | |
70 if(last == NULL) | |
71 STAILQ_INS(tman->timers, mb_timer_t, next, timer); | |
72 else if(visit == NULL) | |
73 STAILQ_INS_TAIL(tman->timers, mb_timer_t, next, timer); | |
74 else | |
75 STAILQ_INS_AFTER(mb_timer_t, next, timer, last); | |
76 | |
77 return timer; | |
78 } | |
79 | |
80 int mb_tman_remove(mb_tman_t *tman, mb_timer_t *timer) { | |
81 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer); | |
82 elmpool_elm_free(tman->timer_pool, timer); | |
83 | |
84 return OK; | |
85 } | |
86 | |
50 | 87 /*! \brief Get how long to next timeout from this monent. |
88 * | |
89 * \return 0 for having next timeout, -1 for not more timeout. | |
90 */ | |
39 | 91 int mb_tman_next_timeout(mb_tman_t *tman, |
41 | 92 const mb_timeval_t *now, mb_timeval_t *tmo_after) { |
39 | 93 mb_timer_t *timer; |
94 | |
95 timer = STAILQ_HEAD(tman->timers); | |
96 if(timer == NULL) | |
97 return ERR; | |
98 | |
41 | 99 if(!MB_TIMEVAL_LATER(&timer->tmo, now)) { |
100 memset(tmo_after, 0, sizeof(mb_timeval_t)); | |
39 | 101 return OK; |
102 } | |
103 | |
43
6270230b9248
Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents:
41
diff
changeset
|
104 MB_TIMEVAL_CP(tmo_after, &timer->tmo); |
41 | 105 MB_TIMEVAL_DIFF(tmo_after, now); |
39 | 106 |
107 return OK; | |
108 } | |
109 | |
41 | 110 int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now) { |
39 | 111 mb_timer_t *timer; |
112 | |
113 while((timer = STAILQ_HEAD(tman->timers)) != NULL){ | |
41 | 114 if(MB_TIMEVAL_LATER(&timer->tmo, now)) |
39 | 115 break; |
41 | 116 timer->hdlr(&timer->tmo, now, timer->arg); |
39 | 117 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer); |
118 elmpool_elm_free(tman->timer_pool, timer); | |
119 } | |
120 | |
121 return OK; | |
122 } |