include: reduce default stack size
[minix.git] / lib / libc / isc / ev_timers.c
blob9a6e730db0eb859dcf1dabb3c4ae2be033e61187
1 /* $NetBSD: ev_timers.c,v 1.8 2009/04/12 17:07:17 christos Exp $ */
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 #include <sys/cdefs.h>
25 #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
26 #ifdef notdef
27 static const char rcsid[] = "Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp";
28 #else
29 __RCSID("$NetBSD: ev_timers.c,v 1.8 2009/04/12 17:07:17 christos Exp $");
30 #endif
31 #endif
33 /* Import. */
35 #include "port_before.h"
36 #include "fd_setsize.h"
38 #include <errno.h>
40 #include <isc/assertions.h>
41 #include <isc/eventlib.h>
42 #include "eventlib_p.h"
44 #include "port_after.h"
46 /* Constants. */
48 #define MILLION 1000000
49 #define BILLION 1000000000
51 /* Forward. */
53 #ifndef _LIBC
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);
60 /* Private type. */
62 typedef struct {
63 evTimerFunc func;
64 void * uap;
65 struct timespec lastTouched;
66 struct timespec max_idle;
67 evTimer * timer;
68 } idle_timer;
69 #endif
71 /* Public. */
73 struct timespec
74 evConsTime(time_t sec, long nsec) {
75 struct timespec x;
77 x.tv_sec = sec;
78 x.tv_nsec = nsec;
79 return (x);
82 struct timespec
83 evAddTime(struct timespec addend1, struct timespec addend2) {
84 struct timespec x;
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) {
89 x.tv_sec++;
90 x.tv_nsec -= BILLION;
92 return (x);
95 struct timespec
96 evSubTime(struct timespec minuend, struct timespec subtrahend) {
97 struct timespec x;
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;
102 else {
103 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
104 x.tv_sec--;
106 return (x);
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;
113 long n;
115 if (s != 0)
116 return SGN(s);
118 n = a.tv_nsec - b.tv_nsec;
119 return SGN(n);
122 struct timespec
123 evNowTime() {
124 struct timeval now;
125 #ifdef CLOCK_REALTIME
126 struct timespec tsnow;
127 int m = CLOCK_REALTIME;
129 #ifdef CLOCK_MONOTONIC
130 #ifndef _LIBC
131 if (__evOptMonoTime)
132 m = CLOCK_MONOTONIC;
133 #endif
134 #endif
135 if (clock_gettime(m, &tsnow) == 0)
136 return (tsnow);
137 #endif
138 if (gettimeofday(&now, NULL) < 0)
139 return (evConsTime(0L, 0L));
140 return (evTimeSpec(now));
143 struct timespec
144 evUTCTime(void) {
145 struct timeval now;
146 #ifdef CLOCK_REALTIME
147 struct timespec tsnow;
148 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
149 return (tsnow);
150 #endif
151 if (gettimeofday(&now, NULL) < 0)
152 return (evConsTime(0L, 0L));
153 return (evTimeSpec(now));
156 #ifndef _LIBC
157 struct timespec
158 evLastEventTime(evContext opaqueCtx) {
159 evContext_p *ctx = opaqueCtx.opaque;
161 return (ctx->lastEventTime);
163 #endif
165 struct timespec
166 evTimeSpec(struct timeval tv) {
167 struct timespec ts;
169 ts.tv_sec = tv.tv_sec;
170 ts.tv_nsec = tv.tv_usec * 1000;
171 return (ts);
174 struct timeval
175 evTimeVal(struct timespec ts) {
176 struct timeval tv;
178 tv.tv_sec = ts.tv_sec;
179 tv.tv_usec = ts.tv_nsec / 1000;
180 return (tv);
183 #ifndef _LIBC
185 evSetTimer(evContext opaqueCtx,
186 evTimerFunc func,
187 void *uap,
188 struct timespec due,
189 struct timespec inter,
190 evTimerID *opaqueID
192 evContext_p *ctx = opaqueCtx.opaque;
193 evTimer *id;
195 evPrintf(ctx, 1,
196 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
197 ctx, func, uap,
198 (long)due.tv_sec, due.tv_nsec,
199 (long)inter.tv_sec, inter.tv_nsec);
201 #ifdef __hpux
203 * tv_sec and tv_nsec are unsigned.
205 if (due.tv_nsec >= BILLION)
206 EV_ERR(EINVAL);
208 if (inter.tv_nsec >= BILLION)
209 EV_ERR(EINVAL);
210 #else
211 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
212 EV_ERR(EINVAL);
214 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
215 EV_ERR(EINVAL);
216 #endif
218 /* due={0,0} is a magic cookie meaning "now." */
219 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
220 due = evNowTime();
222 /* Allocate and fill. */
223 OKNEW(id);
224 id->func = func;
225 id->uap = uap;
226 id->due = due;
227 id->inter = inter;
229 if (heap_insert(ctx->timers, id) < 0)
230 return (-1);
232 /* Remember the ID if the caller provided us a place for it. */
233 if (opaqueID)
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);
241 return (0);
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);
258 return (0);
261 if (heap_element(ctx->timers, del->index) != del)
262 EV_ERR(ENOENT);
264 if (heap_delete(ctx->timers, del->index) < 0)
265 return (-1);
266 FREE(del);
268 if (ctx->debug > 7) {
269 evPrintf(ctx, 7, "timers after evClearTimer:\n");
270 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
273 return (0);
277 evConfigTimer(evContext opaqueCtx,
278 evTimerID id,
279 const char *param,
280 int value
282 evContext_p *ctx = opaqueCtx.opaque;
283 evTimer *timer = id.opaque;
284 int result=0;
286 UNUSED(value);
288 if (heap_element(ctx->timers, timer->index) != timer)
289 EV_ERR(ENOENT);
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;
295 else
296 EV_ERR(EINVAL);
298 return (result);
302 evResetTimer(evContext opaqueCtx,
303 evTimerID id,
304 evTimerFunc func,
305 void *uap,
306 struct timespec due,
307 struct timespec inter
309 evContext_p *ctx = opaqueCtx.opaque;
310 evTimer *timer = id.opaque;
311 struct timespec old_due;
312 int result=0;
314 if (heap_element(ctx->timers, timer->index) != timer)
315 EV_ERR(ENOENT);
317 #ifdef __hpux
319 * tv_sec and tv_nsec are unsigned.
321 if (due.tv_nsec >= BILLION)
322 EV_ERR(EINVAL);
324 if (inter.tv_nsec >= BILLION)
325 EV_ERR(EINVAL);
326 #else
327 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
328 EV_ERR(EINVAL);
330 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
331 EV_ERR(EINVAL);
332 #endif
334 old_due = timer->due;
336 timer->func = func;
337 timer->uap = uap;
338 timer->due = due;
339 timer->inter = inter;
341 switch (evCmpTime(due, old_due)) {
342 case -1:
343 result = heap_increased(ctx->timers, timer->index);
344 break;
345 case 0:
346 result = 0;
347 break;
348 case 1:
349 result = heap_decreased(ctx->timers, timer->index);
350 break;
353 if (ctx->debug > 7) {
354 evPrintf(ctx, 7, "timers after evResetTimer:\n");
355 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
358 return (result);
362 evSetIdleTimer(evContext opaqueCtx,
363 evTimerFunc func,
364 void *uap,
365 struct timespec max_idle,
366 evTimerID *opaqueID
368 evContext_p *ctx = opaqueCtx.opaque;
369 idle_timer *tt;
371 /* Allocate and fill. */
372 OKNEW(tt);
373 tt->func = func;
374 tt->uap = uap;
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) {
381 FREE(tt);
382 return (-1);
385 tt->timer = opaqueID->opaque;
387 return (0);
391 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
392 evTimer *del = id.opaque;
393 idle_timer *tt = del->uap;
395 FREE(tt);
396 return (evClearTimer(opaqueCtx, id));
400 evResetIdleTimer(evContext opaqueCtx,
401 evTimerID opaqueID,
402 evTimerFunc func,
403 void *uap,
404 struct timespec max_idle
406 evContext_p *ctx = opaqueCtx.opaque;
407 evTimer *timer = opaqueID.opaque;
408 idle_timer *tt = timer->uap;
410 tt->func = func;
411 tt->uap = 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),
417 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;
428 return (0);
431 /* Public to the rest of eventlib. */
433 heap_context
434 evCreateTimers(const evContext_p *ctx) {
436 UNUSED(ctx);
438 return (heap_new(due_sooner, set_index, 2048));
441 void
442 evDestroyTimers(const evContext_p *ctx) {
443 (void) heap_for_each(ctx->timers, free_timer, NULL);
444 (void) heap_free(ctx->timers);
447 /* Private. */
449 static int
450 due_sooner(void *a, void *b) {
451 evTimer *a_timer, *b_timer;
453 a_timer = a;
454 b_timer = b;
455 return (evCmpTime(a_timer->due, b_timer->due) < 0);
458 static void
459 set_index(void *what, int idx) {
460 evTimer *timer;
462 timer = what;
463 timer->index = idx;
466 static void
467 free_timer(void *what, void *uap) {
468 evTimer *t = what;
470 UNUSED(uap);
472 FREE(t);
475 static void
476 print_timer(void *what, void *uap) {
477 evTimer *cur = what;
478 evContext_p *ctx = uap;
480 cur = what;
481 evPrintf(ctx, 7,
482 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
483 cur->func, cur->uap,
484 (long)cur->due.tv_sec, cur->due.tv_nsec,
485 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
488 static void
489 idle_timeout(evContext opaqueCtx,
490 void *uap,
491 struct timespec due,
492 struct timespec inter
494 evContext_p *ctx = opaqueCtx.opaque;
495 idle_timer *this = uap;
496 struct timespec idle;
498 UNUSED(due);
499 UNUSED(inter);
501 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
502 if (evCmpTime(idle, this->max_idle) >= 0) {
503 (this->func)(opaqueCtx, this->uap, this->timer->due,
504 this->max_idle);
506 * Setting the interval to zero will cause the timer to
507 * be cleaned up in evDrop().
509 this->timer->inter = evConsTime(0L, 0L);
510 FREE(this);
511 } else {
512 /* evDrop() will reschedule the timer. */
513 this->timer->inter = evSubTime(this->max_idle, idle);
516 #endif
518 /*! \file */