1 /* $NetBSD: ev_timers.c,v 1.8 2009/04/12 17:07:17 christos Exp $ */
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 #include <sys/cdefs.h>
25 #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
27 static const char rcsid
[] = "Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp";
29 __RCSID("$NetBSD: ev_timers.c,v 1.8 2009/04/12 17:07:17 christos Exp $");
35 #include "port_before.h"
36 #include "fd_setsize.h"
40 #include <isc/assertions.h>
41 #include <isc/eventlib.h>
42 #include "eventlib_p.h"
44 #include "port_after.h"
48 #define MILLION 1000000
49 #define BILLION 1000000000
54 static int due_sooner(void *, void *);
55 static void set_index(void *, int);
56 static void free_timer(void *, void *);
57 static void print_timer(void *, void *);
58 static void idle_timeout(evContext
, void *, struct timespec
, struct timespec
);
65 struct timespec lastTouched
;
66 struct timespec max_idle
;
74 evConsTime(time_t sec
, long nsec
) {
83 evAddTime(struct timespec addend1
, struct timespec addend2
) {
86 x
.tv_sec
= addend1
.tv_sec
+ addend2
.tv_sec
;
87 x
.tv_nsec
= addend1
.tv_nsec
+ addend2
.tv_nsec
;
88 if (x
.tv_nsec
>= BILLION
) {
96 evSubTime(struct timespec minuend
, struct timespec subtrahend
) {
99 x
.tv_sec
= minuend
.tv_sec
- subtrahend
.tv_sec
;
100 if (minuend
.tv_nsec
>= subtrahend
.tv_nsec
)
101 x
.tv_nsec
= minuend
.tv_nsec
- subtrahend
.tv_nsec
;
103 x
.tv_nsec
= BILLION
- subtrahend
.tv_nsec
+ minuend
.tv_nsec
;
110 evCmpTime(struct timespec a
, struct timespec b
) {
111 #define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
112 time_t s
= a
.tv_sec
- b
.tv_sec
;
118 n
= a
.tv_nsec
- b
.tv_nsec
;
125 #ifdef CLOCK_REALTIME
126 struct timespec tsnow
;
127 int m
= CLOCK_REALTIME
;
129 #ifdef CLOCK_MONOTONIC
135 if (clock_gettime(m
, &tsnow
) == 0)
138 if (gettimeofday(&now
, NULL
) < 0)
139 return (evConsTime(0L, 0L));
140 return (evTimeSpec(now
));
146 #ifdef CLOCK_REALTIME
147 struct timespec tsnow
;
148 if (clock_gettime(CLOCK_REALTIME
, &tsnow
) == 0)
151 if (gettimeofday(&now
, NULL
) < 0)
152 return (evConsTime(0L, 0L));
153 return (evTimeSpec(now
));
158 evLastEventTime(evContext opaqueCtx
) {
159 evContext_p
*ctx
= opaqueCtx
.opaque
;
161 return (ctx
->lastEventTime
);
166 evTimeSpec(struct timeval tv
) {
169 ts
.tv_sec
= tv
.tv_sec
;
170 ts
.tv_nsec
= tv
.tv_usec
* 1000;
175 evTimeVal(struct timespec ts
) {
178 tv
.tv_sec
= ts
.tv_sec
;
179 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
185 evSetTimer(evContext opaqueCtx
,
189 struct timespec inter
,
192 evContext_p
*ctx
= opaqueCtx
.opaque
;
196 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
198 (long)due
.tv_sec
, due
.tv_nsec
,
199 (long)inter
.tv_sec
, inter
.tv_nsec
);
203 * tv_sec and tv_nsec are unsigned.
205 if (due
.tv_nsec
>= BILLION
)
208 if (inter
.tv_nsec
>= BILLION
)
211 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
214 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
218 /* due={0,0} is a magic cookie meaning "now." */
219 if (due
.tv_sec
== (time_t)0 && due
.tv_nsec
== 0L)
222 /* Allocate and fill. */
229 if (heap_insert(ctx
->timers
, id
) < 0)
232 /* Remember the ID if the caller provided us a place for it. */
234 opaqueID
->opaque
= id
;
236 if (ctx
->debug
> 7) {
237 evPrintf(ctx
, 7, "timers after evSetTimer:\n");
238 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
245 evClearTimer(evContext opaqueCtx
, evTimerID id
) {
246 evContext_p
*ctx
= opaqueCtx
.opaque
;
247 evTimer
*del
= id
.opaque
;
249 if (ctx
->cur
!= NULL
&&
250 ctx
->cur
->type
== Timer
&&
251 ctx
->cur
->u
.timer
.this == del
) {
252 evPrintf(ctx
, 8, "deferring delete of timer (executing)\n");
254 * Setting the interval to zero ensures that evDrop() will
255 * clean up the timer.
257 del
->inter
= evConsTime(0, 0);
261 if (heap_element(ctx
->timers
, del
->index
) != del
)
264 if (heap_delete(ctx
->timers
, del
->index
) < 0)
268 if (ctx
->debug
> 7) {
269 evPrintf(ctx
, 7, "timers after evClearTimer:\n");
270 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
277 evConfigTimer(evContext opaqueCtx
,
282 evContext_p
*ctx
= opaqueCtx
.opaque
;
283 evTimer
*timer
= id
.opaque
;
288 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
291 if (strcmp(param
, "rate") == 0)
292 timer
->mode
|= EV_TMR_RATE
;
293 else if (strcmp(param
, "interval") == 0)
294 timer
->mode
&= ~EV_TMR_RATE
;
302 evResetTimer(evContext opaqueCtx
,
307 struct timespec inter
309 evContext_p
*ctx
= opaqueCtx
.opaque
;
310 evTimer
*timer
= id
.opaque
;
311 struct timespec old_due
;
314 if (heap_element(ctx
->timers
, timer
->index
) != timer
)
319 * tv_sec and tv_nsec are unsigned.
321 if (due
.tv_nsec
>= BILLION
)
324 if (inter
.tv_nsec
>= BILLION
)
327 if (due
.tv_sec
< 0 || due
.tv_nsec
< 0 || due
.tv_nsec
>= BILLION
)
330 if (inter
.tv_sec
< 0 || inter
.tv_nsec
< 0 || inter
.tv_nsec
>= BILLION
)
334 old_due
= timer
->due
;
339 timer
->inter
= inter
;
341 switch (evCmpTime(due
, old_due
)) {
343 result
= heap_increased(ctx
->timers
, timer
->index
);
349 result
= heap_decreased(ctx
->timers
, timer
->index
);
353 if (ctx
->debug
> 7) {
354 evPrintf(ctx
, 7, "timers after evResetTimer:\n");
355 (void) heap_for_each(ctx
->timers
, print_timer
, (void *)ctx
);
362 evSetIdleTimer(evContext opaqueCtx
,
365 struct timespec max_idle
,
368 evContext_p
*ctx
= opaqueCtx
.opaque
;
371 /* Allocate and fill. */
375 tt
->lastTouched
= ctx
->lastEventTime
;
376 tt
->max_idle
= max_idle
;
378 if (evSetTimer(opaqueCtx
, idle_timeout
, tt
,
379 evAddTime(ctx
->lastEventTime
, max_idle
),
380 max_idle
, opaqueID
) < 0) {
385 tt
->timer
= opaqueID
->opaque
;
391 evClearIdleTimer(evContext opaqueCtx
, evTimerID id
) {
392 evTimer
*del
= id
.opaque
;
393 idle_timer
*tt
= del
->uap
;
396 return (evClearTimer(opaqueCtx
, id
));
400 evResetIdleTimer(evContext opaqueCtx
,
404 struct timespec max_idle
406 evContext_p
*ctx
= opaqueCtx
.opaque
;
407 evTimer
*timer
= opaqueID
.opaque
;
408 idle_timer
*tt
= timer
->uap
;
412 tt
->lastTouched
= ctx
->lastEventTime
;
413 tt
->max_idle
= max_idle
;
415 return (evResetTimer(opaqueCtx
, opaqueID
, idle_timeout
, tt
,
416 evAddTime(ctx
->lastEventTime
, max_idle
),
421 evTouchIdleTimer(evContext opaqueCtx
, evTimerID id
) {
422 evContext_p
*ctx
= opaqueCtx
.opaque
;
423 evTimer
*t
= id
.opaque
;
424 idle_timer
*tt
= t
->uap
;
426 tt
->lastTouched
= ctx
->lastEventTime
;
431 /* Public to the rest of eventlib. */
434 evCreateTimers(const evContext_p
*ctx
) {
438 return (heap_new(due_sooner
, set_index
, 2048));
442 evDestroyTimers(const evContext_p
*ctx
) {
443 (void) heap_for_each(ctx
->timers
, free_timer
, NULL
);
444 (void) heap_free(ctx
->timers
);
450 due_sooner(void *a
, void *b
) {
451 evTimer
*a_timer
, *b_timer
;
455 return (evCmpTime(a_timer
->due
, b_timer
->due
) < 0);
459 set_index(void *what
, int idx
) {
467 free_timer(void *what
, void *uap
) {
476 print_timer(void *what
, void *uap
) {
478 evContext_p
*ctx
= uap
;
482 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
484 (long)cur
->due
.tv_sec
, cur
->due
.tv_nsec
,
485 (long)cur
->inter
.tv_sec
, cur
->inter
.tv_nsec
);
489 idle_timeout(evContext opaqueCtx
,
492 struct timespec inter
494 evContext_p
*ctx
= opaqueCtx
.opaque
;
495 idle_timer
*this = uap
;
496 struct timespec idle
;
501 idle
= evSubTime(ctx
->lastEventTime
, this->lastTouched
);
502 if (evCmpTime(idle
, this->max_idle
) >= 0) {
503 (this->func
)(opaqueCtx
, this->uap
, this->timer
->due
,
506 * Setting the interval to zero will cause the timer to
507 * be cleaned up in evDrop().
509 this->timer
->inter
= evConsTime(0L, 0L);
512 /* evDrop() will reschedule the timer. */
513 this->timer
->inter
= evSubTime(this->max_idle
, idle
);