No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / isc / ev_timers.c
blobe12472baca8891bd2b8e08f2a13af3cd087c5a58
1 /* $NetBSD$ */
3 /*
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";
26 #endif
28 /* Import. */
30 #include "port_before.h"
31 #include "fd_setsize.h"
33 #include <errno.h>
35 #include <isc/assertions.h>
36 #include <isc/eventlib.h>
37 #include "eventlib_p.h"
39 #include "port_after.h"
41 /* Constants. */
43 #define MILLION 1000000
44 #define BILLION 1000000000
46 /* Forward. */
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);
54 /* Private type. */
56 typedef struct {
57 evTimerFunc func;
58 void * uap;
59 struct timespec lastTouched;
60 struct timespec max_idle;
61 evTimer * timer;
62 } idle_timer;
64 /* Public. */
66 struct timespec
67 evConsTime(time_t sec, long nsec) {
68 struct timespec x;
70 x.tv_sec = sec;
71 x.tv_nsec = nsec;
72 return (x);
75 struct timespec
76 evAddTime(struct timespec addend1, struct timespec addend2) {
77 struct timespec x;
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) {
82 x.tv_sec++;
83 x.tv_nsec -= BILLION;
85 return (x);
88 struct timespec
89 evSubTime(struct timespec minuend, struct timespec subtrahend) {
90 struct timespec x;
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;
95 else {
96 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
97 x.tv_sec--;
99 return (x);
103 evCmpTime(struct timespec a, struct timespec b) {
104 long x = a.tv_sec - b.tv_sec;
106 if (x == 0L)
107 x = a.tv_nsec - b.tv_nsec;
108 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
111 struct timespec
112 evNowTime() {
113 struct timeval now;
114 #ifdef CLOCK_REALTIME
115 struct timespec tsnow;
116 int m = CLOCK_REALTIME;
118 #ifdef CLOCK_MONOTONIC
119 if (__evOptMonoTime)
120 m = CLOCK_MONOTONIC;
121 #endif
122 if (clock_gettime(m, &tsnow) == 0)
123 return (tsnow);
124 #endif
125 if (gettimeofday(&now, NULL) < 0)
126 return (evConsTime(0, 0));
127 return (evTimeSpec(now));
130 struct timespec
131 evUTCTime() {
132 struct timeval now;
133 #ifdef CLOCK_REALTIME
134 struct timespec tsnow;
135 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
136 return (tsnow);
137 #endif
138 if (gettimeofday(&now, NULL) < 0)
139 return (evConsTime(0, 0));
140 return (evTimeSpec(now));
143 struct timespec
144 evLastEventTime(evContext opaqueCtx) {
145 evContext_p *ctx = opaqueCtx.opaque;
147 return (ctx->lastEventTime);
150 struct timespec
151 evTimeSpec(struct timeval tv) {
152 struct timespec ts;
154 ts.tv_sec = tv.tv_sec;
155 ts.tv_nsec = tv.tv_usec * 1000;
156 return (ts);
159 struct timeval
160 evTimeVal(struct timespec ts) {
161 struct timeval tv;
163 tv.tv_sec = ts.tv_sec;
164 tv.tv_usec = ts.tv_nsec / 1000;
165 return (tv);
169 evSetTimer(evContext opaqueCtx,
170 evTimerFunc func,
171 void *uap,
172 struct timespec due,
173 struct timespec inter,
174 evTimerID *opaqueID
176 evContext_p *ctx = opaqueCtx.opaque;
177 evTimer *id;
179 evPrintf(ctx, 1,
180 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
181 ctx, func, uap,
182 (long)due.tv_sec, due.tv_nsec,
183 (long)inter.tv_sec, inter.tv_nsec);
185 #ifdef __hpux
187 * tv_sec and tv_nsec are unsigned.
189 if (due.tv_nsec >= BILLION)
190 EV_ERR(EINVAL);
192 if (inter.tv_nsec >= BILLION)
193 EV_ERR(EINVAL);
194 #else
195 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
196 EV_ERR(EINVAL);
198 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
199 EV_ERR(EINVAL);
200 #endif
202 /* due={0,0} is a magic cookie meaning "now." */
203 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
204 due = evNowTime();
206 /* Allocate and fill. */
207 OKNEW(id);
208 id->func = func;
209 id->uap = uap;
210 id->due = due;
211 id->inter = inter;
213 if (heap_insert(ctx->timers, id) < 0)
214 return (-1);
216 /* Remember the ID if the caller provided us a place for it. */
217 if (opaqueID)
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);
225 return (0);
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);
242 return (0);
245 if (heap_element(ctx->timers, del->index) != del)
246 EV_ERR(ENOENT);
248 if (heap_delete(ctx->timers, del->index) < 0)
249 return (-1);
250 FREE(del);
252 if (ctx->debug > 7) {
253 evPrintf(ctx, 7, "timers after evClearTimer:\n");
254 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
257 return (0);
261 evConfigTimer(evContext opaqueCtx,
262 evTimerID id,
263 const char *param,
264 int value
266 evContext_p *ctx = opaqueCtx.opaque;
267 evTimer *timer = id.opaque;
268 int result=0;
270 UNUSED(value);
272 if (heap_element(ctx->timers, timer->index) != timer)
273 EV_ERR(ENOENT);
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;
279 else
280 EV_ERR(EINVAL);
282 return (result);
286 evResetTimer(evContext opaqueCtx,
287 evTimerID id,
288 evTimerFunc func,
289 void *uap,
290 struct timespec due,
291 struct timespec inter
293 evContext_p *ctx = opaqueCtx.opaque;
294 evTimer *timer = id.opaque;
295 struct timespec old_due;
296 int result=0;
298 if (heap_element(ctx->timers, timer->index) != timer)
299 EV_ERR(ENOENT);
301 #ifdef __hpux
303 * tv_sec and tv_nsec are unsigned.
305 if (due.tv_nsec >= BILLION)
306 EV_ERR(EINVAL);
308 if (inter.tv_nsec >= BILLION)
309 EV_ERR(EINVAL);
310 #else
311 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
312 EV_ERR(EINVAL);
314 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
315 EV_ERR(EINVAL);
316 #endif
318 old_due = timer->due;
320 timer->func = func;
321 timer->uap = uap;
322 timer->due = due;
323 timer->inter = inter;
325 switch (evCmpTime(due, old_due)) {
326 case -1:
327 result = heap_increased(ctx->timers, timer->index);
328 break;
329 case 0:
330 result = 0;
331 break;
332 case 1:
333 result = heap_decreased(ctx->timers, timer->index);
334 break;
337 if (ctx->debug > 7) {
338 evPrintf(ctx, 7, "timers after evResetTimer:\n");
339 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
342 return (result);
346 evSetIdleTimer(evContext opaqueCtx,
347 evTimerFunc func,
348 void *uap,
349 struct timespec max_idle,
350 evTimerID *opaqueID
352 evContext_p *ctx = opaqueCtx.opaque;
353 idle_timer *tt;
355 /* Allocate and fill. */
356 OKNEW(tt);
357 tt->func = func;
358 tt->uap = uap;
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) {
365 FREE(tt);
366 return (-1);
369 tt->timer = opaqueID->opaque;
371 return (0);
375 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
376 evTimer *del = id.opaque;
377 idle_timer *tt = del->uap;
379 FREE(tt);
380 return (evClearTimer(opaqueCtx, id));
384 evResetIdleTimer(evContext opaqueCtx,
385 evTimerID opaqueID,
386 evTimerFunc func,
387 void *uap,
388 struct timespec max_idle
390 evContext_p *ctx = opaqueCtx.opaque;
391 evTimer *timer = opaqueID.opaque;
392 idle_timer *tt = timer->uap;
394 tt->func = func;
395 tt->uap = 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),
401 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;
412 return (0);
415 /* Public to the rest of eventlib. */
417 heap_context
418 evCreateTimers(const evContext_p *ctx) {
420 UNUSED(ctx);
422 return (heap_new(due_sooner, set_index, 2048));
425 void
426 evDestroyTimers(const evContext_p *ctx) {
427 (void) heap_for_each(ctx->timers, free_timer, NULL);
428 (void) heap_free(ctx->timers);
431 /* Private. */
433 static int
434 due_sooner(void *a, void *b) {
435 evTimer *a_timer, *b_timer;
437 a_timer = a;
438 b_timer = b;
439 return (evCmpTime(a_timer->due, b_timer->due) < 0);
442 static void
443 set_index(void *what, int index) {
444 evTimer *timer;
446 timer = what;
447 timer->index = index;
450 static void
451 free_timer(void *what, void *uap) {
452 evTimer *t = what;
454 UNUSED(uap);
456 FREE(t);
459 static void
460 print_timer(void *what, void *uap) {
461 evTimer *cur = what;
462 evContext_p *ctx = uap;
464 cur = what;
465 evPrintf(ctx, 7,
466 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
467 cur->func, cur->uap,
468 (long)cur->due.tv_sec, cur->due.tv_nsec,
469 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
472 static void
473 idle_timeout(evContext opaqueCtx,
474 void *uap,
475 struct timespec due,
476 struct timespec inter
478 evContext_p *ctx = opaqueCtx.opaque;
479 idle_timer *this = uap;
480 struct timespec idle;
482 UNUSED(due);
483 UNUSED(inter);
485 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
486 if (evCmpTime(idle, this->max_idle) >= 0) {
487 (this->func)(opaqueCtx, this->uap, this->timer->due,
488 this->max_idle);
490 * Setting the interval to zero will cause the timer to
491 * be cleaned up in evDrop().
493 this->timer->inter = evConsTime(0, 0);
494 FREE(this);
495 } else {
496 /* evDrop() will reschedule the timer. */
497 this->timer->inter = evSubTime(this->max_idle, idle);
501 /*! \file */