2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* ev_timers.c - implement timers for the eventlib
21 * vix 09sep95 [initial]
24 #if !defined(LINT) && !defined(CODECENTER)
25 static const char rcsid[] = "$Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp $";
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include "port_before.h"
34 #include "fd_setsize.h"
40 #include <isc/assertions.h>
42 #include <isc/eventlib.h>
43 #include "eventlib_p.h"
45 #include "port_after.h"
49 #define MILLION 1000000
50 #define BILLION 1000000000
55 static int __evOptMonoTime;
57 static int due_sooner(void *, void *);
58 static void set_index(void *, int);
59 static void free_timer(void *, void *);
60 static void print_timer(void *, void *);
61 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
68 struct timespec lastTouched;
69 struct timespec max_idle;
77 evConsTime(time_t sec, long nsec) {
86 evAddTime(struct timespec addend1, struct timespec addend2) {
89 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
90 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
91 if (x.tv_nsec >= BILLION) {
99 evSubTime(struct timespec minuend, struct timespec subtrahend) {
102 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
103 if (minuend.tv_nsec >= subtrahend.tv_nsec)
104 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
106 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
113 evCmpTime(struct timespec a, struct timespec b) {
114 long x = a.tv_sec - b.tv_sec;
117 x = a.tv_nsec - b.tv_nsec;
118 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
124 #ifdef CLOCK_REALTIME
125 struct timespec tsnow;
126 int m = CLOCK_REALTIME;
128 #ifdef CLOCK_MONOTONIC
132 if (clock_gettime(m, &tsnow) == 0)
135 if (gettimeofday(&now, NULL) < 0)
136 return (evConsTime(0, 0));
137 return (evTimeSpec(now));
143 #ifdef CLOCK_REALTIME
144 struct timespec tsnow;
145 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
148 if (gettimeofday(&now, NULL) < 0)
149 return (evConsTime(0, 0));
150 return (evTimeSpec(now));
155 evLastEventTime(evContext opaqueCtx) {
156 evContext_p *ctx = opaqueCtx.opaque;
158 return (ctx->lastEventTime);
163 evTimeSpec(struct timeval tv) {
166 ts.tv_sec = tv.tv_sec;
167 ts.tv_nsec = tv.tv_usec * 1000;
171 #if !defined(USE_KQUEUE) || !defined(_LIBC)
173 evTimeVal(struct timespec ts) {
176 tv.tv_sec = ts.tv_sec;
177 tv.tv_usec = ts.tv_nsec / 1000;
184 evSetTimer(evContext opaqueCtx,
188 struct timespec inter,
191 evContext_p *ctx = opaqueCtx.opaque;
195 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
197 (long)due.tv_sec, due.tv_nsec,
198 (long)inter.tv_sec, inter.tv_nsec);
202 * tv_sec and tv_nsec are unsigned.
204 if (due.tv_nsec >= BILLION)
207 if (inter.tv_nsec >= BILLION)
210 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
213 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
217 /* due={0,0} is a magic cookie meaning "now." */
218 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
221 /* Allocate and fill. */
228 if (heap_insert(ctx->timers, id) < 0)
231 /* Remember the ID if the caller provided us a place for it. */
233 opaqueID->opaque = id;
235 if (ctx->debug > 7) {
236 evPrintf(ctx, 7, "timers after evSetTimer:\n");
237 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
244 evClearTimer(evContext opaqueCtx, evTimerID id) {
245 evContext_p *ctx = opaqueCtx.opaque;
246 evTimer *del = id.opaque;
248 if (ctx->cur != NULL &&
249 ctx->cur->type == Timer &&
250 ctx->cur->u.timer.this == del) {
251 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
253 * Setting the interval to zero ensures that evDrop() will
254 * clean up the timer.
256 del->inter = evConsTime(0, 0);
260 if (heap_element(ctx->timers, del->index) != del)
263 if (heap_delete(ctx->timers, del->index) < 0)
267 if (ctx->debug > 7) {
268 evPrintf(ctx, 7, "timers after evClearTimer:\n");
269 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
276 evConfigTimer(evContext opaqueCtx,
281 evContext_p *ctx = opaqueCtx.opaque;
282 evTimer *timer = id.opaque;
287 if (heap_element(ctx->timers, timer->index) != timer)
290 if (strcmp(param, "rate") == 0)
291 timer->mode |= EV_TMR_RATE;
292 else if (strcmp(param, "interval") == 0)
293 timer->mode &= ~EV_TMR_RATE;
301 evResetTimer(evContext opaqueCtx,
306 struct timespec inter
308 evContext_p *ctx = opaqueCtx.opaque;
309 evTimer *timer = id.opaque;
310 struct timespec old_due;
313 if (heap_element(ctx->timers, timer->index) != timer)
318 * tv_sec and tv_nsec are unsigned.
320 if (due.tv_nsec >= BILLION)
323 if (inter.tv_nsec >= BILLION)
326 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
329 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
333 old_due = timer->due;
338 timer->inter = inter;
340 switch (evCmpTime(due, old_due)) {
342 result = heap_increased(ctx->timers, timer->index);
348 result = heap_decreased(ctx->timers, timer->index);
352 if (ctx->debug > 7) {
353 evPrintf(ctx, 7, "timers after evResetTimer:\n");
354 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
361 evSetIdleTimer(evContext opaqueCtx,
364 struct timespec max_idle,
367 evContext_p *ctx = opaqueCtx.opaque;
370 /* Allocate and fill. */
374 tt->lastTouched = ctx->lastEventTime;
375 tt->max_idle = max_idle;
377 if (evSetTimer(opaqueCtx, idle_timeout, tt,
378 evAddTime(ctx->lastEventTime, max_idle),
379 max_idle, opaqueID) < 0) {
384 tt->timer = opaqueID->opaque;
390 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
391 evTimer *del = id.opaque;
392 idle_timer *tt = del->uap;
395 return (evClearTimer(opaqueCtx, id));
399 evResetIdleTimer(evContext opaqueCtx,
403 struct timespec max_idle
405 evContext_p *ctx = opaqueCtx.opaque;
406 evTimer *timer = opaqueID.opaque;
407 idle_timer *tt = timer->uap;
411 tt->lastTouched = ctx->lastEventTime;
412 tt->max_idle = max_idle;
414 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
415 evAddTime(ctx->lastEventTime, max_idle),
420 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
421 evContext_p *ctx = opaqueCtx.opaque;
422 evTimer *t = id.opaque;
423 idle_timer *tt = t->uap;
425 tt->lastTouched = ctx->lastEventTime;
430 /* Public to the rest of eventlib. */
433 evCreateTimers(const evContext_p *ctx) {
437 return (heap_new(due_sooner, set_index, 2048));
441 evDestroyTimers(const evContext_p *ctx) {
442 (void) heap_for_each(ctx->timers, free_timer, NULL);
443 (void) heap_free(ctx->timers);
449 due_sooner(void *a, void *b) {
450 evTimer *a_timer, *b_timer;
454 return (evCmpTime(a_timer->due, b_timer->due) < 0);
458 set_index(void *what, int index) {
462 timer->index = index;
466 free_timer(void *what, void *uap) {
475 print_timer(void *what, void *uap) {
477 evContext_p *ctx = uap;
481 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
483 (long)cur->due.tv_sec, cur->due.tv_nsec,
484 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
488 idle_timeout(evContext opaqueCtx,
491 struct timespec inter
493 evContext_p *ctx = opaqueCtx.opaque;
494 idle_timer *this = uap;
495 struct timespec idle;
500 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
501 if (evCmpTime(idle, this->max_idle) >= 0) {
502 (this->func)(opaqueCtx, this->uap, this->timer->due,
505 * Setting the interval to zero will cause the timer to
506 * be cleaned up in evDrop().
508 this->timer->inter = evConsTime(0, 0);
511 /* evDrop() will reschedule the timer. */
512 this->timer->inter = evSubTime(this->max_idle, idle);