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"
29 #include "fd_setsize.h"
33 #include <isc/assertions.h>
34 #include <isc/eventlib.h>
35 #include "eventlib_p.h"
37 #include "port_after.h"
41 #define MILLION 1000000
42 #define BILLION 1000000000
46 static int due_sooner(void *, void *);
47 static void set_index(void *, int);
48 static void free_timer(void *, void *);
49 static void print_timer(void *, void *);
50 static void idle_timeout(evContext
, void *, struct timespec
, struct timespec
);
57 struct timespec lastTouched
;
58 struct timespec max_idle
;
65 evConsTime(time_t sec
, long nsec
) {
74 evAddTime(struct timespec addend1
, struct timespec addend2
) {
77 x
.tv_sec
= addend1
.tv_sec
+ addend2
.tv_sec
;
78 x
.tv_nsec
= addend1
.tv_nsec
+ addend2
.tv_nsec
;
79 if (x
.tv_nsec
>= BILLION
) {
87 evSubTime(struct timespec minuend
, struct timespec subtrahend
) {
90 x
.tv_sec
= minuend
.tv_sec
- subtrahend
.tv_sec
;
91 if (minuend
.tv_nsec
>= subtrahend
.tv_nsec
)
92 x
.tv_nsec
= minuend
.tv_nsec
- subtrahend
.tv_nsec
;
94 x
.tv_nsec
= BILLION
- subtrahend
.tv_nsec
+ minuend
.tv_nsec
;
101 evCmpTime(struct timespec a
, struct timespec b
) {
102 long x
= a
.tv_sec
- b
.tv_sec
;
105 x
= a
.tv_nsec
- b
.tv_nsec
;
106 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
112 #ifdef CLOCK_REALTIME
113 struct timespec tsnow
;
114 int m
= CLOCK_REALTIME
;
116 #ifdef CLOCK_MONOTONIC
120 if (clock_gettime(m
, &tsnow
) == 0)
123 if (gettimeofday(&now
, NULL
) < 0)
124 return (evConsTime(0, 0));
125 return (evTimeSpec(now
));
131 #ifdef CLOCK_REALTIME
132 struct timespec tsnow
;
133 if (clock_gettime(CLOCK_REALTIME
, &tsnow
) == 0)
136 if (gettimeofday(&now
, NULL
) < 0)
137 return (evConsTime(0, 0));
138 return (evTimeSpec(now
));
142 evLastEventTime(evContext opaqueCtx
) {
143 evContext_p
*ctx
= opaqueCtx
.opaque
;
145 return (ctx
->lastEventTime
);
149 evTimeSpec(struct timeval tv
) {
152 ts
.tv_sec
= tv
.tv_sec
;
153 ts
.tv_nsec
= tv
.tv_usec
* 1000;
158 evTimeVal(struct timespec ts
) {
161 tv
.tv_sec
= ts
.tv_sec
;
162 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
167 evSetTimer(evContext opaqueCtx
,
171 struct timespec inter
,
174 evContext_p
*ctx
= opaqueCtx
.opaque
;
178 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
180 (long)due
.tv_sec
, due
.tv_nsec
,
181 (long)inter
.tv_sec
, inter
.tv_nsec
);
185 * tv_sec and tv_nsec are unsigned.
187 if (due
.tv_nsec
>= BILLION
)
190 if (inter
.tv_nsec
>= BILLION
)
193 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
196 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
200 /* due={0,0} is a magic cookie meaning "now." */
201 if (due
.tv_sec
== (time_t)0 && due
.tv_nsec
== 0L)
204 /* Allocate and fill. */
211 if (heap_insert(ctx
->timers
, id
) < 0)
214 /* Remember the ID if the caller provided us a place for it. */
216 opaqueID
->opaque
= id
;
218 if (ctx
->debug
> 7) {
219 evPrintf(ctx
, 7, "timers after evSetTimer:\n");
220 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
227 evClearTimer(evContext opaqueCtx
, evTimerID id
) {
228 evContext_p
*ctx
= opaqueCtx
.opaque
;
229 evTimer
*del
= id
.opaque
;
231 if (ctx
->cur
!= NULL
&&
232 ctx
->cur
->type
== Timer
&&
233 ctx
->cur
->u
.timer
.this == del
) {
234 evPrintf(ctx
, 8, "deferring delete of timer (executing)\n");
236 * Setting the interval to zero ensures that evDrop() will
237 * clean up the timer.
239 del
->inter
= evConsTime(0, 0);
243 if (heap_element(ctx
->timers
, del
->index
) != del
)
246 if (heap_delete(ctx
->timers
, del
->index
) < 0)
250 if (ctx
->debug
> 7) {
251 evPrintf(ctx
, 7, "timers after evClearTimer:\n");
252 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
259 evConfigTimer(evContext opaqueCtx
,
264 evContext_p
*ctx
= opaqueCtx
.opaque
;
265 evTimer
*timer
= id
.opaque
;
270 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
273 if (strcmp(param
, "rate") == 0)
274 timer
->mode
|= EV_TMR_RATE
;
275 else if (strcmp(param
, "interval") == 0)
276 timer
->mode
&= ~EV_TMR_RATE
;
284 evResetTimer(evContext opaqueCtx
,
289 struct timespec inter
291 evContext_p
*ctx
= opaqueCtx
.opaque
;
292 evTimer
*timer
= id
.opaque
;
293 struct timespec old_due
;
296 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
301 * tv_sec and tv_nsec are unsigned.
303 if (due
.tv_nsec
>= BILLION
)
306 if (inter
.tv_nsec
>= BILLION
)
309 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
312 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
316 old_due
= timer
->due
;
321 timer
->inter
= inter
;
323 switch (evCmpTime(due
, old_due
)) {
325 result
= heap_increased(ctx
->timers
, timer
->index
);
331 result
= heap_decreased(ctx
->timers
, timer
->index
);
335 if (ctx
->debug
> 7) {
336 evPrintf(ctx
, 7, "timers after evResetTimer:\n");
337 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
344 evSetIdleTimer(evContext opaqueCtx
,
347 struct timespec max_idle
,
350 evContext_p
*ctx
= opaqueCtx
.opaque
;
353 /* Allocate and fill. */
357 tt
->lastTouched
= ctx
->lastEventTime
;
358 tt
->max_idle
= max_idle
;
360 if (evSetTimer(opaqueCtx
, idle_timeout
, tt
,
361 evAddTime(ctx
->lastEventTime
, max_idle
),
362 max_idle
, opaqueID
) < 0) {
367 tt
->timer
= opaqueID
->opaque
;
373 evClearIdleTimer(evContext opaqueCtx
, evTimerID id
) {
374 evTimer
*del
= id
.opaque
;
375 idle_timer
*tt
= del
->uap
;
378 return (evClearTimer(opaqueCtx
, id
));
382 evResetIdleTimer(evContext opaqueCtx
,
386 struct timespec max_idle
388 evContext_p
*ctx
= opaqueCtx
.opaque
;
389 evTimer
*timer
= opaqueID
.opaque
;
390 idle_timer
*tt
= timer
->uap
;
394 tt
->lastTouched
= ctx
->lastEventTime
;
395 tt
->max_idle
= max_idle
;
397 return (evResetTimer(opaqueCtx
, opaqueID
, idle_timeout
, tt
,
398 evAddTime(ctx
->lastEventTime
, max_idle
),
403 evTouchIdleTimer(evContext opaqueCtx
, evTimerID id
) {
404 evContext_p
*ctx
= opaqueCtx
.opaque
;
405 evTimer
*t
= id
.opaque
;
406 idle_timer
*tt
= t
->uap
;
408 tt
->lastTouched
= ctx
->lastEventTime
;
413 /* Public to the rest of eventlib. */
416 evCreateTimers(const evContext_p
*ctx
) {
420 return (heap_new(due_sooner
, set_index
, 2048));
424 evDestroyTimers(const evContext_p
*ctx
) {
425 (void) heap_for_each(ctx
->timers
, free_timer
, NULL
);
426 (void) heap_free(ctx
->timers
);
432 due_sooner(void *a
, void *b
) {
433 evTimer
*a_timer
, *b_timer
;
437 return (evCmpTime(a_timer
->due
, b_timer
->due
) < 0);
441 set_index(void *what
, int index
) {
445 timer
->index
= index
;
449 free_timer(void *what
, void *uap
) {
458 print_timer(void *what
, void *uap
) {
460 evContext_p
*ctx
= uap
;
464 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
466 (long)cur
->due
.tv_sec
, cur
->due
.tv_nsec
,
467 (long)cur
->inter
.tv_sec
, cur
->inter
.tv_nsec
);
471 idle_timeout(evContext opaqueCtx
,
474 struct timespec inter
476 evContext_p
*ctx
= opaqueCtx
.opaque
;
477 idle_timer
*this = uap
;
478 struct timespec idle
;
483 idle
= evSubTime(ctx
->lastEventTime
, this->lastTouched
);
484 if (evCmpTime(idle
, this->max_idle
) >= 0) {
485 (this->func
)(opaqueCtx
, this->uap
, this->timer
->due
,
488 * Setting the interval to zero will cause the timer to
489 * be cleaned up in evDrop().
491 this->timer
->inter
= evConsTime(0, 0);
494 /* evDrop() will reschedule the timer. */
495 this->timer
->inter
= evSubTime(this->max_idle
, idle
);