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.2.2.1.4.5 2004/03/17 02:39:13 marka Exp $";
28 #include "port_before.h"
30 #include "fd_setsize.h"
36 #include <isc/assertions.h>
38 #include "isc/eventlib.h"
39 #include "eventlib_p.h"
41 #include "port_after.h"
45 #define MILLION 1000000
46 #define BILLION 1000000000
51 static int __evOptMonoTime;
53 static int due_sooner(void *, void *);
54 static void set_index(void *, int);
55 static void free_timer(void *, void *);
56 static void print_timer(void *, void *);
57 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
64 struct timespec lastTouched;
65 struct timespec max_idle;
72 evConsTime(time_t sec, long nsec) {
81 evAddTime(struct timespec addend1, struct timespec addend2) {
84 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
85 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
86 if (x.tv_nsec >= BILLION) {
94 evSubTime(struct timespec minuend, struct timespec subtrahend) {
97 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
98 if (minuend.tv_nsec >= subtrahend.tv_nsec)
99 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
101 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
108 evCmpTime(struct timespec a, struct timespec b) {
109 long x = a.tv_sec - b.tv_sec;
112 x = a.tv_nsec - b.tv_nsec;
113 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
119 #ifdef CLOCK_REALTIME
120 struct timespec tsnow;
121 int m = CLOCK_REALTIME;
123 #ifdef CLOCK_MONOTONIC
127 if (clock_gettime(m, &tsnow) == 0)
130 if (gettimeofday(&now, NULL) < 0)
131 return (evConsTime(0, 0));
132 return (evTimeSpec(now));
138 #ifdef CLOCK_REALTIME
139 struct timespec tsnow;
140 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
143 if (gettimeofday(&now, NULL) < 0)
144 return (evConsTime(0, 0));
145 return (evTimeSpec(now));
150 evLastEventTime(evContext opaqueCtx) {
151 evContext_p *ctx = opaqueCtx.opaque;
153 return (ctx->lastEventTime);
158 evTimeSpec(struct timeval tv) {
161 ts.tv_sec = tv.tv_sec;
162 ts.tv_nsec = tv.tv_usec * 1000;
166 #if !defined(USE_KQUEUE) || !defined(_LIBC)
168 evTimeVal(struct timespec ts) {
171 tv.tv_sec = ts.tv_sec;
172 tv.tv_usec = ts.tv_nsec / 1000;
179 evSetTimer(evContext opaqueCtx,
183 struct timespec inter,
186 evContext_p *ctx = opaqueCtx.opaque;
190 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
192 (long)due.tv_sec, due.tv_nsec,
193 (long)inter.tv_sec, inter.tv_nsec);
197 * tv_sec and tv_nsec are unsigned.
199 if (due.tv_nsec >= BILLION)
202 if (inter.tv_nsec >= BILLION)
205 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
208 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
212 /* due={0,0} is a magic cookie meaning "now." */
213 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
216 /* Allocate and fill. */
223 if (heap_insert(ctx->timers, id) < 0)
226 /* Remember the ID if the caller provided us a place for it. */
228 opaqueID->opaque = id;
230 if (ctx->debug > 7) {
231 evPrintf(ctx, 7, "timers after evSetTimer:\n");
232 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
239 evClearTimer(evContext opaqueCtx, evTimerID id) {
240 evContext_p *ctx = opaqueCtx.opaque;
241 evTimer *del = id.opaque;
243 if (ctx->cur != NULL &&
244 ctx->cur->type == Timer &&
245 ctx->cur->u.timer.this == del) {
246 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
248 * Setting the interval to zero ensures that evDrop() will
249 * clean up the timer.
251 del->inter = evConsTime(0, 0);
255 if (heap_element(ctx->timers, del->index) != del)
258 if (heap_delete(ctx->timers, del->index) < 0)
262 if (ctx->debug > 7) {
263 evPrintf(ctx, 7, "timers after evClearTimer:\n");
264 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
271 evConfigTimer(evContext opaqueCtx,
276 evContext_p *ctx = opaqueCtx.opaque;
277 evTimer *timer = id.opaque;
282 if (heap_element(ctx->timers, timer->index) != timer)
285 if (strcmp(param, "rate") == 0)
286 timer->mode |= EV_TMR_RATE;
287 else if (strcmp(param, "interval") == 0)
288 timer->mode &= ~EV_TMR_RATE;
296 evResetTimer(evContext opaqueCtx,
301 struct timespec inter
303 evContext_p *ctx = opaqueCtx.opaque;
304 evTimer *timer = id.opaque;
305 struct timespec old_due;
308 if (heap_element(ctx->timers, timer->index) != timer)
313 * tv_sec and tv_nsec are unsigned.
315 if (due.tv_nsec >= BILLION)
318 if (inter.tv_nsec >= BILLION)
321 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
324 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
328 old_due = timer->due;
333 timer->inter = inter;
335 switch (evCmpTime(due, old_due)) {
337 result = heap_increased(ctx->timers, timer->index);
343 result = heap_decreased(ctx->timers, timer->index);
347 if (ctx->debug > 7) {
348 evPrintf(ctx, 7, "timers after evResetTimer:\n");
349 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
356 evSetIdleTimer(evContext opaqueCtx,
359 struct timespec max_idle,
362 evContext_p *ctx = opaqueCtx.opaque;
365 /* Allocate and fill. */
369 tt->lastTouched = ctx->lastEventTime;
370 tt->max_idle = max_idle;
372 if (evSetTimer(opaqueCtx, idle_timeout, tt,
373 evAddTime(ctx->lastEventTime, max_idle),
374 max_idle, opaqueID) < 0) {
379 tt->timer = opaqueID->opaque;
385 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
386 evTimer *del = id.opaque;
387 idle_timer *tt = del->uap;
390 return (evClearTimer(opaqueCtx, id));
394 evResetIdleTimer(evContext opaqueCtx,
398 struct timespec max_idle
400 evContext_p *ctx = opaqueCtx.opaque;
401 evTimer *timer = opaqueID.opaque;
402 idle_timer *tt = timer->uap;
406 tt->lastTouched = ctx->lastEventTime;
407 tt->max_idle = max_idle;
409 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
410 evAddTime(ctx->lastEventTime, max_idle),
415 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
416 evContext_p *ctx = opaqueCtx.opaque;
417 evTimer *t = id.opaque;
418 idle_timer *tt = t->uap;
420 tt->lastTouched = ctx->lastEventTime;
425 /* Public to the rest of eventlib. */
428 evCreateTimers(const evContext_p *ctx) {
432 return (heap_new(due_sooner, set_index, 2048));
436 evDestroyTimers(const evContext_p *ctx) {
437 (void) heap_for_each(ctx->timers, free_timer, NULL);
438 (void) heap_free(ctx->timers);
444 due_sooner(void *a, void *b) {
445 evTimer *a_timer, *b_timer;
449 return (evCmpTime(a_timer->due, b_timer->due) < 0);
453 set_index(void *what, int index) {
457 timer->index = index;
461 free_timer(void *what, void *uap) {
470 print_timer(void *what, void *uap) {
472 evContext_p *ctx = uap;
476 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
478 (long)cur->due.tv_sec, cur->due.tv_nsec,
479 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
483 idle_timeout(evContext opaqueCtx,
486 struct timespec inter
488 evContext_p *ctx = opaqueCtx.opaque;
489 idle_timer *this = uap;
490 struct timespec idle;
495 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
496 if (evCmpTime(idle, this->max_idle) >= 0) {
497 (this->func)(opaqueCtx, this->uap, this->timer->due,
500 * Setting the interval to zero will cause the timer to
501 * be cleaned up in evDrop().
503 this->timer->inter = evConsTime(0, 0);
506 /* evDrop() will reschedule the timer. */
507 this->timer->inter = evSubTime(this->max_idle, idle);