timer.c revision 1.10.6.1 1 /* $NetBSD: timer.c,v 1.10.6.1 2013/02/25 00:30:49 tls Exp $ */
2 /* $KAME: timer.c,v 1.11 2005/04/14 06:22:35 suz Exp $ */
3
4 /*
5 * Copyright (C) 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/queue.h>
34 #include <sys/time.h>
35
36 #include <unistd.h>
37 #include <syslog.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <search.h>
41 #include "timer.h"
42
43 struct rtadvd_timer_head_t ra_timer = TAILQ_HEAD_INITIALIZER(ra_timer);
44
45 #define MILLION 1000000
46 #define TIMEVAL_EQUAL(t1,t2) ((t1)->tv_sec == (t2)->tv_sec &&\
47 (t1)->tv_usec == (t2)->tv_usec)
48
49 static struct timeval tm_limit = {0x7fffffff, 0x7fffffff};
50 static struct timeval tm_max;
51
52 void
53 rtadvd_timer_init(void)
54 {
55
56 TAILQ_INIT(&ra_timer);
57 tm_max = tm_limit;
58 }
59
60 struct rtadvd_timer *
61 rtadvd_add_timer(struct rtadvd_timer *(*timeout) (void *),
62 void (*update) (void *, struct timeval *),
63 void *timeodata, void *updatedata)
64 {
65 struct rtadvd_timer *newtimer;
66
67 if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
68 syslog(LOG_ERR,
69 "<%s> can't allocate memory", __func__);
70 exit(1);
71 }
72
73 memset(newtimer, 0, sizeof(*newtimer));
74
75 if (timeout == NULL) {
76 syslog(LOG_ERR,
77 "<%s> timeout function unspecified", __func__);
78 exit(1);
79 }
80 newtimer->expire = timeout;
81 newtimer->update = update;
82 newtimer->expire_data = timeodata;
83 newtimer->update_data = updatedata;
84 newtimer->tm = tm_max;
85
86 /* link into chain */
87 TAILQ_INSERT_TAIL(&ra_timer, newtimer, next);
88
89 return(newtimer);
90 }
91
92 void
93 rtadvd_remove_timer(struct rtadvd_timer **timer)
94 {
95
96 if (*timer) {
97 TAILQ_REMOVE(&ra_timer, *timer, next);
98 free(*timer);
99 *timer = NULL;
100 }
101 }
102
103 void
104 rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *timer)
105 {
106 struct timeval now;
107
108 /* reset the timer */
109 gettimeofday(&now, NULL);
110
111 TIMEVAL_ADD(&now, tm, &timer->tm);
112
113 /* update the next expiration time */
114 if (TIMEVAL_LT(timer->tm, tm_max))
115 tm_max = timer->tm;
116
117 return;
118 }
119
120 /*
121 * Check expiration for each timer. If a timer expires,
122 * call the expire function for the timer and update the timer.
123 * Return the next interval for select() call.
124 */
125 struct timeval *
126 rtadvd_check_timer(void)
127 {
128 static struct timeval returnval;
129 struct timeval now;
130 struct rtadvd_timer *tm, *tmn;
131
132 gettimeofday(&now, NULL);
133 tm_max = tm_limit;
134
135 TAILQ_FOREACH_SAFE(tm, &ra_timer, next, tmn) {
136 if (TIMEVAL_LEQ(tm->tm, now)) {
137 if ((*tm->expire)(tm->expire_data) == NULL)
138 continue; /* the timer was removed */
139 if (tm->update)
140 (*tm->update)(tm->update_data, &tm->tm);
141 TIMEVAL_ADD(&tm->tm, &now, &tm->tm);
142 }
143
144 if (TIMEVAL_LT(tm->tm, tm_max))
145 tm_max = tm->tm;
146 }
147
148 if (TIMEVAL_EQUAL(&tm_max, &tm_limit)) {
149 /* no need to timeout */
150 return(NULL);
151 } else if (TIMEVAL_LT(tm_max, now)) {
152 /* this may occur when the interval is too small */
153 returnval.tv_sec = returnval.tv_usec = 0;
154 } else
155 TIMEVAL_SUB(&tm_max, &now, &returnval);
156 return(&returnval);
157 }
158
159 struct timeval *
160 rtadvd_timer_rest(struct rtadvd_timer *timer)
161 {
162 static struct timeval returnval, now;
163
164 gettimeofday(&now, NULL);
165 if (TIMEVAL_LEQ(timer->tm, now)) {
166 syslog(LOG_DEBUG,
167 "<%s> a timer must be expired, but not yet",
168 __func__);
169 returnval.tv_sec = returnval.tv_usec = 0;
170 }
171 else
172 TIMEVAL_SUB(&timer->tm, &now, &returnval);
173
174 return(&returnval);
175 }
176
177 /* result = a + b */
178 void
179 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
180 {
181 long l;
182
183 if ((l = a->tv_usec + b->tv_usec) < MILLION) {
184 result->tv_usec = l;
185 result->tv_sec = a->tv_sec + b->tv_sec;
186 }
187 else {
188 result->tv_usec = l - MILLION;
189 result->tv_sec = a->tv_sec + b->tv_sec + 1;
190 }
191 }
192
193 /*
194 * result = a - b
195 * XXX: this function assumes that a >= b.
196 */
197 void
198 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
199 {
200 long l;
201
202 if ((l = a->tv_usec - b->tv_usec) >= 0) {
203 result->tv_usec = l;
204 result->tv_sec = a->tv_sec - b->tv_sec;
205 }
206 else {
207 result->tv_usec = MILLION + l;
208 result->tv_sec = a->tv_sec - b->tv_sec - 1;
209 }
210 }
211