annotate src/timer.c @ 191:18f8c3126cdb

Refine installation instructions and document event dispatching by wycc.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 18 Nov 2008 13:55:09 +0800
parents 530bb7728546
children 586e50f82c1f
rev   line source
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <stdint.h>
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <stdlib.h>
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 #define OK 0
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 #define ERR -1
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 struct _mb_timer {
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
13 mb_timeval_t tmo;
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 mb_tmo_hdlr hdlr;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 void *arg;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 mb_timer_t *next;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 };
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 struct _mb_tman {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 STAILQ(mb_timer_t) timers;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 elmpool_t *timer_pool;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 };
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 mb_tman_t *mb_tman_new(void) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 mb_tman_t *tman;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 tman = (mb_tman_t *)malloc(sizeof(mb_tman_t));
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 if(tman == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 tman->timer_pool = elmpool_new(sizeof(mb_timer_t), 32);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 if(tman->timer_pool == NULL) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 free(tman);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 STAILQ_INIT(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 return tman;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 void mb_tman_free(mb_tman_t *tman) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 elmpool_free(tman->timer_pool);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 free(tman);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 mb_timer_t *mb_tman_timeout(mb_tman_t *tman,
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
48 const mb_timeval_t *tmo,
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 mb_tmo_hdlr hdlr, void *arg) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 mb_timer_t *timer, *visit, *last;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 timer = elmpool_elm_alloc(tman->timer_pool);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 if(timer == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 timer->hdlr = hdlr;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 timer->arg = arg;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 last = NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 for(visit = STAILQ_HEAD(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 visit != NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 break;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67 last = visit;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 if(last == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 STAILQ_INS(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 else if(visit == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 STAILQ_INS_TAIL(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 else
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 STAILQ_INS_AFTER(mb_timer_t, next, timer, last);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 return timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 int mb_tman_remove(mb_tman_t *tman, mb_timer_t *timer) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 elmpool_elm_free(tman->timer_pool, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86
50
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
87 /*! \brief Get how long to next timeout from this monent.
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
88 *
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
89 * \return 0 for having next timeout, -1 for not more timeout.
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
90 */
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 int mb_tman_next_timeout(mb_tman_t *tman,
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
92 const mb_timeval_t *now, mb_timeval_t *tmo_after) {
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 mb_timer_t *timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 timer = STAILQ_HEAD(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96 if(timer == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 return ERR;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
99 if(!MB_TIMEVAL_LATER(&timer->tmo, now)) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
100 memset(tmo_after, 0, sizeof(mb_timeval_t));
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
102 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
105 MB_TIMEVAL_DIFF(tmo_after, now);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
107 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
108 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
110 int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now) {
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 mb_timer_t *timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
113 while((timer = STAILQ_HEAD(tman->timers)) != NULL){
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
114 if(MB_TIMEVAL_LATER(&timer->tmo, now))
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 break;
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
116 timer->hdlr(&timer->tmo, now, timer->arg);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 elmpool_elm_free(tman->timer_pool, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122 }