libc: Move unused rcsid[] etc. constants into the
[dragonfly.git] / lib / libc / isc / ev_timers.c
blob9fc89150c2b0a772c7accd7e6bddee15392f13da
1 /*
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.
17 * $Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp $
20 /* ev_timers.c - implement timers for the eventlib
21 * vix 09sep95 [initial]
24 /* Import. */
26 #include "port_before.h"
27 #ifndef _LIBC
28 #include "fd_setsize.h"
29 #endif
31 #include <errno.h>
33 #ifndef _LIBC
34 #include <isc/assertions.h>
35 #endif
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. */
47 #ifdef _LIBC
48 static int __evOptMonoTime;
49 #else
50 static int due_sooner(void *, void *);
51 static void set_index(void *, int);
52 static void free_timer(void *, void *);
53 static void print_timer(void *, void *);
54 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
56 /* Private type. */
58 typedef struct {
59 evTimerFunc func;
60 void * uap;
61 struct timespec lastTouched;
62 struct timespec max_idle;
63 evTimer * timer;
64 } idle_timer;
65 #endif
66 /* Public. */
68 struct timespec
69 evConsTime(time_t sec, long nsec) {
70 struct timespec x;
72 x.tv_sec = sec;
73 x.tv_nsec = nsec;
74 return (x);
77 struct timespec
78 evAddTime(struct timespec addend1, struct timespec addend2) {
79 struct timespec x;
81 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
82 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
83 if (x.tv_nsec >= BILLION) {
84 x.tv_sec++;
85 x.tv_nsec -= BILLION;
87 return (x);
90 struct timespec
91 evSubTime(struct timespec minuend, struct timespec subtrahend) {
92 struct timespec x;
94 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
95 if (minuend.tv_nsec >= subtrahend.tv_nsec)
96 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
97 else {
98 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
99 x.tv_sec--;
101 return (x);
105 evCmpTime(struct timespec a, struct timespec b) {
106 long x = a.tv_sec - b.tv_sec;
108 if (x == 0L)
109 x = a.tv_nsec - b.tv_nsec;
110 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
113 struct timespec
114 evNowTime(void) {
115 struct timeval now;
116 #ifdef CLOCK_REALTIME
117 struct timespec tsnow;
118 int m = CLOCK_REALTIME;
120 #ifdef CLOCK_MONOTONIC
121 if (__evOptMonoTime)
122 m = CLOCK_MONOTONIC;
123 #endif
124 if (clock_gettime(m, &tsnow) == 0)
125 return (tsnow);
126 #endif
127 if (gettimeofday(&now, NULL) < 0)
128 return (evConsTime(0, 0));
129 return (evTimeSpec(now));
132 struct timespec
133 evUTCTime(void) {
134 struct timeval now;
135 #ifdef CLOCK_REALTIME
136 struct timespec tsnow;
137 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
138 return (tsnow);
139 #endif
140 if (gettimeofday(&now, NULL) < 0)
141 return (evConsTime(0, 0));
142 return (evTimeSpec(now));
145 #ifndef _LIBC
146 struct timespec
147 evLastEventTime(evContext opaqueCtx) {
148 evContext_p *ctx = opaqueCtx.opaque;
150 return (ctx->lastEventTime);
152 #endif
154 struct timespec
155 evTimeSpec(struct timeval tv) {
156 struct timespec ts;
158 ts.tv_sec = tv.tv_sec;
159 ts.tv_nsec = tv.tv_usec * 1000;
160 return (ts);
162 #if !defined(USE_KQUEUE) || !defined(_LIBC)
163 struct timeval
164 evTimeVal(struct timespec ts) {
165 struct timeval tv;
167 tv.tv_sec = ts.tv_sec;
168 tv.tv_usec = ts.tv_nsec / 1000;
169 return (tv);
171 #endif
173 #ifndef _LIBC
175 evSetTimer(evContext opaqueCtx,
176 evTimerFunc func,
177 void *uap,
178 struct timespec due,
179 struct timespec inter,
180 evTimerID *opaqueID
182 evContext_p *ctx = opaqueCtx.opaque;
183 evTimer *id;
185 evPrintf(ctx, 1,
186 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
187 ctx, func, uap,
188 (long)due.tv_sec, due.tv_nsec,
189 (long)inter.tv_sec, inter.tv_nsec);
191 #ifdef __hpux
193 * tv_sec and tv_nsec are unsigned.
195 if (due.tv_nsec >= BILLION)
196 EV_ERR(EINVAL);
198 if (inter.tv_nsec >= BILLION)
199 EV_ERR(EINVAL);
200 #else
201 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
202 EV_ERR(EINVAL);
204 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
205 EV_ERR(EINVAL);
206 #endif
208 /* due={0,0} is a magic cookie meaning "now." */
209 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
210 due = evNowTime();
212 /* Allocate and fill. */
213 OKNEW(id);
214 id->func = func;
215 id->uap = uap;
216 id->due = due;
217 id->inter = inter;
219 if (heap_insert(ctx->timers, id) < 0)
220 return (-1);
222 /* Remember the ID if the caller provided us a place for it. */
223 if (opaqueID)
224 opaqueID->opaque = id;
226 if (ctx->debug > 7) {
227 evPrintf(ctx, 7, "timers after evSetTimer:\n");
228 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
231 return (0);
235 evClearTimer(evContext opaqueCtx, evTimerID id) {
236 evContext_p *ctx = opaqueCtx.opaque;
237 evTimer *del = id.opaque;
239 if (ctx->cur != NULL &&
240 ctx->cur->type == Timer &&
241 ctx->cur->u.timer.this == del) {
242 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
244 * Setting the interval to zero ensures that evDrop() will
245 * clean up the timer.
247 del->inter = evConsTime(0, 0);
248 return (0);
251 if (heap_element(ctx->timers, del->index) != del)
252 EV_ERR(ENOENT);
254 if (heap_delete(ctx->timers, del->index) < 0)
255 return (-1);
256 FREE(del);
258 if (ctx->debug > 7) {
259 evPrintf(ctx, 7, "timers after evClearTimer:\n");
260 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
263 return (0);
267 evConfigTimer(evContext opaqueCtx,
268 evTimerID id,
269 const char *param,
270 int value
272 evContext_p *ctx = opaqueCtx.opaque;
273 evTimer *timer = id.opaque;
274 int result=0;
276 UNUSED(value);
278 if (heap_element(ctx->timers, timer->index) != timer)
279 EV_ERR(ENOENT);
281 if (strcmp(param, "rate") == 0)
282 timer->mode |= EV_TMR_RATE;
283 else if (strcmp(param, "interval") == 0)
284 timer->mode &= ~EV_TMR_RATE;
285 else
286 EV_ERR(EINVAL);
288 return (result);
292 evResetTimer(evContext opaqueCtx,
293 evTimerID id,
294 evTimerFunc func,
295 void *uap,
296 struct timespec due,
297 struct timespec inter
299 evContext_p *ctx = opaqueCtx.opaque;
300 evTimer *timer = id.opaque;
301 struct timespec old_due;
302 int result=0;
304 if (heap_element(ctx->timers, timer->index) != timer)
305 EV_ERR(ENOENT);
307 #ifdef __hpux
309 * tv_sec and tv_nsec are unsigned.
311 if (due.tv_nsec >= BILLION)
312 EV_ERR(EINVAL);
314 if (inter.tv_nsec >= BILLION)
315 EV_ERR(EINVAL);
316 #else
317 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
318 EV_ERR(EINVAL);
320 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
321 EV_ERR(EINVAL);
322 #endif
324 old_due = timer->due;
326 timer->func = func;
327 timer->uap = uap;
328 timer->due = due;
329 timer->inter = inter;
331 switch (evCmpTime(due, old_due)) {
332 case -1:
333 result = heap_increased(ctx->timers, timer->index);
334 break;
335 case 0:
336 result = 0;
337 break;
338 case 1:
339 result = heap_decreased(ctx->timers, timer->index);
340 break;
343 if (ctx->debug > 7) {
344 evPrintf(ctx, 7, "timers after evResetTimer:\n");
345 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
348 return (result);
352 evSetIdleTimer(evContext opaqueCtx,
353 evTimerFunc func,
354 void *uap,
355 struct timespec max_idle,
356 evTimerID *opaqueID
358 evContext_p *ctx = opaqueCtx.opaque;
359 idle_timer *tt;
361 /* Allocate and fill. */
362 OKNEW(tt);
363 tt->func = func;
364 tt->uap = uap;
365 tt->lastTouched = ctx->lastEventTime;
366 tt->max_idle = max_idle;
368 if (evSetTimer(opaqueCtx, idle_timeout, tt,
369 evAddTime(ctx->lastEventTime, max_idle),
370 max_idle, opaqueID) < 0) {
371 FREE(tt);
372 return (-1);
375 tt->timer = opaqueID->opaque;
377 return (0);
381 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
382 evTimer *del = id.opaque;
383 idle_timer *tt = del->uap;
385 FREE(tt);
386 return (evClearTimer(opaqueCtx, id));
390 evResetIdleTimer(evContext opaqueCtx,
391 evTimerID opaqueID,
392 evTimerFunc func,
393 void *uap,
394 struct timespec max_idle
396 evContext_p *ctx = opaqueCtx.opaque;
397 evTimer *timer = opaqueID.opaque;
398 idle_timer *tt = timer->uap;
400 tt->func = func;
401 tt->uap = uap;
402 tt->lastTouched = ctx->lastEventTime;
403 tt->max_idle = max_idle;
405 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
406 evAddTime(ctx->lastEventTime, max_idle),
407 max_idle));
411 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
412 evContext_p *ctx = opaqueCtx.opaque;
413 evTimer *t = id.opaque;
414 idle_timer *tt = t->uap;
416 tt->lastTouched = ctx->lastEventTime;
418 return (0);
421 /* Public to the rest of eventlib. */
423 heap_context
424 evCreateTimers(const evContext_p *ctx) {
426 UNUSED(ctx);
428 return (heap_new(due_sooner, set_index, 2048));
431 void
432 evDestroyTimers(const evContext_p *ctx) {
433 (void) heap_for_each(ctx->timers, free_timer, NULL);
434 (void) heap_free(ctx->timers);
437 /* Private. */
439 static int
440 due_sooner(void *a, void *b) {
441 evTimer *a_timer, *b_timer;
443 a_timer = a;
444 b_timer = b;
445 return (evCmpTime(a_timer->due, b_timer->due) < 0);
448 static void
449 set_index(void *what, int index) {
450 evTimer *timer;
452 timer = what;
453 timer->index = index;
456 static void
457 free_timer(void *what, void *uap) {
458 evTimer *t = what;
460 UNUSED(uap);
462 FREE(t);
465 static void
466 print_timer(void *what, void *uap) {
467 evTimer *cur = what;
468 evContext_p *ctx = uap;
470 cur = what;
471 evPrintf(ctx, 7,
472 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
473 cur->func, cur->uap,
474 (long)cur->due.tv_sec, cur->due.tv_nsec,
475 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
478 static void
479 idle_timeout(evContext opaqueCtx,
480 void *uap,
481 struct timespec due,
482 struct timespec inter
484 evContext_p *ctx = opaqueCtx.opaque;
485 idle_timer *this = uap;
486 struct timespec idle;
488 UNUSED(due);
489 UNUSED(inter);
491 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
492 if (evCmpTime(idle, this->max_idle) >= 0) {
493 (this->func)(opaqueCtx, this->uap, this->timer->due,
494 this->max_idle);
496 * Setting the interval to zero will cause the timer to
497 * be cleaned up in evDrop().
499 this->timer->inter = evConsTime(0, 0);
500 FREE(this);
501 } else {
502 /* evDrop() will reschedule the timer. */
503 this->timer->inter = evSubTime(this->max_idle, idle);
506 #endif /* !_LIBC */
508 /*! \file */