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 $";
29 #include "port_before.h"
31 #include "fd_setsize.h"
37 #include <isc/assertions.h>
39 #include <isc/eventlib.h>
40 #include "eventlib_p.h"
42 #include "port_after.h"
46 #define MILLION 1000000
47 #define BILLION 1000000000
52 static int __evOptMonoTime;
54 static int due_sooner(void *, void *);
55 static void set_index(void *, int);
56 static void free_timer(void *, void *);
57 static void print_timer(void *, void *);
58 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
65 struct timespec lastTouched;
66 struct timespec max_idle;
74 evConsTime(time_t sec, long nsec) {
83 evAddTime(struct timespec addend1, struct timespec addend2) {
86 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
87 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
88 if (x.tv_nsec >= BILLION) {
96 evSubTime(struct timespec minuend, struct timespec subtrahend) {
99 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
100 if (minuend.tv_nsec >= subtrahend.tv_nsec)
101 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
103 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
110 evCmpTime(struct timespec a, struct timespec b) {
111 long x = a.tv_sec - b.tv_sec;
114 x = a.tv_nsec - b.tv_nsec;
115 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
121 #ifdef CLOCK_REALTIME
122 struct timespec tsnow;
123 int m = CLOCK_REALTIME;
125 #ifdef CLOCK_MONOTONIC
129 if (clock_gettime(m, &tsnow) == 0)
132 if (gettimeofday(&now, NULL) < 0)
133 return (evConsTime(0, 0));
134 return (evTimeSpec(now));
140 #ifdef CLOCK_REALTIME
141 struct timespec tsnow;
142 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
145 if (gettimeofday(&now, NULL) < 0)
146 return (evConsTime(0, 0));
147 return (evTimeSpec(now));
152 evLastEventTime(evContext opaqueCtx) {
153 evContext_p *ctx = opaqueCtx.opaque;
155 return (ctx->lastEventTime);
160 evTimeSpec(struct timeval tv) {
163 ts.tv_sec = tv.tv_sec;
164 ts.tv_nsec = tv.tv_usec * 1000;
168 #if !defined(USE_KQUEUE) || !defined(_LIBC)
170 evTimeVal(struct timespec ts) {
173 tv.tv_sec = ts.tv_sec;
174 tv.tv_usec = ts.tv_nsec / 1000;
181 evSetTimer(evContext opaqueCtx,
185 struct timespec inter,
188 evContext_p *ctx = opaqueCtx.opaque;
192 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
194 (long)due.tv_sec, due.tv_nsec,
195 (long)inter.tv_sec, inter.tv_nsec);
199 * tv_sec and tv_nsec are unsigned.
201 if (due.tv_nsec >= BILLION)
204 if (inter.tv_nsec >= BILLION)
207 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
210 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
214 /* due={0,0} is a magic cookie meaning "now." */
215 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
218 /* Allocate and fill. */
225 if (heap_insert(ctx->timers, id) < 0)
228 /* Remember the ID if the caller provided us a place for it. */
230 opaqueID->opaque = id;
232 if (ctx->debug > 7) {
233 evPrintf(ctx, 7, "timers after evSetTimer:\n");
234 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
241 evClearTimer(evContext opaqueCtx, evTimerID id) {
242 evContext_p *ctx = opaqueCtx.opaque;
243 evTimer *del = id.opaque;
245 if (ctx->cur != NULL &&
246 ctx->cur->type == Timer &&
247 ctx->cur->u.timer.this == del) {
248 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
250 * Setting the interval to zero ensures that evDrop() will
251 * clean up the timer.
253 del->inter = evConsTime(0, 0);
257 if (heap_element(ctx->timers, del->index) != del)
260 if (heap_delete(ctx->timers, del->index) < 0)
264 if (ctx->debug > 7) {
265 evPrintf(ctx, 7, "timers after evClearTimer:\n");
266 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
273 evConfigTimer(evContext opaqueCtx,
278 evContext_p *ctx = opaqueCtx.opaque;
279 evTimer *timer = id.opaque;
284 if (heap_element(ctx->timers, timer->index) != timer)
287 if (strcmp(param, "rate") == 0)
288 timer->mode |= EV_TMR_RATE;
289 else if (strcmp(param, "interval") == 0)
290 timer->mode &= ~EV_TMR_RATE;
298 evResetTimer(evContext opaqueCtx,
303 struct timespec inter
305 evContext_p *ctx = opaqueCtx.opaque;
306 evTimer *timer = id.opaque;
307 struct timespec old_due;
310 if (heap_element(ctx->timers, timer->index) != timer)
315 * tv_sec and tv_nsec are unsigned.
317 if (due.tv_nsec >= BILLION)
320 if (inter.tv_nsec >= BILLION)
323 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
326 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
330 old_due = timer->due;
335 timer->inter = inter;
337 switch (evCmpTime(due, old_due)) {
339 result = heap_increased(ctx->timers, timer->index);
345 result = heap_decreased(ctx->timers, timer->index);
349 if (ctx->debug > 7) {
350 evPrintf(ctx, 7, "timers after evResetTimer:\n");
351 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
358 evSetIdleTimer(evContext opaqueCtx,
361 struct timespec max_idle,
364 evContext_p *ctx = opaqueCtx.opaque;
367 /* Allocate and fill. */
371 tt->lastTouched = ctx->lastEventTime;
372 tt->max_idle = max_idle;
374 if (evSetTimer(opaqueCtx, idle_timeout, tt,
375 evAddTime(ctx->lastEventTime, max_idle),
376 max_idle, opaqueID) < 0) {
381 tt->timer = opaqueID->opaque;
387 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
388 evTimer *del = id.opaque;
389 idle_timer *tt = del->uap;
392 return (evClearTimer(opaqueCtx, id));
396 evResetIdleTimer(evContext opaqueCtx,
400 struct timespec max_idle
402 evContext_p *ctx = opaqueCtx.opaque;
403 evTimer *timer = opaqueID.opaque;
404 idle_timer *tt = timer->uap;
408 tt->lastTouched = ctx->lastEventTime;
409 tt->max_idle = max_idle;
411 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
412 evAddTime(ctx->lastEventTime, max_idle),
417 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
418 evContext_p *ctx = opaqueCtx.opaque;
419 evTimer *t = id.opaque;
420 idle_timer *tt = t->uap;
422 tt->lastTouched = ctx->lastEventTime;
427 /* Public to the rest of eventlib. */
430 evCreateTimers(const evContext_p *ctx) {
434 return (heap_new(due_sooner, set_index, 2048));
438 evDestroyTimers(const evContext_p *ctx) {
439 (void) heap_for_each(ctx->timers, free_timer, NULL);
440 (void) heap_free(ctx->timers);
446 due_sooner(void *a, void *b) {
447 evTimer *a_timer, *b_timer;
451 return (evCmpTime(a_timer->due, b_timer->due) < 0);
455 set_index(void *what, int index) {
459 timer->index = index;
463 free_timer(void *what, void *uap) {
472 print_timer(void *what, void *uap) {
474 evContext_p *ctx = uap;
478 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
480 (long)cur->due.tv_sec, cur->due.tv_nsec,
481 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
485 idle_timeout(evContext opaqueCtx,
488 struct timespec inter
490 evContext_p *ctx = opaqueCtx.opaque;
491 idle_timer *this = uap;
492 struct timespec idle;
497 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
498 if (evCmpTime(idle, this->max_idle) >= 0) {
499 (this->func)(opaqueCtx, this->uap, this->timer->due,
502 * Setting the interval to zero will cause the timer to
503 * be cleaned up in evDrop().
505 this->timer->inter = evConsTime(0, 0);
508 /* evDrop() will reschedule the timer. */
509 this->timer->inter = evSubTime(this->max_idle, idle);