Mercurial > MadButterfly
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 | 3 #include <stdio.h> |
4 #include <stdint.h> | |
5 #include <stdlib.h> | |
41 | 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 | 9 |
10 | |
11 #define OK 0 | |
12 #define ERR -1 | |
13 | |
14 struct _mb_timer { | |
41 | 15 mb_timeval_t tmo; |
39 | 16 mb_tmo_hdlr hdlr; |
17 void *arg; | |
18 mb_timer_t *next; | |
19 }; | |
20 | |
21 struct _mb_tman { | |
22 STAILQ(mb_timer_t) timers; | |
23 elmpool_t *timer_pool; | |
24 }; | |
25 | |
26 mb_tman_t *mb_tman_new(void) { | |
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 | 29 tman = (mb_tman_t *)malloc(sizeof(mb_tman_t)); |
30 if(tman == NULL) | |
31 return NULL; | |
32 | |
33 tman->timer_pool = elmpool_new(sizeof(mb_timer_t), 32); | |
34 if(tman->timer_pool == NULL) { | |
35 free(tman); | |
36 return NULL; | |
37 } | |
38 | |
39 STAILQ_INIT(tman->timers); | |
40 | |
41 return tman; | |
42 } | |
43 | |
44 void mb_tman_free(mb_tman_t *tman) { | |
45 elmpool_free(tman->timer_pool); | |
46 free(tman); | |
47 } | |
48 | |
49 mb_timer_t *mb_tman_timeout(mb_tman_t *tman, | |
41 | 50 const mb_timeval_t *tmo, |
39 | 51 mb_tmo_hdlr hdlr, void *arg) { |
52 mb_timer_t *timer, *visit, *last; | |
53 | |
54 timer = elmpool_elm_alloc(tman->timer_pool); | |
55 if(timer == NULL) | |
56 return NULL; | |
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 | 59 timer->hdlr = hdlr; |
60 timer->arg = arg; | |
61 | |
62 last = NULL; | |
63 for(visit = STAILQ_HEAD(tman->timers); | |
64 visit != NULL; | |
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 | 68 break; |
69 last = visit; | |
70 } | |
71 | |
72 if(last == NULL) | |
73 STAILQ_INS(tman->timers, mb_timer_t, next, timer); | |
74 else if(visit == NULL) | |
75 STAILQ_INS_TAIL(tman->timers, mb_timer_t, next, timer); | |
76 else | |
77 STAILQ_INS_AFTER(mb_timer_t, next, timer, last); | |
78 | |
79 return timer; | |
80 } | |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
186
diff
changeset
|
81 |
39 | 82 int mb_tman_remove(mb_tman_t *tman, mb_timer_t *timer) { |
83 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer); | |
84 elmpool_elm_free(tman->timer_pool, timer); | |
85 | |
86 return OK; | |
87 } | |
88 | |
50 | 89 /*! \brief Get how long to next timeout from this monent. |
90 * | |
91 * \return 0 for having next timeout, -1 for not more timeout. | |
92 */ | |
39 | 93 int mb_tman_next_timeout(mb_tman_t *tman, |
41 | 94 const mb_timeval_t *now, mb_timeval_t *tmo_after) { |
39 | 95 mb_timer_t *timer; |
96 | |
97 timer = STAILQ_HEAD(tman->timers); | |
98 if(timer == NULL) | |
99 return ERR; | |
100 | |
41 | 101 if(!MB_TIMEVAL_LATER(&timer->tmo, now)) { |
102 memset(tmo_after, 0, sizeof(mb_timeval_t)); | |
39 | 103 return OK; |
104 } | |
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 | 107 MB_TIMEVAL_DIFF(tmo_after, now); |
39 | 108 |
109 return OK; | |
110 } | |
111 | |
41 | 112 int mb_tman_handle_timeout(mb_tman_t *tman, mb_timeval_t *now) { |
39 | 113 mb_timer_t *timer; |
114 | |
115 while((timer = STAILQ_HEAD(tman->timers)) != NULL){ | |
41 | 116 if(MB_TIMEVAL_LATER(&timer->tmo, now)) |
39 | 117 break; |
41 | 118 timer->hdlr(&timer->tmo, now, timer->arg); |
39 | 119 STAILQ_REMOVE(tman->timers, mb_timer_t, next, timer); |
120 elmpool_elm_free(tman->timer_pool, timer); | |
121 } | |
122 | |
123 return OK; | |
124 } |