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