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