1*f14fb602SLionel Sambuc /* $NetBSD: ev_timers.c,v 1.11 2012/03/21 00:34:54 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras * Copyright (c) 1995-1999 by Internet Software Consortium
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this software for any
82fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras *
112fe8fb19SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
122fe8fb19SBen Gras * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
132fe8fb19SBen Gras * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
142fe8fb19SBen Gras * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
152fe8fb19SBen Gras * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
162fe8fb19SBen Gras * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
172fe8fb19SBen Gras * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras */
192fe8fb19SBen Gras
202fe8fb19SBen Gras /* ev_timers.c - implement timers for the eventlib
212fe8fb19SBen Gras * vix 09sep95 [initial]
222fe8fb19SBen Gras */
232fe8fb19SBen Gras
242fe8fb19SBen Gras #include <sys/cdefs.h>
252fe8fb19SBen Gras #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
262fe8fb19SBen Gras #ifdef notdef
272fe8fb19SBen Gras static const char rcsid[] = "Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp";
282fe8fb19SBen Gras #else
29*f14fb602SLionel Sambuc __RCSID("$NetBSD: ev_timers.c,v 1.11 2012/03/21 00:34:54 christos Exp $");
302fe8fb19SBen Gras #endif
312fe8fb19SBen Gras #endif
322fe8fb19SBen Gras
332fe8fb19SBen Gras /* Import. */
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include "port_before.h"
362fe8fb19SBen Gras #include "fd_setsize.h"
372fe8fb19SBen Gras
382fe8fb19SBen Gras #include <errno.h>
392fe8fb19SBen Gras
402fe8fb19SBen Gras #include <isc/assertions.h>
412fe8fb19SBen Gras #include <isc/eventlib.h>
422fe8fb19SBen Gras #include "eventlib_p.h"
432fe8fb19SBen Gras
442fe8fb19SBen Gras #include "port_after.h"
452fe8fb19SBen Gras
462fe8fb19SBen Gras /* Constants. */
472fe8fb19SBen Gras
482fe8fb19SBen Gras #define MILLION 1000000
492fe8fb19SBen Gras #define BILLION 1000000000
502fe8fb19SBen Gras
512fe8fb19SBen Gras /* Forward. */
522fe8fb19SBen Gras
532fe8fb19SBen Gras #ifndef _LIBC
542fe8fb19SBen Gras static int due_sooner(void *, void *);
552fe8fb19SBen Gras static void set_index(void *, int);
562fe8fb19SBen Gras static void free_timer(void *, void *);
572fe8fb19SBen Gras static void print_timer(void *, void *);
582fe8fb19SBen Gras static void idle_timeout(evContext, void *, struct timespec, struct timespec);
592fe8fb19SBen Gras
602fe8fb19SBen Gras /* Private type. */
612fe8fb19SBen Gras
622fe8fb19SBen Gras typedef struct {
632fe8fb19SBen Gras evTimerFunc func;
642fe8fb19SBen Gras void * uap;
652fe8fb19SBen Gras struct timespec lastTouched;
662fe8fb19SBen Gras struct timespec max_idle;
672fe8fb19SBen Gras evTimer * timer;
682fe8fb19SBen Gras } idle_timer;
692fe8fb19SBen Gras #endif
702fe8fb19SBen Gras
712fe8fb19SBen Gras /* Public. */
722fe8fb19SBen Gras
732fe8fb19SBen Gras struct timespec
evConsTime(time_t sec,long nsec)742fe8fb19SBen Gras evConsTime(time_t sec, long nsec) {
752fe8fb19SBen Gras struct timespec x;
762fe8fb19SBen Gras
772fe8fb19SBen Gras x.tv_sec = sec;
782fe8fb19SBen Gras x.tv_nsec = nsec;
792fe8fb19SBen Gras return (x);
802fe8fb19SBen Gras }
812fe8fb19SBen Gras
822fe8fb19SBen Gras struct timespec
evAddTime(struct timespec addend1,struct timespec addend2)832fe8fb19SBen Gras evAddTime(struct timespec addend1, struct timespec addend2) {
842fe8fb19SBen Gras struct timespec x;
852fe8fb19SBen Gras
862fe8fb19SBen Gras x.tv_sec = addend1.tv_sec + addend2.tv_sec;
872fe8fb19SBen Gras x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
882fe8fb19SBen Gras if (x.tv_nsec >= BILLION) {
892fe8fb19SBen Gras x.tv_sec++;
902fe8fb19SBen Gras x.tv_nsec -= BILLION;
912fe8fb19SBen Gras }
922fe8fb19SBen Gras return (x);
932fe8fb19SBen Gras }
942fe8fb19SBen Gras
952fe8fb19SBen Gras struct timespec
evSubTime(struct timespec minuend,struct timespec subtrahend)962fe8fb19SBen Gras evSubTime(struct timespec minuend, struct timespec subtrahend) {
972fe8fb19SBen Gras struct timespec x;
982fe8fb19SBen Gras
992fe8fb19SBen Gras x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
1002fe8fb19SBen Gras if (minuend.tv_nsec >= subtrahend.tv_nsec)
1012fe8fb19SBen Gras x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
1022fe8fb19SBen Gras else {
1032fe8fb19SBen Gras x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
1042fe8fb19SBen Gras x.tv_sec--;
1052fe8fb19SBen Gras }
1062fe8fb19SBen Gras return (x);
1072fe8fb19SBen Gras }
1082fe8fb19SBen Gras
1092fe8fb19SBen Gras int
evCmpTime(struct timespec a,struct timespec b)1102fe8fb19SBen Gras evCmpTime(struct timespec a, struct timespec b) {
1112fe8fb19SBen Gras #define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
1122fe8fb19SBen Gras time_t s = a.tv_sec - b.tv_sec;
1132fe8fb19SBen Gras long n;
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras if (s != 0)
1162fe8fb19SBen Gras return SGN(s);
1172fe8fb19SBen Gras
1182fe8fb19SBen Gras n = a.tv_nsec - b.tv_nsec;
1192fe8fb19SBen Gras return SGN(n);
1202fe8fb19SBen Gras }
1212fe8fb19SBen Gras
1222fe8fb19SBen Gras struct timespec
evNowTime(void)123*f14fb602SLionel Sambuc evNowTime(void)
124*f14fb602SLionel Sambuc {
1252fe8fb19SBen Gras struct timeval now;
1262fe8fb19SBen Gras #ifdef CLOCK_REALTIME
1272fe8fb19SBen Gras struct timespec tsnow;
1282fe8fb19SBen Gras int m = CLOCK_REALTIME;
1292fe8fb19SBen Gras
1302fe8fb19SBen Gras #ifdef CLOCK_MONOTONIC
1312fe8fb19SBen Gras #ifndef _LIBC
1322fe8fb19SBen Gras if (__evOptMonoTime)
1332fe8fb19SBen Gras m = CLOCK_MONOTONIC;
1342fe8fb19SBen Gras #endif
1352fe8fb19SBen Gras #endif
1362fe8fb19SBen Gras if (clock_gettime(m, &tsnow) == 0)
1372fe8fb19SBen Gras return (tsnow);
1382fe8fb19SBen Gras #endif
1392fe8fb19SBen Gras if (gettimeofday(&now, NULL) < 0)
140*f14fb602SLionel Sambuc return (evConsTime((time_t)0, 0L));
1412fe8fb19SBen Gras return (evTimeSpec(now));
1422fe8fb19SBen Gras }
1432fe8fb19SBen Gras
1442fe8fb19SBen Gras struct timespec
evUTCTime(void)1452fe8fb19SBen Gras evUTCTime(void) {
1462fe8fb19SBen Gras struct timeval now;
1472fe8fb19SBen Gras #ifdef CLOCK_REALTIME
1482fe8fb19SBen Gras struct timespec tsnow;
1492fe8fb19SBen Gras if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
1502fe8fb19SBen Gras return (tsnow);
1512fe8fb19SBen Gras #endif
1522fe8fb19SBen Gras if (gettimeofday(&now, NULL) < 0)
153*f14fb602SLionel Sambuc return (evConsTime((time_t)0, 0L));
1542fe8fb19SBen Gras return (evTimeSpec(now));
1552fe8fb19SBen Gras }
1562fe8fb19SBen Gras
1572fe8fb19SBen Gras #ifndef _LIBC
1582fe8fb19SBen Gras struct timespec
evLastEventTime(evContext opaqueCtx)1592fe8fb19SBen Gras evLastEventTime(evContext opaqueCtx) {
1602fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
1612fe8fb19SBen Gras
1622fe8fb19SBen Gras return (ctx->lastEventTime);
1632fe8fb19SBen Gras }
1642fe8fb19SBen Gras #endif
1652fe8fb19SBen Gras
1662fe8fb19SBen Gras struct timespec
evTimeSpec(struct timeval tv)1672fe8fb19SBen Gras evTimeSpec(struct timeval tv) {
1682fe8fb19SBen Gras struct timespec ts;
1692fe8fb19SBen Gras
1702fe8fb19SBen Gras ts.tv_sec = tv.tv_sec;
1712fe8fb19SBen Gras ts.tv_nsec = tv.tv_usec * 1000;
1722fe8fb19SBen Gras return (ts);
1732fe8fb19SBen Gras }
1742fe8fb19SBen Gras
1752fe8fb19SBen Gras struct timeval
evTimeVal(struct timespec ts)1762fe8fb19SBen Gras evTimeVal(struct timespec ts) {
1772fe8fb19SBen Gras struct timeval tv;
1782fe8fb19SBen Gras
1792fe8fb19SBen Gras tv.tv_sec = ts.tv_sec;
180*f14fb602SLionel Sambuc tv.tv_usec = (suseconds_t)(ts.tv_nsec / 1000);
1812fe8fb19SBen Gras return (tv);
1822fe8fb19SBen Gras }
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras #ifndef _LIBC
1852fe8fb19SBen Gras int
evSetTimer(evContext opaqueCtx,evTimerFunc func,void * uap,struct timespec due,struct timespec inter,evTimerID * opaqueID)1862fe8fb19SBen Gras evSetTimer(evContext opaqueCtx,
1872fe8fb19SBen Gras evTimerFunc func,
1882fe8fb19SBen Gras void *uap,
1892fe8fb19SBen Gras struct timespec due,
1902fe8fb19SBen Gras struct timespec inter,
1912fe8fb19SBen Gras evTimerID *opaqueID
1922fe8fb19SBen Gras ) {
1932fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
1942fe8fb19SBen Gras evTimer *id;
1952fe8fb19SBen Gras
1962fe8fb19SBen Gras evPrintf(ctx, 1,
1972fe8fb19SBen Gras "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
1982fe8fb19SBen Gras ctx, func, uap,
1992fe8fb19SBen Gras (long)due.tv_sec, due.tv_nsec,
2002fe8fb19SBen Gras (long)inter.tv_sec, inter.tv_nsec);
2012fe8fb19SBen Gras
2022fe8fb19SBen Gras #ifdef __hpux
2032fe8fb19SBen Gras /*
2042fe8fb19SBen Gras * tv_sec and tv_nsec are unsigned.
2052fe8fb19SBen Gras */
2062fe8fb19SBen Gras if (due.tv_nsec >= BILLION)
2072fe8fb19SBen Gras EV_ERR(EINVAL);
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras if (inter.tv_nsec >= BILLION)
2102fe8fb19SBen Gras EV_ERR(EINVAL);
2112fe8fb19SBen Gras #else
2122fe8fb19SBen Gras if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
2132fe8fb19SBen Gras EV_ERR(EINVAL);
2142fe8fb19SBen Gras
2152fe8fb19SBen Gras if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
2162fe8fb19SBen Gras EV_ERR(EINVAL);
2172fe8fb19SBen Gras #endif
2182fe8fb19SBen Gras
2192fe8fb19SBen Gras /* due={0,0} is a magic cookie meaning "now." */
2202fe8fb19SBen Gras if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
2212fe8fb19SBen Gras due = evNowTime();
2222fe8fb19SBen Gras
2232fe8fb19SBen Gras /* Allocate and fill. */
2242fe8fb19SBen Gras OKNEW(id);
2252fe8fb19SBen Gras id->func = func;
2262fe8fb19SBen Gras id->uap = uap;
2272fe8fb19SBen Gras id->due = due;
2282fe8fb19SBen Gras id->inter = inter;
2292fe8fb19SBen Gras
2302fe8fb19SBen Gras if (heap_insert(ctx->timers, id) < 0)
2312fe8fb19SBen Gras return (-1);
2322fe8fb19SBen Gras
2332fe8fb19SBen Gras /* Remember the ID if the caller provided us a place for it. */
2342fe8fb19SBen Gras if (opaqueID)
2352fe8fb19SBen Gras opaqueID->opaque = id;
2362fe8fb19SBen Gras
2372fe8fb19SBen Gras if (ctx->debug > 7) {
2382fe8fb19SBen Gras evPrintf(ctx, 7, "timers after evSetTimer:\n");
2392fe8fb19SBen Gras (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
2402fe8fb19SBen Gras }
2412fe8fb19SBen Gras
2422fe8fb19SBen Gras return (0);
2432fe8fb19SBen Gras }
2442fe8fb19SBen Gras
2452fe8fb19SBen Gras int
evClearTimer(evContext opaqueCtx,evTimerID id)2462fe8fb19SBen Gras evClearTimer(evContext opaqueCtx, evTimerID id) {
2472fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
2482fe8fb19SBen Gras evTimer *del = id.opaque;
2492fe8fb19SBen Gras
2502fe8fb19SBen Gras if (ctx->cur != NULL &&
2512fe8fb19SBen Gras ctx->cur->type == Timer &&
2522fe8fb19SBen Gras ctx->cur->u.timer.this == del) {
2532fe8fb19SBen Gras evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
2542fe8fb19SBen Gras /*
2552fe8fb19SBen Gras * Setting the interval to zero ensures that evDrop() will
2562fe8fb19SBen Gras * clean up the timer.
2572fe8fb19SBen Gras */
2582fe8fb19SBen Gras del->inter = evConsTime(0, 0);
2592fe8fb19SBen Gras return (0);
2602fe8fb19SBen Gras }
2612fe8fb19SBen Gras
2622fe8fb19SBen Gras if (heap_element(ctx->timers, del->index) != del)
2632fe8fb19SBen Gras EV_ERR(ENOENT);
2642fe8fb19SBen Gras
2652fe8fb19SBen Gras if (heap_delete(ctx->timers, del->index) < 0)
2662fe8fb19SBen Gras return (-1);
2672fe8fb19SBen Gras FREE(del);
2682fe8fb19SBen Gras
2692fe8fb19SBen Gras if (ctx->debug > 7) {
2702fe8fb19SBen Gras evPrintf(ctx, 7, "timers after evClearTimer:\n");
2712fe8fb19SBen Gras (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
2722fe8fb19SBen Gras }
2732fe8fb19SBen Gras
2742fe8fb19SBen Gras return (0);
2752fe8fb19SBen Gras }
2762fe8fb19SBen Gras
2772fe8fb19SBen Gras int
evConfigTimer(evContext opaqueCtx,evTimerID id,const char * param,int value)2782fe8fb19SBen Gras evConfigTimer(evContext opaqueCtx,
2792fe8fb19SBen Gras evTimerID id,
2802fe8fb19SBen Gras const char *param,
2812fe8fb19SBen Gras int value
2822fe8fb19SBen Gras ) {
2832fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
2842fe8fb19SBen Gras evTimer *timer = id.opaque;
2852fe8fb19SBen Gras int result=0;
2862fe8fb19SBen Gras
2872fe8fb19SBen Gras UNUSED(value);
2882fe8fb19SBen Gras
2892fe8fb19SBen Gras if (heap_element(ctx->timers, timer->index) != timer)
2902fe8fb19SBen Gras EV_ERR(ENOENT);
2912fe8fb19SBen Gras
2922fe8fb19SBen Gras if (strcmp(param, "rate") == 0)
2932fe8fb19SBen Gras timer->mode |= EV_TMR_RATE;
2942fe8fb19SBen Gras else if (strcmp(param, "interval") == 0)
2952fe8fb19SBen Gras timer->mode &= ~EV_TMR_RATE;
2962fe8fb19SBen Gras else
2972fe8fb19SBen Gras EV_ERR(EINVAL);
2982fe8fb19SBen Gras
2992fe8fb19SBen Gras return (result);
3002fe8fb19SBen Gras }
3012fe8fb19SBen Gras
3022fe8fb19SBen Gras int
evResetTimer(evContext opaqueCtx,evTimerID id,evTimerFunc func,void * uap,struct timespec due,struct timespec inter)3032fe8fb19SBen Gras evResetTimer(evContext opaqueCtx,
3042fe8fb19SBen Gras evTimerID id,
3052fe8fb19SBen Gras evTimerFunc func,
3062fe8fb19SBen Gras void *uap,
3072fe8fb19SBen Gras struct timespec due,
3082fe8fb19SBen Gras struct timespec inter
3092fe8fb19SBen Gras ) {
3102fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
3112fe8fb19SBen Gras evTimer *timer = id.opaque;
3122fe8fb19SBen Gras struct timespec old_due;
3132fe8fb19SBen Gras int result=0;
3142fe8fb19SBen Gras
3152fe8fb19SBen Gras if (heap_element(ctx->timers, timer->index) != timer)
3162fe8fb19SBen Gras EV_ERR(ENOENT);
3172fe8fb19SBen Gras
3182fe8fb19SBen Gras #ifdef __hpux
3192fe8fb19SBen Gras /*
3202fe8fb19SBen Gras * tv_sec and tv_nsec are unsigned.
3212fe8fb19SBen Gras */
3222fe8fb19SBen Gras if (due.tv_nsec >= BILLION)
3232fe8fb19SBen Gras EV_ERR(EINVAL);
3242fe8fb19SBen Gras
3252fe8fb19SBen Gras if (inter.tv_nsec >= BILLION)
3262fe8fb19SBen Gras EV_ERR(EINVAL);
3272fe8fb19SBen Gras #else
3282fe8fb19SBen Gras if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
3292fe8fb19SBen Gras EV_ERR(EINVAL);
3302fe8fb19SBen Gras
3312fe8fb19SBen Gras if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
3322fe8fb19SBen Gras EV_ERR(EINVAL);
3332fe8fb19SBen Gras #endif
3342fe8fb19SBen Gras
3352fe8fb19SBen Gras old_due = timer->due;
3362fe8fb19SBen Gras
3372fe8fb19SBen Gras timer->func = func;
3382fe8fb19SBen Gras timer->uap = uap;
3392fe8fb19SBen Gras timer->due = due;
3402fe8fb19SBen Gras timer->inter = inter;
3412fe8fb19SBen Gras
3422fe8fb19SBen Gras switch (evCmpTime(due, old_due)) {
3432fe8fb19SBen Gras case -1:
3442fe8fb19SBen Gras result = heap_increased(ctx->timers, timer->index);
3452fe8fb19SBen Gras break;
3462fe8fb19SBen Gras case 0:
3472fe8fb19SBen Gras result = 0;
3482fe8fb19SBen Gras break;
3492fe8fb19SBen Gras case 1:
3502fe8fb19SBen Gras result = heap_decreased(ctx->timers, timer->index);
3512fe8fb19SBen Gras break;
3522fe8fb19SBen Gras }
3532fe8fb19SBen Gras
3542fe8fb19SBen Gras if (ctx->debug > 7) {
3552fe8fb19SBen Gras evPrintf(ctx, 7, "timers after evResetTimer:\n");
3562fe8fb19SBen Gras (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
3572fe8fb19SBen Gras }
3582fe8fb19SBen Gras
3592fe8fb19SBen Gras return (result);
3602fe8fb19SBen Gras }
3612fe8fb19SBen Gras
3622fe8fb19SBen Gras int
evSetIdleTimer(evContext opaqueCtx,evTimerFunc func,void * uap,struct timespec max_idle,evTimerID * opaqueID)3632fe8fb19SBen Gras evSetIdleTimer(evContext opaqueCtx,
3642fe8fb19SBen Gras evTimerFunc func,
3652fe8fb19SBen Gras void *uap,
3662fe8fb19SBen Gras struct timespec max_idle,
3672fe8fb19SBen Gras evTimerID *opaqueID
3682fe8fb19SBen Gras ) {
3692fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
3702fe8fb19SBen Gras idle_timer *tt;
3712fe8fb19SBen Gras
3722fe8fb19SBen Gras /* Allocate and fill. */
3732fe8fb19SBen Gras OKNEW(tt);
3742fe8fb19SBen Gras tt->func = func;
3752fe8fb19SBen Gras tt->uap = uap;
3762fe8fb19SBen Gras tt->lastTouched = ctx->lastEventTime;
3772fe8fb19SBen Gras tt->max_idle = max_idle;
3782fe8fb19SBen Gras
3792fe8fb19SBen Gras if (evSetTimer(opaqueCtx, idle_timeout, tt,
3802fe8fb19SBen Gras evAddTime(ctx->lastEventTime, max_idle),
3812fe8fb19SBen Gras max_idle, opaqueID) < 0) {
3822fe8fb19SBen Gras FREE(tt);
3832fe8fb19SBen Gras return (-1);
3842fe8fb19SBen Gras }
3852fe8fb19SBen Gras
3862fe8fb19SBen Gras tt->timer = opaqueID->opaque;
3872fe8fb19SBen Gras
3882fe8fb19SBen Gras return (0);
3892fe8fb19SBen Gras }
3902fe8fb19SBen Gras
3912fe8fb19SBen Gras int
evClearIdleTimer(evContext opaqueCtx,evTimerID id)3922fe8fb19SBen Gras evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
3932fe8fb19SBen Gras evTimer *del = id.opaque;
3942fe8fb19SBen Gras idle_timer *tt = del->uap;
3952fe8fb19SBen Gras
3962fe8fb19SBen Gras FREE(tt);
3972fe8fb19SBen Gras return (evClearTimer(opaqueCtx, id));
3982fe8fb19SBen Gras }
3992fe8fb19SBen Gras
4002fe8fb19SBen Gras int
evResetIdleTimer(evContext opaqueCtx,evTimerID opaqueID,evTimerFunc func,void * uap,struct timespec max_idle)4012fe8fb19SBen Gras evResetIdleTimer(evContext opaqueCtx,
4022fe8fb19SBen Gras evTimerID opaqueID,
4032fe8fb19SBen Gras evTimerFunc func,
4042fe8fb19SBen Gras void *uap,
4052fe8fb19SBen Gras struct timespec max_idle
4062fe8fb19SBen Gras ) {
4072fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
4082fe8fb19SBen Gras evTimer *timer = opaqueID.opaque;
4092fe8fb19SBen Gras idle_timer *tt = timer->uap;
4102fe8fb19SBen Gras
4112fe8fb19SBen Gras tt->func = func;
4122fe8fb19SBen Gras tt->uap = uap;
4132fe8fb19SBen Gras tt->lastTouched = ctx->lastEventTime;
4142fe8fb19SBen Gras tt->max_idle = max_idle;
4152fe8fb19SBen Gras
4162fe8fb19SBen Gras return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
4172fe8fb19SBen Gras evAddTime(ctx->lastEventTime, max_idle),
4182fe8fb19SBen Gras max_idle));
4192fe8fb19SBen Gras }
4202fe8fb19SBen Gras
4212fe8fb19SBen Gras int
evTouchIdleTimer(evContext opaqueCtx,evTimerID id)4222fe8fb19SBen Gras evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
4232fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
4242fe8fb19SBen Gras evTimer *t = id.opaque;
4252fe8fb19SBen Gras idle_timer *tt = t->uap;
4262fe8fb19SBen Gras
4272fe8fb19SBen Gras tt->lastTouched = ctx->lastEventTime;
4282fe8fb19SBen Gras
4292fe8fb19SBen Gras return (0);
4302fe8fb19SBen Gras }
4312fe8fb19SBen Gras
4322fe8fb19SBen Gras /* Public to the rest of eventlib. */
4332fe8fb19SBen Gras
4342fe8fb19SBen Gras heap_context
evCreateTimers(const evContext_p * ctx)4352fe8fb19SBen Gras evCreateTimers(const evContext_p *ctx) {
4362fe8fb19SBen Gras
4372fe8fb19SBen Gras UNUSED(ctx);
4382fe8fb19SBen Gras
4392fe8fb19SBen Gras return (heap_new(due_sooner, set_index, 2048));
4402fe8fb19SBen Gras }
4412fe8fb19SBen Gras
4422fe8fb19SBen Gras void
evDestroyTimers(const evContext_p * ctx)4432fe8fb19SBen Gras evDestroyTimers(const evContext_p *ctx) {
4442fe8fb19SBen Gras (void) heap_for_each(ctx->timers, free_timer, NULL);
4452fe8fb19SBen Gras (void) heap_free(ctx->timers);
4462fe8fb19SBen Gras }
4472fe8fb19SBen Gras
4482fe8fb19SBen Gras /* Private. */
4492fe8fb19SBen Gras
4502fe8fb19SBen Gras static int
due_sooner(void * a,void * b)4512fe8fb19SBen Gras due_sooner(void *a, void *b) {
4522fe8fb19SBen Gras evTimer *a_timer, *b_timer;
4532fe8fb19SBen Gras
4542fe8fb19SBen Gras a_timer = a;
4552fe8fb19SBen Gras b_timer = b;
4562fe8fb19SBen Gras return (evCmpTime(a_timer->due, b_timer->due) < 0);
4572fe8fb19SBen Gras }
4582fe8fb19SBen Gras
4592fe8fb19SBen Gras static void
set_index(void * what,int idx)4602fe8fb19SBen Gras set_index(void *what, int idx) {
4612fe8fb19SBen Gras evTimer *timer;
4622fe8fb19SBen Gras
4632fe8fb19SBen Gras timer = what;
4642fe8fb19SBen Gras timer->index = idx;
4652fe8fb19SBen Gras }
4662fe8fb19SBen Gras
4672fe8fb19SBen Gras static void
free_timer(void * what,void * uap)4682fe8fb19SBen Gras free_timer(void *what, void *uap) {
4692fe8fb19SBen Gras evTimer *t = what;
4702fe8fb19SBen Gras
4712fe8fb19SBen Gras UNUSED(uap);
4722fe8fb19SBen Gras
4732fe8fb19SBen Gras FREE(t);
4742fe8fb19SBen Gras }
4752fe8fb19SBen Gras
4762fe8fb19SBen Gras static void
print_timer(void * what,void * uap)4772fe8fb19SBen Gras print_timer(void *what, void *uap) {
4782fe8fb19SBen Gras evTimer *cur = what;
4792fe8fb19SBen Gras evContext_p *ctx = uap;
4802fe8fb19SBen Gras
4812fe8fb19SBen Gras cur = what;
4822fe8fb19SBen Gras evPrintf(ctx, 7,
4832fe8fb19SBen Gras " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
4842fe8fb19SBen Gras cur->func, cur->uap,
4852fe8fb19SBen Gras (long)cur->due.tv_sec, cur->due.tv_nsec,
4862fe8fb19SBen Gras (long)cur->inter.tv_sec, cur->inter.tv_nsec);
4872fe8fb19SBen Gras }
4882fe8fb19SBen Gras
4892fe8fb19SBen Gras static void
idle_timeout(evContext opaqueCtx,void * uap,struct timespec due,struct timespec inter)4902fe8fb19SBen Gras idle_timeout(evContext opaqueCtx,
4912fe8fb19SBen Gras void *uap,
4922fe8fb19SBen Gras struct timespec due,
4932fe8fb19SBen Gras struct timespec inter
4942fe8fb19SBen Gras ) {
4952fe8fb19SBen Gras evContext_p *ctx = opaqueCtx.opaque;
4962fe8fb19SBen Gras idle_timer *this = uap;
4972fe8fb19SBen Gras struct timespec idle;
4982fe8fb19SBen Gras
4992fe8fb19SBen Gras UNUSED(due);
5002fe8fb19SBen Gras UNUSED(inter);
5012fe8fb19SBen Gras
5022fe8fb19SBen Gras idle = evSubTime(ctx->lastEventTime, this->lastTouched);
5032fe8fb19SBen Gras if (evCmpTime(idle, this->max_idle) >= 0) {
5042fe8fb19SBen Gras (this->func)(opaqueCtx, this->uap, this->timer->due,
5052fe8fb19SBen Gras this->max_idle);
5062fe8fb19SBen Gras /*
5072fe8fb19SBen Gras * Setting the interval to zero will cause the timer to
5082fe8fb19SBen Gras * be cleaned up in evDrop().
5092fe8fb19SBen Gras */
5102fe8fb19SBen Gras this->timer->inter = evConsTime(0L, 0L);
5112fe8fb19SBen Gras FREE(this);
5122fe8fb19SBen Gras } else {
5132fe8fb19SBen Gras /* evDrop() will reschedule the timer. */
5142fe8fb19SBen Gras this->timer->inter = evSubTime(this->max_idle, idle);
5152fe8fb19SBen Gras }
5162fe8fb19SBen Gras }
5172fe8fb19SBen Gras #endif
5182fe8fb19SBen Gras
5192fe8fb19SBen Gras /*! \file */
520