1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | #ifdef __rtems__ |
---|
3 | #include <machine/rtems-bsd-program.h> |
---|
4 | #include "rtems-bsd-racoon-namespace.h" |
---|
5 | #endif /* __rtems__ */ |
---|
6 | |
---|
7 | /* $NetBSD: schedule.c,v 1.7 2009/01/23 09:10:13 tteras Exp $ */ |
---|
8 | |
---|
9 | /* $KAME: schedule.c,v 1.19 2001/11/05 10:53:19 sakane Exp $ */ |
---|
10 | |
---|
11 | /* |
---|
12 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
---|
13 | * Copyright (C) 2008 Timo Teras. |
---|
14 | * All rights reserved. |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * 3. Neither the name of the project nor the names of its contributors |
---|
25 | * may be used to endorse or promote products derived from this software |
---|
26 | * without specific prior written permission. |
---|
27 | * |
---|
28 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
---|
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
---|
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
38 | * SUCH DAMAGE. |
---|
39 | */ |
---|
40 | |
---|
41 | #include "config.h" |
---|
42 | |
---|
43 | #include <sys/types.h> |
---|
44 | #include <sys/param.h> |
---|
45 | #include <sys/time.h> |
---|
46 | #include <sys/queue.h> |
---|
47 | #include <sys/socket.h> |
---|
48 | |
---|
49 | #include <stdlib.h> |
---|
50 | #include <unistd.h> |
---|
51 | #include <stdio.h> |
---|
52 | #include <string.h> |
---|
53 | #include <errno.h> |
---|
54 | #include <time.h> |
---|
55 | |
---|
56 | #include "misc.h" |
---|
57 | #include "plog.h" |
---|
58 | #include "schedule.h" |
---|
59 | #include "var.h" |
---|
60 | #include "gcmalloc.h" |
---|
61 | |
---|
62 | #ifndef TAILQ_FOREACH |
---|
63 | #define TAILQ_FOREACH(elm, head, field) \ |
---|
64 | for (elm = TAILQ_FIRST(head); elm; elm = TAILQ_NEXT(elm, field)) |
---|
65 | #endif |
---|
66 | |
---|
67 | static TAILQ_HEAD(_schedtree, sched) sctree; |
---|
68 | |
---|
69 | void |
---|
70 | sched_get_monotonic_time(tv) |
---|
71 | struct timeval *tv; |
---|
72 | { |
---|
73 | #ifdef HAVE_CLOCK_MONOTONIC |
---|
74 | struct timespec ts; |
---|
75 | |
---|
76 | clock_gettime(CLOCK_MONOTONIC, &ts); |
---|
77 | tv->tv_sec = ts.tv_sec; |
---|
78 | tv->tv_usec = ts.tv_nsec / 1000; |
---|
79 | #else |
---|
80 | gettimeofday(tv, NULL); |
---|
81 | #endif |
---|
82 | } |
---|
83 | |
---|
84 | time_t |
---|
85 | sched_monotonic_to_time_t(tv, now) |
---|
86 | struct timeval *tv, *now; |
---|
87 | { |
---|
88 | #ifdef HAVE_CLOCK_MONOTONIC |
---|
89 | struct timeval mynow, res; |
---|
90 | |
---|
91 | if (now == NULL) { |
---|
92 | sched_get_monotonic_time(&mynow); |
---|
93 | now = &mynow; |
---|
94 | } |
---|
95 | timersub(now, tv, &res); |
---|
96 | |
---|
97 | return time(NULL) + res.tv_sec; |
---|
98 | #else |
---|
99 | return tv->tv_sec; |
---|
100 | #endif |
---|
101 | } |
---|
102 | |
---|
103 | /* |
---|
104 | * schedule handler |
---|
105 | * OUT: |
---|
106 | * time to block until next event. |
---|
107 | * if no entry, NULL returned. |
---|
108 | */ |
---|
109 | struct timeval * |
---|
110 | schedular() |
---|
111 | { |
---|
112 | static struct timeval timeout; |
---|
113 | struct timeval now; |
---|
114 | struct sched *p; |
---|
115 | |
---|
116 | sched_get_monotonic_time(&now); |
---|
117 | while (!TAILQ_EMPTY(&sctree) && |
---|
118 | timercmp(&TAILQ_FIRST(&sctree)->xtime, &now, <=)) { |
---|
119 | void (*func)(struct sched *); |
---|
120 | |
---|
121 | p = TAILQ_FIRST(&sctree); |
---|
122 | func = p->func; |
---|
123 | sched_cancel(p); |
---|
124 | func(p); |
---|
125 | } |
---|
126 | |
---|
127 | p = TAILQ_FIRST(&sctree); |
---|
128 | if (p == NULL) |
---|
129 | return NULL; |
---|
130 | |
---|
131 | timersub(&p->xtime, &now, &timeout); |
---|
132 | |
---|
133 | return &timeout; |
---|
134 | } |
---|
135 | |
---|
136 | /* |
---|
137 | * add new schedule to schedule table. |
---|
138 | */ |
---|
139 | void |
---|
140 | sched_schedule(sc, tick, func) |
---|
141 | struct sched *sc; |
---|
142 | time_t tick; |
---|
143 | void (*func) __P((struct sched *)); |
---|
144 | { |
---|
145 | static long id = 1; |
---|
146 | struct sched *p; |
---|
147 | struct timeval now; |
---|
148 | |
---|
149 | sched_cancel(sc); |
---|
150 | |
---|
151 | sc->func = func; |
---|
152 | sc->id = id++; |
---|
153 | sc->tick.tv_sec = tick; |
---|
154 | sc->tick.tv_usec = 0; |
---|
155 | sched_get_monotonic_time(&now); |
---|
156 | timeradd(&now, &sc->tick, &sc->xtime); |
---|
157 | |
---|
158 | /* add to schedule table */ |
---|
159 | TAILQ_FOREACH(p, &sctree, chain) { |
---|
160 | if (timercmp(&sc->xtime, &p->xtime, <)) |
---|
161 | break; |
---|
162 | } |
---|
163 | if (p == NULL) |
---|
164 | TAILQ_INSERT_TAIL(&sctree, sc, chain); |
---|
165 | else |
---|
166 | TAILQ_INSERT_BEFORE(p, sc, chain); |
---|
167 | } |
---|
168 | |
---|
169 | /* |
---|
170 | * cancel scheduled callback |
---|
171 | */ |
---|
172 | void |
---|
173 | sched_cancel(sc) |
---|
174 | struct sched *sc; |
---|
175 | { |
---|
176 | if (sc->func != NULL) { |
---|
177 | TAILQ_REMOVE(&sctree, sc, chain); |
---|
178 | sc->func = NULL; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | /* |
---|
183 | * for debug |
---|
184 | */ |
---|
185 | int |
---|
186 | sched_dump(buf, len) |
---|
187 | caddr_t *buf; |
---|
188 | int *len; |
---|
189 | { |
---|
190 | caddr_t new; |
---|
191 | struct sched *p; |
---|
192 | struct scheddump *dst; |
---|
193 | struct timeval now, created; |
---|
194 | int cnt = 0; |
---|
195 | |
---|
196 | /* initialize */ |
---|
197 | *len = 0; |
---|
198 | *buf = NULL; |
---|
199 | |
---|
200 | TAILQ_FOREACH(p, &sctree, chain) |
---|
201 | cnt++; |
---|
202 | |
---|
203 | /* no entry */ |
---|
204 | if (cnt == 0) |
---|
205 | return -1; |
---|
206 | |
---|
207 | *len = cnt * sizeof(*dst); |
---|
208 | |
---|
209 | new = racoon_malloc(*len); |
---|
210 | if (new == NULL) |
---|
211 | return -1; |
---|
212 | dst = (struct scheddump *)new; |
---|
213 | |
---|
214 | sched_get_monotonic_time(&now); |
---|
215 | p = TAILQ_FIRST(&sctree); |
---|
216 | while (p) { |
---|
217 | timersub(&p->xtime, &p->tick, &created); |
---|
218 | dst->xtime = p->xtime.tv_sec; |
---|
219 | dst->id = p->id; |
---|
220 | dst->created = sched_monotonic_to_time_t(&created, &now); |
---|
221 | dst->tick = p->tick.tv_sec; |
---|
222 | |
---|
223 | p = TAILQ_NEXT(p, chain); |
---|
224 | if (p == NULL) |
---|
225 | break; |
---|
226 | dst++; |
---|
227 | } |
---|
228 | |
---|
229 | *buf = new; |
---|
230 | |
---|
231 | return 0; |
---|
232 | } |
---|
233 | |
---|
234 | /* initialize schedule table */ |
---|
235 | void |
---|
236 | sched_init() |
---|
237 | { |
---|
238 | TAILQ_INIT(&sctree); |
---|
239 | } |
---|
240 | |
---|
241 | #ifdef STEST |
---|
242 | #include <sys/types.h> |
---|
243 | #include <sys/time.h> |
---|
244 | #include <unistd.h> |
---|
245 | #include <err.h> |
---|
246 | |
---|
247 | void |
---|
248 | test(tick) |
---|
249 | int *tick; |
---|
250 | { |
---|
251 | printf("execute %d\n", *tick); |
---|
252 | racoon_free(tick); |
---|
253 | } |
---|
254 | |
---|
255 | void |
---|
256 | getstdin() |
---|
257 | { |
---|
258 | int *tick; |
---|
259 | char buf[16]; |
---|
260 | |
---|
261 | read(0, buf, sizeof(buf)); |
---|
262 | if (buf[0] == 'd') { |
---|
263 | struct scheddump *scbuf, *p; |
---|
264 | int len; |
---|
265 | sched_dump((caddr_t *)&scbuf, &len); |
---|
266 | if (scbuf == NULL) |
---|
267 | return; |
---|
268 | for (p = scbuf; len; p++) { |
---|
269 | printf("xtime=%ld\n", p->xtime); |
---|
270 | len -= sizeof(*p); |
---|
271 | } |
---|
272 | racoon_free(scbuf); |
---|
273 | return; |
---|
274 | } |
---|
275 | |
---|
276 | tick = (int *)racoon_malloc(sizeof(*tick)); |
---|
277 | *tick = atoi(buf); |
---|
278 | printf("new queue tick = %d\n", *tick); |
---|
279 | sched_new(*tick, test, tick); |
---|
280 | } |
---|
281 | |
---|
282 | int |
---|
283 | main() |
---|
284 | { |
---|
285 | static fd_set mask0; |
---|
286 | int nfds = 0; |
---|
287 | fd_set rfds; |
---|
288 | struct timeval *timeout; |
---|
289 | int error; |
---|
290 | |
---|
291 | FD_ZERO(&mask0); |
---|
292 | FD_SET(0, &mask0); |
---|
293 | nfds = 1; |
---|
294 | |
---|
295 | /* initialize */ |
---|
296 | sched_init(); |
---|
297 | |
---|
298 | while (1) { |
---|
299 | rfds = mask0; |
---|
300 | |
---|
301 | timeout = schedular(); |
---|
302 | |
---|
303 | error = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, timeout); |
---|
304 | if (error < 0) { |
---|
305 | switch (errno) { |
---|
306 | case EINTR: continue; |
---|
307 | default: |
---|
308 | err(1, "select"); |
---|
309 | } |
---|
310 | /*NOTREACHED*/ |
---|
311 | } |
---|
312 | |
---|
313 | if (FD_ISSET(0, &rfds)) |
---|
314 | getstdin(); |
---|
315 | } |
---|
316 | } |
---|
317 | #endif |
---|
318 | #ifdef __rtems__ |
---|
319 | #include "rtems-bsd-racoon-schedule-data.h" |
---|
320 | #endif /* __rtems__ */ |
---|