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.6 2005/04/27 04:56:36 sra 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
50 static int __evOptMonoTime
;
52 static int due_sooner(void *, void *);
53 static void set_index(void *, int);
54 static void free_timer(void *, void *);
55 static void print_timer(void *, void *);
56 static void idle_timeout(evContext
, void *, struct timespec
, struct timespec
);
63 struct timespec lastTouched
;
64 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;
164 #if !defined(USE_KQUEUE) || !defined(_LIBC)
166 evTimeVal(struct timespec ts
) {
169 tv
.tv_sec
= ts
.tv_sec
;
170 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
177 evSetTimer(evContext opaqueCtx
,
181 struct timespec inter
,
184 evContext_p
*ctx
= opaqueCtx
.opaque
;
188 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
190 (long)due
.tv_sec
, due
.tv_nsec
,
191 (long)inter
.tv_sec
, inter
.tv_nsec
);
195 * tv_sec and tv_nsec are unsigned.
197 if (due
.tv_nsec
>= BILLION
)
200 if (inter
.tv_nsec
>= BILLION
)
203 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
206 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
210 /* due={0,0} is a magic cookie meaning "now." */
211 if (due
.tv_sec
== (time_t)0 && due
.tv_nsec
== 0L)
214 /* Allocate and fill. */
221 if (heap_insert(ctx
->timers
, id
) < 0)
224 /* Remember the ID if the caller provided us a place for it. */
226 opaqueID
->opaque
= id
;
228 if (ctx
->debug
> 7) {
229 evPrintf(ctx
, 7, "timers after evSetTimer:\n");
230 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
237 evClearTimer(evContext opaqueCtx
, evTimerID id
) {
238 evContext_p
*ctx
= opaqueCtx
.opaque
;
239 evTimer
*del
= id
.opaque
;
241 if (ctx
->cur
!= NULL
&&
242 ctx
->cur
->type
== Timer
&&
243 ctx
->cur
->u
.timer
.this == del
) {
244 evPrintf(ctx
, 8, "deferring delete of timer (executing)\n");
246 * Setting the interval to zero ensures that evDrop() will
247 * clean up the timer.
249 del
->inter
= evConsTime(0, 0);
253 if (heap_element(ctx
->timers
, del
->index
) != del
)
256 if (heap_delete(ctx
->timers
, del
->index
) < 0)
260 if (ctx
->debug
> 7) {
261 evPrintf(ctx
, 7, "timers after evClearTimer:\n");
262 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
269 evConfigTimer(evContext opaqueCtx
,
274 evContext_p
*ctx
= opaqueCtx
.opaque
;
275 evTimer
*timer
= id
.opaque
;
280 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
283 if (strcmp(param
, "rate") == 0)
284 timer
->mode
|= EV_TMR_RATE
;
285 else if (strcmp(param
, "interval") == 0)
286 timer
->mode
&= ~EV_TMR_RATE
;
294 evResetTimer(evContext opaqueCtx
,
299 struct timespec inter
301 evContext_p
*ctx
= opaqueCtx
.opaque
;
302 evTimer
*timer
= id
.opaque
;
303 struct timespec old_due
;
306 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
311 * tv_sec and tv_nsec are unsigned.
313 if (due
.tv_nsec
>= BILLION
)
316 if (inter
.tv_nsec
>= BILLION
)
319 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
322 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
326 old_due
= timer
->due
;
331 timer
->inter
= inter
;
333 switch (evCmpTime(due
, old_due
)) {
335 result
= heap_increased(ctx
->timers
, timer
->index
);
341 result
= heap_decreased(ctx
->timers
, timer
->index
);
345 if (ctx
->debug
> 7) {
346 evPrintf(ctx
, 7, "timers after evResetTimer:\n");
347 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
354 evSetIdleTimer(evContext opaqueCtx
,
357 struct timespec max_idle
,
360 evContext_p
*ctx
= opaqueCtx
.opaque
;
363 /* Allocate and fill. */
367 tt
->lastTouched
= ctx
->lastEventTime
;
368 tt
->max_idle
= max_idle
;
370 if (evSetTimer(opaqueCtx
, idle_timeout
, tt
,
371 evAddTime(ctx
->lastEventTime
, max_idle
),
372 max_idle
, opaqueID
) < 0) {
377 tt
->timer
= opaqueID
->opaque
;
383 evClearIdleTimer(evContext opaqueCtx
, evTimerID id
) {
384 evTimer
*del
= id
.opaque
;
385 idle_timer
*tt
= del
->uap
;
388 return (evClearTimer(opaqueCtx
, id
));
392 evResetIdleTimer(evContext opaqueCtx
,
396 struct timespec max_idle
398 evContext_p
*ctx
= opaqueCtx
.opaque
;
399 evTimer
*timer
= opaqueID
.opaque
;
400 idle_timer
*tt
= timer
->uap
;
404 tt
->lastTouched
= ctx
->lastEventTime
;
405 tt
->max_idle
= max_idle
;
407 return (evResetTimer(opaqueCtx
, opaqueID
, idle_timeout
, tt
,
408 evAddTime(ctx
->lastEventTime
, max_idle
),
413 evTouchIdleTimer(evContext opaqueCtx
, evTimerID id
) {
414 evContext_p
*ctx
= opaqueCtx
.opaque
;
415 evTimer
*t
= id
.opaque
;
416 idle_timer
*tt
= t
->uap
;
418 tt
->lastTouched
= ctx
->lastEventTime
;
423 /* Public to the rest of eventlib. */
426 evCreateTimers(const evContext_p
*ctx
) {
430 return (heap_new(due_sooner
, set_index
, 2048));
434 evDestroyTimers(const evContext_p
*ctx
) {
435 (void) heap_for_each(ctx
->timers
, free_timer
, NULL
);
436 (void) heap_free(ctx
->timers
);
442 due_sooner(void *a
, void *b
) {
443 evTimer
*a_timer
, *b_timer
;
447 return (evCmpTime(a_timer
->due
, b_timer
->due
) < 0);
451 set_index(void *what
, int index
) {
455 timer
->index
= index
;
459 free_timer(void *what
, void *uap
) {
468 print_timer(void *what
, void *uap
) {
470 evContext_p
*ctx
= uap
;
474 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
476 (long)cur
->due
.tv_sec
, cur
->due
.tv_nsec
,
477 (long)cur
->inter
.tv_sec
, cur
->inter
.tv_nsec
);
481 idle_timeout(evContext opaqueCtx
,
484 struct timespec inter
486 evContext_p
*ctx
= opaqueCtx
.opaque
;
487 idle_timer
*this = uap
;
488 struct timespec idle
;
493 idle
= evSubTime(ctx
->lastEventTime
, this->lastTouched
);
494 if (evCmpTime(idle
, this->max_idle
) >= 0) {
495 (this->func
)(opaqueCtx
, this->uap
, this->timer
->due
,
498 * Setting the interval to zero will cause the timer to
499 * be cleaned up in evDrop().
501 this->timer
->inter
= evConsTime(0, 0);
504 /* evDrop() will reschedule the timer. */
505 this->timer
->inter
= evSubTime(this->max_idle
, idle
);