Mercurial > MadButterfly
comparison src/timer.c @ 41:400b4b5db0dc
Working on animation
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 08 Aug 2008 21:34:53 +0800 |
parents | db2aa914e14b |
children | 6270230b9248 |
comparison
equal
deleted
inserted
replaced
40:e292beec12d4 | 41:400b4b5db0dc |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdint.h> | 2 #include <stdint.h> |
3 #include <stdlib.h> | 3 #include <stdlib.h> |
4 #include <string.h> | |
4 #include "mb_timer.h" | 5 #include "mb_timer.h" |
5 #include "tools.h" | 6 #include "tools.h" |
6 | 7 |
7 | 8 |
8 #define OK 0 | 9 #define OK 0 |
9 #define ERR -1 | 10 #define ERR -1 |
10 | 11 |
11 struct _mb_timer { | 12 struct _mb_timer { |
12 mbsec_t sec; | 13 mb_timeval_t tmo; |
13 mbusec_t usec; | |
14 mb_tmo_hdlr hdlr; | 14 mb_tmo_hdlr hdlr; |
15 void *arg; | 15 void *arg; |
16 mb_timer_t *next; | 16 mb_timer_t *next; |
17 }; | 17 }; |
18 | 18 |
43 elmpool_free(tman->timer_pool); | 43 elmpool_free(tman->timer_pool); |
44 free(tman); | 44 free(tman); |
45 } | 45 } |
46 | 46 |
47 mb_timer_t *mb_tman_timeout(mb_tman_t *tman, | 47 mb_timer_t *mb_tman_timeout(mb_tman_t *tman, |
48 mbsec_t sec, mbusec_t usec, | 48 const mb_timeval_t *tmo, |
49 mb_tmo_hdlr hdlr, void *arg) { | 49 mb_tmo_hdlr hdlr, void *arg) { |
50 mb_timer_t *timer, *visit, *last; | 50 mb_timer_t *timer, *visit, *last; |
51 | 51 |
52 timer = elmpool_elm_alloc(tman->timer_pool); | 52 timer = elmpool_elm_alloc(tman->timer_pool); |
53 if(timer == NULL) | 53 if(timer == NULL) |
54 return NULL; | 54 return NULL; |
55 | 55 |
56 timer->sec = sec; | 56 memcpy(&timer->tmo, tmo, sizeof(mb_timeval_t)); |
57 timer->usec = usec; | |
58 timer->hdlr = hdlr; | 57 timer->hdlr = hdlr; |
59 timer->arg = arg; | 58 timer->arg = arg; |
60 | 59 |
61 last = NULL; | 60 last = NULL; |
62 for(visit = STAILQ_HEAD(tman->timers); | 61 for(visit = STAILQ_HEAD(tman->timers); |
63 visit != NULL; | 62 visit != NULL; |
64 visit = STAILQ_NEXT(mb_timer_t, next, visit)) { | 63 visit = STAILQ_NEXT(mb_timer_t, next, visit)) { |
65 if(sec < visit->sec) | 64 if(MB_TIMEVAL_LATER(&visit->tmo, tmo)) |
66 break; | |
67 if(sec == visit->sec && usec < visit->usec) | |
68 break; | 65 break; |
69 last = visit; | 66 last = visit; |
70 } | 67 } |
71 | 68 |
72 if(last == NULL) | 69 if(last == NULL) |
85 | 82 |
86 return OK; | 83 return OK; |
87 } | 84 } |
88 | 85 |
89 int mb_tman_next_timeout(mb_tman_t *tman, | 86 int mb_tman_next_timeout(mb_tman_t *tman, |
90 mbsec_t now_sec, mbusec_t now_usec, | 87 const mb_timeval_t *now, mb_timeval_t *tmo_after) { |
91 mbsec_t *after_sec, mbusec_t *after_usec) { | |
92 mb_timer_t *timer; | 88 mb_timer_t *timer; |
93 | 89 |
94 timer = STAILQ_HEAD(tman->timers); | 90 timer = STAILQ_HEAD(tman->timers); |
95 if(timer == NULL) | 91 if(timer == NULL) |
96 return ERR; | 92 return ERR; |
97 | 93 |
98 if(now_sec > timer->sec || | 94 if(!MB_TIMEVAL_LATER(&timer->tmo, now)) { |
99 (now_sec == timer->usec && now_usec >= timer->usec)) { | 95 memset(tmo_after, 0, sizeof(mb_timeval_t)); |
100 *after_sec = 0; | |
101 *after_usec = 0; | |
102 return OK; | 96 return OK; |
103 } | 97 } |
104 | 98 |
105 *after_sec = timer->sec - now_sec; | 99 memcpy(tmo_after, &timer->tmo, sizeof(mb_timeval_t)); |
106 if(now_usec > timer->usec) { | 100 MB_TIMEVAL_DIFF(tmo_after, now); |
107 --*after_sec; | |
108 *after_usec = 1000000 + timer->usec - now_usec; | |
109 } else | |
110 *after_usec = timer->usec - now_usec; | |
111 | 101 |
112 return OK; | 102 return OK; |
113 } | 103 } |
114 | 104 |
115 int mb_tman_handle_timeout(mb_tman_t *tman, | 105 int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now) { |
116 mbsec_t now_sec, mbusec_t now_usec) { | |
117 mb_timer_t *timer; | 106 mb_timer_t *timer; |
118 | 107 |
119 while((timer = STAILQ_HEAD(tman->timers)) != NULL){ | 108 while((timer = STAILQ_HEAD(tman->timers)) != NULL){ |
120 if(now_sec < timer->sec || | 109 if(MB_TIMEVAL_LATER(&timer->tmo, now)) |
121 (now_sec == timer->sec && now_usec < timer->usec)) | |
122 break; | 110 break; |
123 timer->hdlr(timer->sec, timer->usec, | 111 timer->hdlr(&timer->tmo, now, timer->arg); |
124 now_sec, now_usec, | |
125 timer->arg); | |
126 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer); | 112 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer); |
127 elmpool_elm_free(tman->timer_pool, timer); | 113 elmpool_elm_free(tman->timer_pool, timer); |
128 } | 114 } |
129 | 115 |
130 return OK; | 116 return OK; |