annotate src/timer.c @ 1113:572277fcec0d

Fix waring of gcc for an typo on type name of variable
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 08 Dec 2010 19:47:25 +0800
parents 7ccc094bdbe5
children
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"
1018
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
9 #include "mb_backend.h"
39
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 #define OK 0
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 #define ERR -1
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 struct _mb_timer {
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
16 mb_timeval_t tmo;
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 mb_tmo_hdlr hdlr;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 void *arg;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 mb_timer_t *next;
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 struct _mb_tman {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 STAILQ(mb_timer_t) timers;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 elmpool_t *timer_pool;
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 mb_tman_t *mb_tman_new(void) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 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
29
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 tman = (mb_tman_t *)malloc(sizeof(mb_tman_t));
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 if(tman == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 tman->timer_pool = elmpool_new(sizeof(mb_timer_t), 32);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 if(tman->timer_pool == NULL) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 free(tman);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 return NULL;
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 STAILQ_INIT(tman->timers);
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 return tman;
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 void mb_tman_free(mb_tman_t *tman) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 elmpool_free(tman->timer_pool);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 free(tman);
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 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
51 const mb_timeval_t *tmo,
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 mb_tmo_hdlr hdlr, void *arg) {
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 mb_timer_t *timer, *visit, *last;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 timer = elmpool_elm_alloc(tman->timer_pool);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 if(timer == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 return NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58
43
6270230b9248 Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
59 MB_TIMEVAL_CP(&timer->tmo, tmo);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 timer->hdlr = hdlr;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 timer->arg = arg;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 last = NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 for(visit = STAILQ_HEAD(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 visit != NULL;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 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
67 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
68 MB_TIMEVAL_EQ(&visit->tmo, tmo))
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 break;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 last = visit;
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 if(last == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 STAILQ_INS(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 else if(visit == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 STAILQ_INS_TAIL(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 else
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 STAILQ_INS_AFTER(mb_timer_t, next, timer, last);
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 return timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 }
822
586e50f82c1f Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents: 186
diff changeset
82
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 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
84 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 elmpool_elm_free(tman->timer_pool, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89
50
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
90 /*! \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
91 *
c986e45c1e91 Unittest for animate.c
Thinker K.F. Li <thinker@branda.to>
parents: 43
diff changeset
92 * \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
93 */
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 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
95 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
96 mb_timer_t *timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 timer = STAILQ_HEAD(tman->timers);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 if(timer == NULL)
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
100 return ERR;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
101
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
102 if(!MB_TIMEVAL_LATER(&timer->tmo, now)) {
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
103 memset(tmo_after, 0, sizeof(mb_timeval_t));
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
104 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
105 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
106
43
6270230b9248 Use MB_TIMEVAL_CP() instead of memcpy
Thinker K.F. Li <thinker@branda.to>
parents: 41
diff changeset
107 MB_TIMEVAL_CP(tmo_after, &timer->tmo);
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
108 MB_TIMEVAL_DIFF(tmo_after, now);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
109
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
110 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
111 }
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
112
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
113 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
114 mb_timer_t *timer;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
115
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
116 while((timer = STAILQ_HEAD(tman->timers)) != NULL){
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
117 if(MB_TIMEVAL_LATER(&timer->tmo, now))
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
118 break;
41
400b4b5db0dc Working on animation
Thinker K.F. Li <thinker@branda.to>
parents: 39
diff changeset
119 timer->hdlr(&timer->tmo, now, timer->arg);
39
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
120 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer);
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
121 elmpool_elm_free(tman->timer_pool, timer);
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
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 return OK;
db2aa914e14b timer for animation
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125 }
1018
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
126
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
127 /*! \defgroup tman_timer_man Timer manager based on mb_tman_t.
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
128 *
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
129 * This implmentation of timer manager is based on mb_tman_t.
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
130 * @{
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
131 */
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
132 struct _tman_timer_man {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
133 mb_timer_man_t timer_man;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
134 mb_tman_t *tman;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
135 };
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
136
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
137 static int _tman_timer_man_timeout(struct _mb_timer_man *tm_man,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
138 mb_timeval_t *tmout,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
139 mb_timer_cb_t cb, void *data);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
140 static void _tman_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
141 static mb_timer_man_t *_tman_timer_fact_new(void);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
142 static void _tman_timer_fact_free(mb_timer_man_t *timer_man);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
143
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
144 static struct _tman_timer_man _tman_default_timer_man = {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
145 {_tman_timer_man_timeout, _tman_timer_man_remove},
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
146 NULL
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
147 };
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
148
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
149 mb_timer_factory_t tman_timer_factory = {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
150 _tman_timer_fact_new,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
151 _tman_timer_fact_free
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
152 };
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
153
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
154 /*! \brief Content of a timeout request.
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
155 *
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
156 * This is only used by internal of X support. This data structure
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
157 * carry information to adopt mb_tman_t to mb_timer_man_t.
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
158 */
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
159 struct _tman_timeout_data {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
160 mb_timer_t *timer; /*!< Handle returned by mb_tman_timeout() */
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
161 mb_timer_cb_t cb; /*!< Real callback function */
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
162 void *data; /*!< data for real callback */
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
163 };
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
164
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
165 static void
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
166 _tman_tmo_hdlr(const mb_timeval_t *tmo,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
167 const mb_timeval_t *now,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
168 void *arg) {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
169 struct _tman_timeout_data *data = (struct _tman_timeout_data *)arg;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
170
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
171 data->cb((int)data, tmo, now, data->data);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
172 }
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
173
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
174 static int
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
175 _tman_timer_man_timeout(struct _mb_timer_man *tm_man,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
176 mb_timeval_t *tmout, /* timeout (wall time) */
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
177 mb_timer_cb_t cb, void *data) {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
178 struct _tman_timer_man *timer_man = (struct _tman_timer_man *)tm_man;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
179 mb_timer_t *timer;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
180 struct _tman_timeout_data *tmout_data;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
181
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
182 tmout_data = O_ALLOC(struct _tman_timeout_data);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
183 tmout_data->cb = cb;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
184 tmout_data->data = data;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
185 timer = mb_tman_timeout(timer_man->tman, tmout,
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
186 _tman_tmo_hdlr, tmout_data);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
187 if(timer == NULL)
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
188 return ERR;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
189 tmout_data->timer = timer;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
190
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
191 return (int)tmout_data;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
192 }
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
193
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
194 static void
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
195 _tman_timer_man_remove(struct _mb_timer_man *tm_man, int tm_hdl) {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
196 struct _tman_timer_man *timer_man = (struct _tman_timer_man *)tm_man;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
197 struct _tman_timeout_data *tmout_data =
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
198 (struct _tman_timeout_data *)tm_hdl;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
199
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
200 mb_tman_remove(timer_man->tman, tmout_data->timer);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
201 free(tmout_data);
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
202 }
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
203
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
204 static mb_timer_man_t *
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
205 _tman_timer_fact_new(void) {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
206 if(_tman_default_timer_man.tman == NULL)
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
207 _tman_default_timer_man.tman = mb_tman_new();
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
208 return (mb_timer_man_t *)&_tman_default_timer_man;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
209 }
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
210
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
211 static void
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
212 _tman_timer_fact_free(mb_timer_man_t *timer_man) {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
213 }
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
214
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
215 mb_tman_t *
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
216 tman_timer_man_get_tman(mb_timer_man_t *tm_man) {
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
217 struct _tman_timer_man *timer_man = (struct _tman_timer_man *)tm_man;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
218
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
219 return timer_man->tman;
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
220 }
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
221
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
222
7ccc094bdbe5 Move the timer manager based on mb_tman_t to timer.c
Thinker K.F. Li <thinker@codemud.net>
parents: 822
diff changeset
223 /* @} */