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";
30 #include "port_before.h"
31 #include "fd_setsize.h"
35 #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
48 static int due_sooner(void *, void *);
49 static void set_index(void *, int);
50 static void free_timer(void *, void *);
51 static void print_timer(void *, void *);
52 static void idle_timeout(evContext
, void *, struct timespec
, struct timespec
);
59 struct timespec lastTouched
;
60 struct timespec max_idle
;
67 evConsTime(time_t sec
, long nsec
) {
76 evAddTime(struct timespec addend1
, struct timespec addend2
) {
79 x
.tv_sec
= addend1
.tv_sec
+ addend2
.tv_sec
;
80 x
.tv_nsec
= addend1
.tv_nsec
+ addend2
.tv_nsec
;
81 if (x
.tv_nsec
>= BILLION
) {
89 evSubTime(struct timespec minuend
, struct timespec subtrahend
) {
92 x
.tv_sec
= minuend
.tv_sec
- subtrahend
.tv_sec
;
93 if (minuend
.tv_nsec
>= subtrahend
.tv_nsec
)
94 x
.tv_nsec
= minuend
.tv_nsec
- subtrahend
.tv_nsec
;
96 x
.tv_nsec
= BILLION
- subtrahend
.tv_nsec
+ minuend
.tv_nsec
;
103 evCmpTime(struct timespec a
, struct timespec b
) {
104 long x
= a
.tv_sec
- b
.tv_sec
;
107 x
= a
.tv_nsec
- b
.tv_nsec
;
108 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
114 #ifdef CLOCK_REALTIME
115 struct timespec tsnow
;
116 int m
= CLOCK_REALTIME
;
118 #ifdef CLOCK_MONOTONIC
122 if (clock_gettime(m
, &tsnow
) == 0)
125 if (gettimeofday(&now
, NULL
) < 0)
126 return (evConsTime(0, 0));
127 return (evTimeSpec(now
));
133 #ifdef CLOCK_REALTIME
134 struct timespec tsnow
;
135 if (clock_gettime(CLOCK_REALTIME
, &tsnow
) == 0)
138 if (gettimeofday(&now
, NULL
) < 0)
139 return (evConsTime(0, 0));
140 return (evTimeSpec(now
));
144 evLastEventTime(evContext opaqueCtx
) {
145 evContext_p
*ctx
= opaqueCtx
.opaque
;
147 return (ctx
->lastEventTime
);
151 evTimeSpec(struct timeval tv
) {
154 ts
.tv_sec
= tv
.tv_sec
;
155 ts
.tv_nsec
= tv
.tv_usec
* 1000;
160 evTimeVal(struct timespec ts
) {
163 tv
.tv_sec
= ts
.tv_sec
;
164 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
169 evSetTimer(evContext opaqueCtx
,
173 struct timespec inter
,
176 evContext_p
*ctx
= opaqueCtx
.opaque
;
180 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
182 (long)due
.tv_sec
, due
.tv_nsec
,
183 (long)inter
.tv_sec
, inter
.tv_nsec
);
187 * tv_sec and tv_nsec are unsigned.
189 if (due
.tv_nsec
>= BILLION
)
192 if (inter
.tv_nsec
>= BILLION
)
195 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
198 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
202 /* due={0,0} is a magic cookie meaning "now." */
203 if (due
.tv_sec
== (time_t)0 && due
.tv_nsec
== 0L)
206 /* Allocate and fill. */
213 if (heap_insert(ctx
->timers
, id
) < 0)
216 /* Remember the ID if the caller provided us a place for it. */
218 opaqueID
->opaque
= id
;
220 if (ctx
->debug
> 7) {
221 evPrintf(ctx
, 7, "timers after evSetTimer:\n");
222 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
229 evClearTimer(evContext opaqueCtx
, evTimerID id
) {
230 evContext_p
*ctx
= opaqueCtx
.opaque
;
231 evTimer
*del
= id
.opaque
;
233 if (ctx
->cur
!= NULL
&&
234 ctx
->cur
->type
== Timer
&&
235 ctx
->cur
->u
.timer
.this == del
) {
236 evPrintf(ctx
, 8, "deferring delete of timer (executing)\n");
238 * Setting the interval to zero ensures that evDrop() will
239 * clean up the timer.
241 del
->inter
= evConsTime(0, 0);
245 if (heap_element(ctx
->timers
, del
->index
) != del
)
248 if (heap_delete(ctx
->timers
, del
->index
) < 0)
252 if (ctx
->debug
> 7) {
253 evPrintf(ctx
, 7, "timers after evClearTimer:\n");
254 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
261 evConfigTimer(evContext opaqueCtx
,
266 evContext_p
*ctx
= opaqueCtx
.opaque
;
267 evTimer
*timer
= id
.opaque
;
272 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
275 if (strcmp(param
, "rate") == 0)
276 timer
->mode
|= EV_TMR_RATE
;
277 else if (strcmp(param
, "interval") == 0)
278 timer
->mode
&= ~EV_TMR_RATE
;
286 evResetTimer(evContext opaqueCtx
,
291 struct timespec inter
293 evContext_p
*ctx
= opaqueCtx
.opaque
;
294 evTimer
*timer
= id
.opaque
;
295 struct timespec old_due
;
298 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
303 * tv_sec and tv_nsec are unsigned.
305 if (due
.tv_nsec
>= BILLION
)
308 if (inter
.tv_nsec
>= BILLION
)
311 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
314 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
318 old_due
= timer
->due
;
323 timer
->inter
= inter
;
325 switch (evCmpTime(due
, old_due
)) {
327 result
= heap_increased(ctx
->timers
, timer
->index
);
333 result
= heap_decreased(ctx
->timers
, timer
->index
);
337 if (ctx
->debug
> 7) {
338 evPrintf(ctx
, 7, "timers after evResetTimer:\n");
339 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
346 evSetIdleTimer(evContext opaqueCtx
,
349 struct timespec max_idle
,
352 evContext_p
*ctx
= opaqueCtx
.opaque
;
355 /* Allocate and fill. */
359 tt
->lastTouched
= ctx
->lastEventTime
;
360 tt
->max_idle
= max_idle
;
362 if (evSetTimer(opaqueCtx
, idle_timeout
, tt
,
363 evAddTime(ctx
->lastEventTime
, max_idle
),
364 max_idle
, opaqueID
) < 0) {
369 tt
->timer
= opaqueID
->opaque
;
375 evClearIdleTimer(evContext opaqueCtx
, evTimerID id
) {
376 evTimer
*del
= id
.opaque
;
377 idle_timer
*tt
= del
->uap
;
380 return (evClearTimer(opaqueCtx
, id
));
384 evResetIdleTimer(evContext opaqueCtx
,
388 struct timespec max_idle
390 evContext_p
*ctx
= opaqueCtx
.opaque
;
391 evTimer
*timer
= opaqueID
.opaque
;
392 idle_timer
*tt
= timer
->uap
;
396 tt
->lastTouched
= ctx
->lastEventTime
;
397 tt
->max_idle
= max_idle
;
399 return (evResetTimer(opaqueCtx
, opaqueID
, idle_timeout
, tt
,
400 evAddTime(ctx
->lastEventTime
, max_idle
),
405 evTouchIdleTimer(evContext opaqueCtx
, evTimerID id
) {
406 evContext_p
*ctx
= opaqueCtx
.opaque
;
407 evTimer
*t
= id
.opaque
;
408 idle_timer
*tt
= t
->uap
;
410 tt
->lastTouched
= ctx
->lastEventTime
;
415 /* Public to the rest of eventlib. */
418 evCreateTimers(const evContext_p
*ctx
) {
422 return (heap_new(due_sooner
, set_index
, 2048));
426 evDestroyTimers(const evContext_p
*ctx
) {
427 (void) heap_for_each(ctx
->timers
, free_timer
, NULL
);
428 (void) heap_free(ctx
->timers
);
434 due_sooner(void *a
, void *b
) {
435 evTimer
*a_timer
, *b_timer
;
439 return (evCmpTime(a_timer
->due
, b_timer
->due
) < 0);
443 set_index(void *what
, int index
) {
447 timer
->index
= index
;
451 free_timer(void *what
, void *uap
) {
460 print_timer(void *what
, void *uap
) {
462 evContext_p
*ctx
= uap
;
466 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
468 (long)cur
->due
.tv_sec
, cur
->due
.tv_nsec
,
469 (long)cur
->inter
.tv_sec
, cur
->inter
.tv_nsec
);
473 idle_timeout(evContext opaqueCtx
,
476 struct timespec inter
478 evContext_p
*ctx
= opaqueCtx
.opaque
;
479 idle_timer
*this = uap
;
480 struct timespec idle
;
485 idle
= evSubTime(ctx
->lastEventTime
, this->lastTouched
);
486 if (evCmpTime(idle
, this->max_idle
) >= 0) {
487 (this->func
)(opaqueCtx
, this->uap
, this->timer
->due
,
490 * Setting the interval to zero will cause the timer to
491 * be cleaned up in evDrop().
493 this->timer
->inter
= evConsTime(0, 0);
496 /* evDrop() will reschedule the timer. */
497 this->timer
->inter
= evSubTime(this->max_idle
, idle
);