2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* ev_timers.c - implement timers for the eventlib
19 * vix 09sep95 [initial]
22 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid[] = "$Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp $";
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
30 #include "port_before.h"
32 #include "fd_setsize.h"
38 #include <isc/assertions.h>
40 #include <isc/eventlib.h>
41 #include "eventlib_p.h"
43 #include "port_after.h"
47 #define MILLION 1000000
48 #define BILLION 1000000000
53 static int __evOptMonoTime;
55 static int due_sooner(void *, void *);
56 static void set_index(void *, int);
57 static void free_timer(void *, void *);
58 static void print_timer(void *, void *);
59 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
66 struct timespec lastTouched;
67 struct timespec max_idle;
75 evConsTime(time_t sec, long nsec) {
84 evAddTime(struct timespec addend1, struct timespec addend2) {
87 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
88 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
89 if (x.tv_nsec >= BILLION) {
97 evSubTime(struct timespec minuend, struct timespec subtrahend) {
100 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
101 if (minuend.tv_nsec >= subtrahend.tv_nsec)
102 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
104 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
111 evCmpTime(struct timespec a, struct timespec b) {
112 long x = a.tv_sec - b.tv_sec;
115 x = a.tv_nsec - b.tv_nsec;
116 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
122 #ifdef CLOCK_REALTIME
123 struct timespec tsnow;
124 int m = CLOCK_REALTIME;
126 #ifdef CLOCK_MONOTONIC
130 if (clock_gettime(m, &tsnow) == 0)
133 if (gettimeofday(&now, NULL) < 0)
134 return (evConsTime(0, 0));
135 return (evTimeSpec(now));
141 #ifdef CLOCK_REALTIME
142 struct timespec tsnow;
143 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
146 if (gettimeofday(&now, NULL) < 0)
147 return (evConsTime(0, 0));
148 return (evTimeSpec(now));
153 evLastEventTime(evContext opaqueCtx) {
154 evContext_p *ctx = opaqueCtx.opaque;
156 return (ctx->lastEventTime);
161 evTimeSpec(struct timeval tv) {
164 ts.tv_sec = tv.tv_sec;
165 ts.tv_nsec = tv.tv_usec * 1000;
169 #if !defined(USE_KQUEUE) || !defined(_LIBC)
171 evTimeVal(struct timespec ts) {
174 tv.tv_sec = ts.tv_sec;
175 tv.tv_usec = ts.tv_nsec / 1000;
182 evSetTimer(evContext opaqueCtx,
186 struct timespec inter,
189 evContext_p *ctx = opaqueCtx.opaque;
193 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
195 (long)due.tv_sec, due.tv_nsec,
196 (long)inter.tv_sec, inter.tv_nsec);
200 * tv_sec and tv_nsec are unsigned.
202 if (due.tv_nsec >= BILLION)
205 if (inter.tv_nsec >= BILLION)
208 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
211 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
215 /* due={0,0} is a magic cookie meaning "now." */
216 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
219 /* Allocate and fill. */
226 if (heap_insert(ctx->timers, id) < 0)
229 /* Remember the ID if the caller provided us a place for it. */
231 opaqueID->opaque = id;
233 if (ctx->debug > 7) {
234 evPrintf(ctx, 7, "timers after evSetTimer:\n");
235 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
242 evClearTimer(evContext opaqueCtx, evTimerID id) {
243 evContext_p *ctx = opaqueCtx.opaque;
244 evTimer *del = id.opaque;
246 if (ctx->cur != NULL &&
247 ctx->cur->type == Timer &&
248 ctx->cur->u.timer.this == del) {
249 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
251 * Setting the interval to zero ensures that evDrop() will
252 * clean up the timer.
254 del->inter = evConsTime(0, 0);
258 if (heap_element(ctx->timers, del->index) != del)
261 if (heap_delete(ctx->timers, del->index) < 0)
265 if (ctx->debug > 7) {
266 evPrintf(ctx, 7, "timers after evClearTimer:\n");
267 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
274 evConfigTimer(evContext opaqueCtx,
279 evContext_p *ctx = opaqueCtx.opaque;
280 evTimer *timer = id.opaque;
285 if (heap_element(ctx->timers, timer->index) != timer)
288 if (strcmp(param, "rate") == 0)
289 timer->mode |= EV_TMR_RATE;
290 else if (strcmp(param, "interval") == 0)
291 timer->mode &= ~EV_TMR_RATE;
299 evResetTimer(evContext opaqueCtx,
304 struct timespec inter
306 evContext_p *ctx = opaqueCtx.opaque;
307 evTimer *timer = id.opaque;
308 struct timespec old_due;
311 if (heap_element(ctx->timers, timer->index) != timer)
316 * tv_sec and tv_nsec are unsigned.
318 if (due.tv_nsec >= BILLION)
321 if (inter.tv_nsec >= BILLION)
324 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
327 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
331 old_due = timer->due;
336 timer->inter = inter;
338 switch (evCmpTime(due, old_due)) {
340 result = heap_increased(ctx->timers, timer->index);
346 result = heap_decreased(ctx->timers, timer->index);
350 if (ctx->debug > 7) {
351 evPrintf(ctx, 7, "timers after evResetTimer:\n");
352 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
359 evSetIdleTimer(evContext opaqueCtx,
362 struct timespec max_idle,
365 evContext_p *ctx = opaqueCtx.opaque;
368 /* Allocate and fill. */
372 tt->lastTouched = ctx->lastEventTime;
373 tt->max_idle = max_idle;
375 if (evSetTimer(opaqueCtx, idle_timeout, tt,
376 evAddTime(ctx->lastEventTime, max_idle),
377 max_idle, opaqueID) < 0) {
382 tt->timer = opaqueID->opaque;
388 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
389 evTimer *del = id.opaque;
390 idle_timer *tt = del->uap;
393 return (evClearTimer(opaqueCtx, id));
397 evResetIdleTimer(evContext opaqueCtx,
401 struct timespec max_idle
403 evContext_p *ctx = opaqueCtx.opaque;
404 evTimer *timer = opaqueID.opaque;
405 idle_timer *tt = timer->uap;
409 tt->lastTouched = ctx->lastEventTime;
410 tt->max_idle = max_idle;
412 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
413 evAddTime(ctx->lastEventTime, max_idle),
418 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
419 evContext_p *ctx = opaqueCtx.opaque;
420 evTimer *t = id.opaque;
421 idle_timer *tt = t->uap;
423 tt->lastTouched = ctx->lastEventTime;
428 /* Public to the rest of eventlib. */
431 evCreateTimers(const evContext_p *ctx) {
435 return (heap_new(due_sooner, set_index, 2048));
439 evDestroyTimers(const evContext_p *ctx) {
440 (void) heap_for_each(ctx->timers, free_timer, NULL);
441 (void) heap_free(ctx->timers);
447 due_sooner(void *a, void *b) {
448 evTimer *a_timer, *b_timer;
452 return (evCmpTime(a_timer->due, b_timer->due) < 0);
456 set_index(void *what, int index) {
460 timer->index = index;
464 free_timer(void *what, void *uap) {
473 print_timer(void *what, void *uap) {
475 evContext_p *ctx = uap;
479 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
481 (long)cur->due.tv_sec, cur->due.tv_nsec,
482 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
486 idle_timeout(evContext opaqueCtx,
489 struct timespec inter
491 evContext_p *ctx = opaqueCtx.opaque;
492 idle_timer *this = uap;
493 struct timespec idle;
498 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
499 if (evCmpTime(idle, this->max_idle) >= 0) {
500 (this->func)(opaqueCtx, this->uap, this->timer->due,
503 * Setting the interval to zero will cause the timer to
504 * be cleaned up in evDrop().
506 this->timer->inter = evConsTime(0, 0);
509 /* evDrop() will reschedule the timer. */
510 this->timer->inter = evSubTime(this->max_idle, idle);