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]
26 #include "port_before.h"
28 #include "fd_setsize.h"
34 #include <isc/assertions.h>
36 #include <isc/eventlib.h>
37 #include "eventlib_p.h"
39 #include "port_after.h"
43 #define MILLION 1000000
44 #define BILLION 1000000000
49 static int __evOptMonoTime;
51 static int due_sooner(void *, void *);
52 static void set_index(void *, int);
53 static void free_timer(void *, void *);
54 static void print_timer(void *, void *);
55 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
62 struct timespec lastTouched;
63 struct timespec max_idle;
71 evConsTime(time_t sec, long nsec) {
80 evAddTime(struct timespec addend1, struct timespec addend2) {
83 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
84 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
85 if (x.tv_nsec >= BILLION) {
93 evSubTime(struct timespec minuend, struct timespec subtrahend) {
96 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
97 if (minuend.tv_nsec >= subtrahend.tv_nsec)
98 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
100 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
107 evCmpTime(struct timespec a, struct timespec b) {
108 long x = a.tv_sec - b.tv_sec;
111 x = a.tv_nsec - b.tv_nsec;
112 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
118 #ifdef CLOCK_REALTIME
119 struct timespec tsnow;
120 int m = CLOCK_REALTIME;
122 #ifdef CLOCK_MONOTONIC
126 if (clock_gettime(m, &tsnow) == 0)
129 if (gettimeofday(&now, NULL) < 0)
130 return (evConsTime(0, 0));
131 return (evTimeSpec(now));
137 #ifdef CLOCK_REALTIME
138 struct timespec tsnow;
139 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
142 if (gettimeofday(&now, NULL) < 0)
143 return (evConsTime(0, 0));
144 return (evTimeSpec(now));
149 evLastEventTime(evContext opaqueCtx) {
150 evContext_p *ctx = opaqueCtx.opaque;
152 return (ctx->lastEventTime);
157 evTimeSpec(struct timeval tv) {
160 ts.tv_sec = tv.tv_sec;
161 ts.tv_nsec = tv.tv_usec * 1000;
165 #if !defined(USE_KQUEUE) || !defined(_LIBC)
167 evTimeVal(struct timespec ts) {
170 tv.tv_sec = ts.tv_sec;
171 tv.tv_usec = ts.tv_nsec / 1000;
178 evSetTimer(evContext opaqueCtx,
182 struct timespec inter,
185 evContext_p *ctx = opaqueCtx.opaque;
189 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
191 (long)due.tv_sec, due.tv_nsec,
192 (long)inter.tv_sec, inter.tv_nsec);
196 * tv_sec and tv_nsec are unsigned.
198 if (due.tv_nsec >= BILLION)
201 if (inter.tv_nsec >= BILLION)
204 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
207 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
211 /* due={0,0} is a magic cookie meaning "now." */
212 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
215 /* Allocate and fill. */
222 if (heap_insert(ctx->timers, id) < 0)
225 /* Remember the ID if the caller provided us a place for it. */
227 opaqueID->opaque = id;
229 if (ctx->debug > 7) {
230 evPrintf(ctx, 7, "timers after evSetTimer:\n");
231 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
238 evClearTimer(evContext opaqueCtx, evTimerID id) {
239 evContext_p *ctx = opaqueCtx.opaque;
240 evTimer *del = id.opaque;
242 if (ctx->cur != NULL &&
243 ctx->cur->type == Timer &&
244 ctx->cur->u.timer.this == del) {
245 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
247 * Setting the interval to zero ensures that evDrop() will
248 * clean up the timer.
250 del->inter = evConsTime(0, 0);
254 if (heap_element(ctx->timers, del->index) != del)
257 if (heap_delete(ctx->timers, del->index) < 0)
261 if (ctx->debug > 7) {
262 evPrintf(ctx, 7, "timers after evClearTimer:\n");
263 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
270 evConfigTimer(evContext opaqueCtx,
275 evContext_p *ctx = opaqueCtx.opaque;
276 evTimer *timer = id.opaque;
281 if (heap_element(ctx->timers, timer->index) != timer)
284 if (strcmp(param, "rate") == 0)
285 timer->mode |= EV_TMR_RATE;
286 else if (strcmp(param, "interval") == 0)
287 timer->mode &= ~EV_TMR_RATE;
295 evResetTimer(evContext opaqueCtx,
300 struct timespec inter
302 evContext_p *ctx = opaqueCtx.opaque;
303 evTimer *timer = id.opaque;
304 struct timespec old_due;
307 if (heap_element(ctx->timers, timer->index) != timer)
312 * tv_sec and tv_nsec are unsigned.
314 if (due.tv_nsec >= BILLION)
317 if (inter.tv_nsec >= BILLION)
320 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
323 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
327 old_due = timer->due;
332 timer->inter = inter;
334 switch (evCmpTime(due, old_due)) {
336 result = heap_increased(ctx->timers, timer->index);
342 result = heap_decreased(ctx->timers, timer->index);
346 if (ctx->debug > 7) {
347 evPrintf(ctx, 7, "timers after evResetTimer:\n");
348 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
355 evSetIdleTimer(evContext opaqueCtx,
358 struct timespec max_idle,
361 evContext_p *ctx = opaqueCtx.opaque;
364 /* Allocate and fill. */
368 tt->lastTouched = ctx->lastEventTime;
369 tt->max_idle = max_idle;
371 if (evSetTimer(opaqueCtx, idle_timeout, tt,
372 evAddTime(ctx->lastEventTime, max_idle),
373 max_idle, opaqueID) < 0) {
378 tt->timer = opaqueID->opaque;
384 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
385 evTimer *del = id.opaque;
386 idle_timer *tt = del->uap;
389 return (evClearTimer(opaqueCtx, id));
393 evResetIdleTimer(evContext opaqueCtx,
397 struct timespec max_idle
399 evContext_p *ctx = opaqueCtx.opaque;
400 evTimer *timer = opaqueID.opaque;
401 idle_timer *tt = timer->uap;
405 tt->lastTouched = ctx->lastEventTime;
406 tt->max_idle = max_idle;
408 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
409 evAddTime(ctx->lastEventTime, max_idle),
414 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
415 evContext_p *ctx = opaqueCtx.opaque;
416 evTimer *t = id.opaque;
417 idle_timer *tt = t->uap;
419 tt->lastTouched = ctx->lastEventTime;
424 /* Public to the rest of eventlib. */
427 evCreateTimers(const evContext_p *ctx) {
431 return (heap_new(due_sooner, set_index, 2048));
435 evDestroyTimers(const evContext_p *ctx) {
436 (void) heap_for_each(ctx->timers, free_timer, NULL);
437 (void) heap_free(ctx->timers);
443 due_sooner(void *a, void *b) {
444 evTimer *a_timer, *b_timer;
448 return (evCmpTime(a_timer->due, b_timer->due) < 0);
452 set_index(void *what, int index) {
456 timer->index = index;
460 free_timer(void *what, void *uap) {
469 print_timer(void *what, void *uap) {
471 evContext_p *ctx = uap;
475 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
477 (long)cur->due.tv_sec, cur->due.tv_nsec,
478 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
482 idle_timeout(evContext opaqueCtx,
485 struct timespec inter
487 evContext_p *ctx = opaqueCtx.opaque;
488 idle_timer *this = uap;
489 struct timespec idle;
494 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
495 if (evCmpTime(idle, this->max_idle) >= 0) {
496 (this->func)(opaqueCtx, this->uap, this->timer->due,
499 * Setting the interval to zero will cause the timer to
500 * be cleaned up in evDrop().
502 this->timer->inter = evConsTime(0, 0);
505 /* evDrop() will reschedule the timer. */
506 this->timer->inter = evSubTime(this->max_idle, idle);