annotate src/timer.c @ 903:4c7f0ec97ad9

Copy src/dfb_supp.c from src/X_supp.c
author Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
date Sun, 03 Oct 2010 16:54:26 +0800
parents 586e50f82c1f
children 7ccc094bdbe5
rev   line source
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 186
diff changeset
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 186
diff changeset
2 // vim: sw=4:ts=8:sts=4
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <stdio.h>
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <stdint.h>
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <stdlib.h>
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
6 #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
7 #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
8 #include "mb_tools.h"
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #define OK 0
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 #define ERR -1
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 struct _mb_timer {
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
15 mb_timeval_t tmo;
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 mb_tmo_hdlr hdlr;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 void *arg;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 mb_timer_t *next;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 };
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 struct _mb_tman {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 STAILQ(mb_timer_t) timers;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 elmpool_t *timer_pool;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 };
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 mb_tman_t *mb_tman_new(void) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 mb_tman_t *tman;
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 186
diff changeset
28
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 tman = (mb_tman_t *)malloc(sizeof(mb_tman_t));
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 if(tman == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 tman->timer_pool = elmpool_new(sizeof(mb_timer_t), 32);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 if(tman->timer_pool == NULL) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 free(tman);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 }
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 STAILQ_INIT(tman->timers);
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 return tman;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 void mb_tman_free(mb_tman_t *tman) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 elmpool_free(tman->timer_pool);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 free(tman);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 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
50 const mb_timeval_t *tmo,
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 mb_tmo_hdlr hdlr, void *arg) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 mb_timer_t *timer, *visit, *last;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 timer = elmpool_elm_alloc(tman->timer_pool);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 if(timer == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57
43
6270230b9248 Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
58 MB_TIMEVAL_CP(&timer->tmo, tmo);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 timer->hdlr = hdlr;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 timer->arg = arg;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 last = NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 for(visit = STAILQ_HEAD(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 visit != NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 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
66 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
67 MB_TIMEVAL_EQ(&visit->tmo, tmo))
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 break;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 last = visit;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 if(last == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 STAILQ_INS(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 else if(visit == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 STAILQ_INS_TAIL(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 else
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 STAILQ_INS_AFTER(mb_timer_t, next, timer, last);
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 return timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 }
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 186
diff changeset
81
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 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
83 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 elmpool_elm_free(tman->timer_pool, timer);
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 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88
50
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
89 /*! \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
90 *
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
91 * \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
92 */
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 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
94 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
95 mb_timer_t *timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 timer = STAILQ_HEAD(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 if(timer == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 return ERR;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
101 if(!MB_TIMEVAL_LATER(&timer->tmo, now)) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
102 memset(tmo_after, 0, sizeof(mb_timeval_t));
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
103 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105
43
6270230b9248 Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
106 MB_TIMEVAL_CP(tmo_after, &timer->tmo);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
107 MB_TIMEVAL_DIFF(tmo_after, now);
39
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 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
112 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
113 mb_timer_t *timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
114
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115 while((timer = STAILQ_HEAD(tman->timers)) != NULL){
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
116 if(MB_TIMEVAL_LATER(&timer->tmo, now))
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
117 break;
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
118 timer->hdlr(&timer->tmo, now, timer->arg);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
119 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 elmpool_elm_free(tman->timer_pool, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
122
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
123 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 }