ev_timers.c revision 1.6 1 1.6 christos /* $NetBSD: ev_timers.c,v 1.6 2008/06/21 20:41:48 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1995-1999 by Internet Software Consortium
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos /* ev_timers.c - implement timers for the eventlib
21 1.1 christos * vix 09sep95 [initial]
22 1.1 christos */
23 1.1 christos
24 1.2 christos #include <sys/cdefs.h>
25 1.2 christos #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
26 1.2 christos #ifdef notdef
27 1.6 christos static const char rcsid[] = "Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp";
28 1.2 christos #else
29 1.6 christos __RCSID("$NetBSD: ev_timers.c,v 1.6 2008/06/21 20:41:48 christos Exp $");
30 1.2 christos #endif
31 1.1 christos #endif
32 1.1 christos
33 1.1 christos /* Import. */
34 1.1 christos
35 1.1 christos #include "port_before.h"
36 1.1 christos #include "fd_setsize.h"
37 1.1 christos
38 1.1 christos #include <errno.h>
39 1.1 christos
40 1.1 christos #include <isc/assertions.h>
41 1.1 christos #include <isc/eventlib.h>
42 1.1 christos #include "eventlib_p.h"
43 1.1 christos
44 1.1 christos #include "port_after.h"
45 1.1 christos
46 1.1 christos /* Constants. */
47 1.1 christos
48 1.1 christos #define MILLION 1000000
49 1.1 christos #define BILLION 1000000000
50 1.1 christos
51 1.1 christos /* Forward. */
52 1.1 christos
53 1.2 christos #ifndef _LIBC
54 1.1 christos static int due_sooner(void *, void *);
55 1.1 christos static void set_index(void *, int);
56 1.1 christos static void free_timer(void *, void *);
57 1.1 christos static void print_timer(void *, void *);
58 1.1 christos static void idle_timeout(evContext, void *, struct timespec, struct timespec);
59 1.1 christos
60 1.1 christos /* Private type. */
61 1.1 christos
62 1.1 christos typedef struct {
63 1.1 christos evTimerFunc func;
64 1.1 christos void * uap;
65 1.1 christos struct timespec lastTouched;
66 1.1 christos struct timespec max_idle;
67 1.1 christos evTimer * timer;
68 1.1 christos } idle_timer;
69 1.2 christos #endif
70 1.1 christos
71 1.1 christos /* Public. */
72 1.1 christos
73 1.1 christos struct timespec
74 1.1 christos evConsTime(time_t sec, long nsec) {
75 1.1 christos struct timespec x;
76 1.1 christos
77 1.1 christos x.tv_sec = sec;
78 1.1 christos x.tv_nsec = nsec;
79 1.1 christos return (x);
80 1.1 christos }
81 1.1 christos
82 1.1 christos struct timespec
83 1.1 christos evAddTime(struct timespec addend1, struct timespec addend2) {
84 1.1 christos struct timespec x;
85 1.1 christos
86 1.1 christos x.tv_sec = addend1.tv_sec + addend2.tv_sec;
87 1.1 christos x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
88 1.1 christos if (x.tv_nsec >= BILLION) {
89 1.1 christos x.tv_sec++;
90 1.1 christos x.tv_nsec -= BILLION;
91 1.1 christos }
92 1.1 christos return (x);
93 1.1 christos }
94 1.1 christos
95 1.1 christos struct timespec
96 1.1 christos evSubTime(struct timespec minuend, struct timespec subtrahend) {
97 1.1 christos struct timespec x;
98 1.1 christos
99 1.1 christos x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
100 1.1 christos if (minuend.tv_nsec >= subtrahend.tv_nsec)
101 1.1 christos x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
102 1.1 christos else {
103 1.1 christos x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
104 1.1 christos x.tv_sec--;
105 1.1 christos }
106 1.1 christos return (x);
107 1.1 christos }
108 1.1 christos
109 1.1 christos int
110 1.1 christos evCmpTime(struct timespec a, struct timespec b) {
111 1.1 christos long x = a.tv_sec - b.tv_sec;
112 1.1 christos
113 1.1 christos if (x == 0L)
114 1.1 christos x = a.tv_nsec - b.tv_nsec;
115 1.1 christos return (x < 0L ? (-1) : x > 0L ? (1) : (0));
116 1.1 christos }
117 1.1 christos
118 1.1 christos struct timespec
119 1.1 christos evNowTime() {
120 1.1 christos struct timeval now;
121 1.1 christos #ifdef CLOCK_REALTIME
122 1.1 christos struct timespec tsnow;
123 1.1 christos int m = CLOCK_REALTIME;
124 1.1 christos
125 1.1 christos #ifdef CLOCK_MONOTONIC
126 1.4 christos #ifndef _LIBC
127 1.1 christos if (__evOptMonoTime)
128 1.1 christos m = CLOCK_MONOTONIC;
129 1.1 christos #endif
130 1.4 christos #endif
131 1.1 christos if (clock_gettime(m, &tsnow) == 0)
132 1.1 christos return (tsnow);
133 1.1 christos #endif
134 1.1 christos if (gettimeofday(&now, NULL) < 0)
135 1.2 christos return (evConsTime(0L, 0L));
136 1.1 christos return (evTimeSpec(now));
137 1.1 christos }
138 1.1 christos
139 1.1 christos struct timespec
140 1.2 christos evUTCTime(void) {
141 1.1 christos struct timeval now;
142 1.1 christos #ifdef CLOCK_REALTIME
143 1.1 christos struct timespec tsnow;
144 1.1 christos if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
145 1.1 christos return (tsnow);
146 1.1 christos #endif
147 1.1 christos if (gettimeofday(&now, NULL) < 0)
148 1.2 christos return (evConsTime(0L, 0L));
149 1.1 christos return (evTimeSpec(now));
150 1.1 christos }
151 1.1 christos
152 1.2 christos #ifndef _LIBC
153 1.1 christos struct timespec
154 1.1 christos evLastEventTime(evContext opaqueCtx) {
155 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
156 1.1 christos
157 1.1 christos return (ctx->lastEventTime);
158 1.1 christos }
159 1.2 christos #endif
160 1.1 christos
161 1.1 christos struct timespec
162 1.1 christos evTimeSpec(struct timeval tv) {
163 1.1 christos struct timespec ts;
164 1.1 christos
165 1.1 christos ts.tv_sec = tv.tv_sec;
166 1.1 christos ts.tv_nsec = tv.tv_usec * 1000;
167 1.1 christos return (ts);
168 1.1 christos }
169 1.1 christos
170 1.1 christos struct timeval
171 1.1 christos evTimeVal(struct timespec ts) {
172 1.1 christos struct timeval tv;
173 1.1 christos
174 1.1 christos tv.tv_sec = ts.tv_sec;
175 1.1 christos tv.tv_usec = ts.tv_nsec / 1000;
176 1.1 christos return (tv);
177 1.1 christos }
178 1.1 christos
179 1.2 christos #ifndef _LIBC
180 1.1 christos int
181 1.1 christos evSetTimer(evContext opaqueCtx,
182 1.1 christos evTimerFunc func,
183 1.1 christos void *uap,
184 1.1 christos struct timespec due,
185 1.1 christos struct timespec inter,
186 1.1 christos evTimerID *opaqueID
187 1.1 christos ) {
188 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
189 1.1 christos evTimer *id;
190 1.1 christos
191 1.1 christos evPrintf(ctx, 1,
192 1.1 christos "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
193 1.1 christos ctx, func, uap,
194 1.1 christos (long)due.tv_sec, due.tv_nsec,
195 1.1 christos (long)inter.tv_sec, inter.tv_nsec);
196 1.1 christos
197 1.1 christos #ifdef __hpux
198 1.1 christos /*
199 1.1 christos * tv_sec and tv_nsec are unsigned.
200 1.1 christos */
201 1.1 christos if (due.tv_nsec >= BILLION)
202 1.1 christos EV_ERR(EINVAL);
203 1.1 christos
204 1.1 christos if (inter.tv_nsec >= BILLION)
205 1.1 christos EV_ERR(EINVAL);
206 1.1 christos #else
207 1.1 christos if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
208 1.1 christos EV_ERR(EINVAL);
209 1.1 christos
210 1.1 christos if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
211 1.1 christos EV_ERR(EINVAL);
212 1.1 christos #endif
213 1.1 christos
214 1.1 christos /* due={0,0} is a magic cookie meaning "now." */
215 1.1 christos if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
216 1.1 christos due = evNowTime();
217 1.1 christos
218 1.1 christos /* Allocate and fill. */
219 1.1 christos OKNEW(id);
220 1.1 christos id->func = func;
221 1.1 christos id->uap = uap;
222 1.1 christos id->due = due;
223 1.1 christos id->inter = inter;
224 1.1 christos
225 1.1 christos if (heap_insert(ctx->timers, id) < 0)
226 1.1 christos return (-1);
227 1.1 christos
228 1.1 christos /* Remember the ID if the caller provided us a place for it. */
229 1.1 christos if (opaqueID)
230 1.1 christos opaqueID->opaque = id;
231 1.1 christos
232 1.1 christos if (ctx->debug > 7) {
233 1.1 christos evPrintf(ctx, 7, "timers after evSetTimer:\n");
234 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
235 1.1 christos }
236 1.1 christos
237 1.1 christos return (0);
238 1.1 christos }
239 1.1 christos
240 1.1 christos int
241 1.1 christos evClearTimer(evContext opaqueCtx, evTimerID id) {
242 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
243 1.1 christos evTimer *del = id.opaque;
244 1.1 christos
245 1.1 christos if (ctx->cur != NULL &&
246 1.1 christos ctx->cur->type == Timer &&
247 1.1 christos ctx->cur->u.timer.this == del) {
248 1.1 christos evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
249 1.1 christos /*
250 1.1 christos * Setting the interval to zero ensures that evDrop() will
251 1.1 christos * clean up the timer.
252 1.1 christos */
253 1.1 christos del->inter = evConsTime(0, 0);
254 1.1 christos return (0);
255 1.1 christos }
256 1.1 christos
257 1.1 christos if (heap_element(ctx->timers, del->index) != del)
258 1.1 christos EV_ERR(ENOENT);
259 1.1 christos
260 1.1 christos if (heap_delete(ctx->timers, del->index) < 0)
261 1.1 christos return (-1);
262 1.1 christos FREE(del);
263 1.1 christos
264 1.1 christos if (ctx->debug > 7) {
265 1.1 christos evPrintf(ctx, 7, "timers after evClearTimer:\n");
266 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
267 1.1 christos }
268 1.1 christos
269 1.1 christos return (0);
270 1.1 christos }
271 1.1 christos
272 1.1 christos int
273 1.1 christos evConfigTimer(evContext opaqueCtx,
274 1.1 christos evTimerID id,
275 1.1 christos const char *param,
276 1.1 christos int value
277 1.1 christos ) {
278 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
279 1.1 christos evTimer *timer = id.opaque;
280 1.1 christos int result=0;
281 1.1 christos
282 1.1 christos UNUSED(value);
283 1.1 christos
284 1.1 christos if (heap_element(ctx->timers, timer->index) != timer)
285 1.1 christos EV_ERR(ENOENT);
286 1.1 christos
287 1.1 christos if (strcmp(param, "rate") == 0)
288 1.1 christos timer->mode |= EV_TMR_RATE;
289 1.1 christos else if (strcmp(param, "interval") == 0)
290 1.1 christos timer->mode &= ~EV_TMR_RATE;
291 1.1 christos else
292 1.1 christos EV_ERR(EINVAL);
293 1.1 christos
294 1.1 christos return (result);
295 1.1 christos }
296 1.1 christos
297 1.1 christos int
298 1.1 christos evResetTimer(evContext opaqueCtx,
299 1.1 christos evTimerID id,
300 1.1 christos evTimerFunc func,
301 1.1 christos void *uap,
302 1.1 christos struct timespec due,
303 1.1 christos struct timespec inter
304 1.1 christos ) {
305 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
306 1.1 christos evTimer *timer = id.opaque;
307 1.1 christos struct timespec old_due;
308 1.1 christos int result=0;
309 1.1 christos
310 1.1 christos if (heap_element(ctx->timers, timer->index) != timer)
311 1.1 christos EV_ERR(ENOENT);
312 1.1 christos
313 1.1 christos #ifdef __hpux
314 1.1 christos /*
315 1.1 christos * tv_sec and tv_nsec are unsigned.
316 1.1 christos */
317 1.1 christos if (due.tv_nsec >= BILLION)
318 1.1 christos EV_ERR(EINVAL);
319 1.1 christos
320 1.1 christos if (inter.tv_nsec >= BILLION)
321 1.1 christos EV_ERR(EINVAL);
322 1.1 christos #else
323 1.1 christos if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
324 1.1 christos EV_ERR(EINVAL);
325 1.1 christos
326 1.1 christos if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
327 1.1 christos EV_ERR(EINVAL);
328 1.1 christos #endif
329 1.1 christos
330 1.1 christos old_due = timer->due;
331 1.1 christos
332 1.1 christos timer->func = func;
333 1.1 christos timer->uap = uap;
334 1.1 christos timer->due = due;
335 1.1 christos timer->inter = inter;
336 1.1 christos
337 1.1 christos switch (evCmpTime(due, old_due)) {
338 1.1 christos case -1:
339 1.1 christos result = heap_increased(ctx->timers, timer->index);
340 1.1 christos break;
341 1.1 christos case 0:
342 1.1 christos result = 0;
343 1.1 christos break;
344 1.1 christos case 1:
345 1.1 christos result = heap_decreased(ctx->timers, timer->index);
346 1.1 christos break;
347 1.1 christos }
348 1.1 christos
349 1.1 christos if (ctx->debug > 7) {
350 1.1 christos evPrintf(ctx, 7, "timers after evResetTimer:\n");
351 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
352 1.1 christos }
353 1.1 christos
354 1.1 christos return (result);
355 1.1 christos }
356 1.1 christos
357 1.1 christos int
358 1.1 christos evSetIdleTimer(evContext opaqueCtx,
359 1.1 christos evTimerFunc func,
360 1.1 christos void *uap,
361 1.1 christos struct timespec max_idle,
362 1.1 christos evTimerID *opaqueID
363 1.1 christos ) {
364 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
365 1.1 christos idle_timer *tt;
366 1.1 christos
367 1.1 christos /* Allocate and fill. */
368 1.1 christos OKNEW(tt);
369 1.1 christos tt->func = func;
370 1.1 christos tt->uap = uap;
371 1.1 christos tt->lastTouched = ctx->lastEventTime;
372 1.1 christos tt->max_idle = max_idle;
373 1.1 christos
374 1.1 christos if (evSetTimer(opaqueCtx, idle_timeout, tt,
375 1.1 christos evAddTime(ctx->lastEventTime, max_idle),
376 1.1 christos max_idle, opaqueID) < 0) {
377 1.1 christos FREE(tt);
378 1.1 christos return (-1);
379 1.1 christos }
380 1.1 christos
381 1.1 christos tt->timer = opaqueID->opaque;
382 1.1 christos
383 1.1 christos return (0);
384 1.1 christos }
385 1.1 christos
386 1.1 christos int
387 1.1 christos evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
388 1.1 christos evTimer *del = id.opaque;
389 1.1 christos idle_timer *tt = del->uap;
390 1.1 christos
391 1.1 christos FREE(tt);
392 1.1 christos return (evClearTimer(opaqueCtx, id));
393 1.1 christos }
394 1.1 christos
395 1.1 christos int
396 1.1 christos evResetIdleTimer(evContext opaqueCtx,
397 1.1 christos evTimerID opaqueID,
398 1.1 christos evTimerFunc func,
399 1.1 christos void *uap,
400 1.1 christos struct timespec max_idle
401 1.1 christos ) {
402 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
403 1.1 christos evTimer *timer = opaqueID.opaque;
404 1.1 christos idle_timer *tt = timer->uap;
405 1.1 christos
406 1.1 christos tt->func = func;
407 1.1 christos tt->uap = uap;
408 1.1 christos tt->lastTouched = ctx->lastEventTime;
409 1.1 christos tt->max_idle = max_idle;
410 1.1 christos
411 1.1 christos return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
412 1.1 christos evAddTime(ctx->lastEventTime, max_idle),
413 1.1 christos max_idle));
414 1.1 christos }
415 1.1 christos
416 1.1 christos int
417 1.1 christos evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
418 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
419 1.1 christos evTimer *t = id.opaque;
420 1.1 christos idle_timer *tt = t->uap;
421 1.1 christos
422 1.1 christos tt->lastTouched = ctx->lastEventTime;
423 1.1 christos
424 1.1 christos return (0);
425 1.1 christos }
426 1.1 christos
427 1.1 christos /* Public to the rest of eventlib. */
428 1.1 christos
429 1.1 christos heap_context
430 1.1 christos evCreateTimers(const evContext_p *ctx) {
431 1.1 christos
432 1.1 christos UNUSED(ctx);
433 1.1 christos
434 1.1 christos return (heap_new(due_sooner, set_index, 2048));
435 1.1 christos }
436 1.1 christos
437 1.1 christos void
438 1.1 christos evDestroyTimers(const evContext_p *ctx) {
439 1.1 christos (void) heap_for_each(ctx->timers, free_timer, NULL);
440 1.1 christos (void) heap_free(ctx->timers);
441 1.1 christos }
442 1.1 christos
443 1.1 christos /* Private. */
444 1.1 christos
445 1.1 christos static int
446 1.1 christos due_sooner(void *a, void *b) {
447 1.1 christos evTimer *a_timer, *b_timer;
448 1.1 christos
449 1.1 christos a_timer = a;
450 1.1 christos b_timer = b;
451 1.1 christos return (evCmpTime(a_timer->due, b_timer->due) < 0);
452 1.1 christos }
453 1.1 christos
454 1.1 christos static void
455 1.2 christos set_index(void *what, int idx) {
456 1.1 christos evTimer *timer;
457 1.1 christos
458 1.1 christos timer = what;
459 1.2 christos timer->index = idx;
460 1.1 christos }
461 1.1 christos
462 1.1 christos static void
463 1.1 christos free_timer(void *what, void *uap) {
464 1.1 christos evTimer *t = what;
465 1.1 christos
466 1.1 christos UNUSED(uap);
467 1.1 christos
468 1.1 christos FREE(t);
469 1.1 christos }
470 1.1 christos
471 1.1 christos static void
472 1.1 christos print_timer(void *what, void *uap) {
473 1.1 christos evTimer *cur = what;
474 1.1 christos evContext_p *ctx = uap;
475 1.1 christos
476 1.1 christos cur = what;
477 1.1 christos evPrintf(ctx, 7,
478 1.1 christos " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
479 1.1 christos cur->func, cur->uap,
480 1.1 christos (long)cur->due.tv_sec, cur->due.tv_nsec,
481 1.1 christos (long)cur->inter.tv_sec, cur->inter.tv_nsec);
482 1.1 christos }
483 1.1 christos
484 1.1 christos static void
485 1.1 christos idle_timeout(evContext opaqueCtx,
486 1.1 christos void *uap,
487 1.1 christos struct timespec due,
488 1.1 christos struct timespec inter
489 1.1 christos ) {
490 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
491 1.1 christos idle_timer *this = uap;
492 1.1 christos struct timespec idle;
493 1.1 christos
494 1.1 christos UNUSED(due);
495 1.1 christos UNUSED(inter);
496 1.1 christos
497 1.1 christos idle = evSubTime(ctx->lastEventTime, this->lastTouched);
498 1.1 christos if (evCmpTime(idle, this->max_idle) >= 0) {
499 1.1 christos (this->func)(opaqueCtx, this->uap, this->timer->due,
500 1.1 christos this->max_idle);
501 1.1 christos /*
502 1.1 christos * Setting the interval to zero will cause the timer to
503 1.1 christos * be cleaned up in evDrop().
504 1.1 christos */
505 1.2 christos this->timer->inter = evConsTime(0L, 0L);
506 1.1 christos FREE(this);
507 1.1 christos } else {
508 1.1 christos /* evDrop() will reschedule the timer. */
509 1.1 christos this->timer->inter = evSubTime(this->max_idle, idle);
510 1.1 christos }
511 1.1 christos }
512 1.2 christos #endif
513 1.3 christos
514 1.3 christos /*! \file */
515