ev_timers.c revision 1.3 1 1.3 christos /* $NetBSD: ev_timers.c,v 1.3 2007/01/27 22:26:43 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1995-1999 by Internet Software Consortium
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos /* ev_timers.c - implement timers for the eventlib
21 1.1 christos * vix 09sep95 [initial]
22 1.1 christos */
23 1.1 christos
24 1.2 christos #include <sys/cdefs.h>
25 1.2 christos #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
26 1.2 christos #ifdef notdef
27 1.3 christos static const char rcsid[] = "Id: ev_timers.c,v 1.5.18.1 2005/04/27 05:01:06 sra Exp";
28 1.2 christos #else
29 1.3 christos __RCSID("$NetBSD: ev_timers.c,v 1.3 2007/01/27 22:26:43 christos Exp $");
30 1.2 christos #endif
31 1.1 christos #endif
32 1.1 christos
33 1.1 christos /* Import. */
34 1.1 christos
35 1.1 christos #include "port_before.h"
36 1.1 christos #include "fd_setsize.h"
37 1.1 christos
38 1.1 christos #include <errno.h>
39 1.1 christos
40 1.1 christos #include <isc/assertions.h>
41 1.1 christos #include <isc/eventlib.h>
42 1.1 christos #include "eventlib_p.h"
43 1.1 christos
44 1.1 christos #include "port_after.h"
45 1.1 christos
46 1.1 christos /* Constants. */
47 1.1 christos
48 1.1 christos #define MILLION 1000000
49 1.1 christos #define BILLION 1000000000
50 1.1 christos
51 1.1 christos /* Forward. */
52 1.1 christos
53 1.2 christos #ifndef _LIBC
54 1.1 christos static int due_sooner(void *, void *);
55 1.1 christos static void set_index(void *, int);
56 1.1 christos static void free_timer(void *, void *);
57 1.1 christos static void print_timer(void *, void *);
58 1.1 christos static void idle_timeout(evContext, void *, struct timespec, struct timespec);
59 1.1 christos
60 1.1 christos /* Private type. */
61 1.1 christos
62 1.1 christos typedef struct {
63 1.1 christos evTimerFunc func;
64 1.1 christos void * uap;
65 1.1 christos struct timespec lastTouched;
66 1.1 christos struct timespec max_idle;
67 1.1 christos evTimer * timer;
68 1.1 christos } idle_timer;
69 1.2 christos #endif
70 1.1 christos
71 1.1 christos /* Public. */
72 1.1 christos
73 1.1 christos struct timespec
74 1.1 christos evConsTime(time_t sec, long nsec) {
75 1.1 christos struct timespec x;
76 1.1 christos
77 1.1 christos x.tv_sec = sec;
78 1.1 christos x.tv_nsec = nsec;
79 1.1 christos return (x);
80 1.1 christos }
81 1.1 christos
82 1.1 christos struct timespec
83 1.1 christos evAddTime(struct timespec addend1, struct timespec addend2) {
84 1.1 christos struct timespec x;
85 1.1 christos
86 1.1 christos x.tv_sec = addend1.tv_sec + addend2.tv_sec;
87 1.1 christos x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
88 1.1 christos if (x.tv_nsec >= BILLION) {
89 1.1 christos x.tv_sec++;
90 1.1 christos x.tv_nsec -= BILLION;
91 1.1 christos }
92 1.1 christos return (x);
93 1.1 christos }
94 1.1 christos
95 1.1 christos struct timespec
96 1.1 christos evSubTime(struct timespec minuend, struct timespec subtrahend) {
97 1.1 christos struct timespec x;
98 1.1 christos
99 1.1 christos x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
100 1.1 christos if (minuend.tv_nsec >= subtrahend.tv_nsec)
101 1.1 christos x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
102 1.1 christos else {
103 1.1 christos x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
104 1.1 christos x.tv_sec--;
105 1.1 christos }
106 1.1 christos return (x);
107 1.1 christos }
108 1.1 christos
109 1.1 christos int
110 1.1 christos evCmpTime(struct timespec a, struct timespec b) {
111 1.1 christos long x = a.tv_sec - b.tv_sec;
112 1.1 christos
113 1.1 christos if (x == 0L)
114 1.1 christos x = a.tv_nsec - b.tv_nsec;
115 1.1 christos return (x < 0L ? (-1) : x > 0L ? (1) : (0));
116 1.1 christos }
117 1.1 christos
118 1.1 christos struct timespec
119 1.1 christos evNowTime() {
120 1.1 christos struct timeval now;
121 1.1 christos #ifdef CLOCK_REALTIME
122 1.1 christos struct timespec tsnow;
123 1.1 christos int m = CLOCK_REALTIME;
124 1.1 christos
125 1.1 christos #ifdef CLOCK_MONOTONIC
126 1.1 christos if (__evOptMonoTime)
127 1.1 christos m = CLOCK_MONOTONIC;
128 1.1 christos #endif
129 1.1 christos if (clock_gettime(m, &tsnow) == 0)
130 1.1 christos return (tsnow);
131 1.1 christos #endif
132 1.1 christos if (gettimeofday(&now, NULL) < 0)
133 1.2 christos return (evConsTime(0L, 0L));
134 1.1 christos return (evTimeSpec(now));
135 1.1 christos }
136 1.1 christos
137 1.1 christos struct timespec
138 1.2 christos evUTCTime(void) {
139 1.1 christos struct timeval now;
140 1.1 christos #ifdef CLOCK_REALTIME
141 1.1 christos struct timespec tsnow;
142 1.1 christos if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
143 1.1 christos return (tsnow);
144 1.1 christos #endif
145 1.1 christos if (gettimeofday(&now, NULL) < 0)
146 1.2 christos return (evConsTime(0L, 0L));
147 1.1 christos return (evTimeSpec(now));
148 1.1 christos }
149 1.1 christos
150 1.2 christos #ifndef _LIBC
151 1.1 christos struct timespec
152 1.1 christos evLastEventTime(evContext opaqueCtx) {
153 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
154 1.1 christos
155 1.1 christos return (ctx->lastEventTime);
156 1.1 christos }
157 1.2 christos #endif
158 1.1 christos
159 1.1 christos struct timespec
160 1.1 christos evTimeSpec(struct timeval tv) {
161 1.1 christos struct timespec ts;
162 1.1 christos
163 1.1 christos ts.tv_sec = tv.tv_sec;
164 1.1 christos ts.tv_nsec = tv.tv_usec * 1000;
165 1.1 christos return (ts);
166 1.1 christos }
167 1.1 christos
168 1.1 christos struct timeval
169 1.1 christos evTimeVal(struct timespec ts) {
170 1.1 christos struct timeval tv;
171 1.1 christos
172 1.1 christos tv.tv_sec = ts.tv_sec;
173 1.1 christos tv.tv_usec = ts.tv_nsec / 1000;
174 1.1 christos return (tv);
175 1.1 christos }
176 1.1 christos
177 1.2 christos #ifndef _LIBC
178 1.1 christos int
179 1.1 christos evSetTimer(evContext opaqueCtx,
180 1.1 christos evTimerFunc func,
181 1.1 christos void *uap,
182 1.1 christos struct timespec due,
183 1.1 christos struct timespec inter,
184 1.1 christos evTimerID *opaqueID
185 1.1 christos ) {
186 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
187 1.1 christos evTimer *id;
188 1.1 christos
189 1.1 christos evPrintf(ctx, 1,
190 1.1 christos "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
191 1.1 christos ctx, func, uap,
192 1.1 christos (long)due.tv_sec, due.tv_nsec,
193 1.1 christos (long)inter.tv_sec, inter.tv_nsec);
194 1.1 christos
195 1.1 christos #ifdef __hpux
196 1.1 christos /*
197 1.1 christos * tv_sec and tv_nsec are unsigned.
198 1.1 christos */
199 1.1 christos if (due.tv_nsec >= BILLION)
200 1.1 christos EV_ERR(EINVAL);
201 1.1 christos
202 1.1 christos if (inter.tv_nsec >= BILLION)
203 1.1 christos EV_ERR(EINVAL);
204 1.1 christos #else
205 1.1 christos if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
206 1.1 christos EV_ERR(EINVAL);
207 1.1 christos
208 1.1 christos if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
209 1.1 christos EV_ERR(EINVAL);
210 1.1 christos #endif
211 1.1 christos
212 1.1 christos /* due={0,0} is a magic cookie meaning "now." */
213 1.1 christos if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
214 1.1 christos due = evNowTime();
215 1.1 christos
216 1.1 christos /* Allocate and fill. */
217 1.1 christos OKNEW(id);
218 1.1 christos id->func = func;
219 1.1 christos id->uap = uap;
220 1.1 christos id->due = due;
221 1.1 christos id->inter = inter;
222 1.1 christos
223 1.1 christos if (heap_insert(ctx->timers, id) < 0)
224 1.1 christos return (-1);
225 1.1 christos
226 1.1 christos /* Remember the ID if the caller provided us a place for it. */
227 1.1 christos if (opaqueID)
228 1.1 christos opaqueID->opaque = id;
229 1.1 christos
230 1.1 christos if (ctx->debug > 7) {
231 1.1 christos evPrintf(ctx, 7, "timers after evSetTimer:\n");
232 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
233 1.1 christos }
234 1.1 christos
235 1.1 christos return (0);
236 1.1 christos }
237 1.1 christos
238 1.1 christos int
239 1.1 christos evClearTimer(evContext opaqueCtx, evTimerID id) {
240 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
241 1.1 christos evTimer *del = id.opaque;
242 1.1 christos
243 1.1 christos if (ctx->cur != NULL &&
244 1.1 christos ctx->cur->type == Timer &&
245 1.1 christos ctx->cur->u.timer.this == del) {
246 1.1 christos evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
247 1.1 christos /*
248 1.1 christos * Setting the interval to zero ensures that evDrop() will
249 1.1 christos * clean up the timer.
250 1.1 christos */
251 1.1 christos del->inter = evConsTime(0, 0);
252 1.1 christos return (0);
253 1.1 christos }
254 1.1 christos
255 1.1 christos if (heap_element(ctx->timers, del->index) != del)
256 1.1 christos EV_ERR(ENOENT);
257 1.1 christos
258 1.1 christos if (heap_delete(ctx->timers, del->index) < 0)
259 1.1 christos return (-1);
260 1.1 christos FREE(del);
261 1.1 christos
262 1.1 christos if (ctx->debug > 7) {
263 1.1 christos evPrintf(ctx, 7, "timers after evClearTimer:\n");
264 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
265 1.1 christos }
266 1.1 christos
267 1.1 christos return (0);
268 1.1 christos }
269 1.1 christos
270 1.1 christos int
271 1.1 christos evConfigTimer(evContext opaqueCtx,
272 1.1 christos evTimerID id,
273 1.1 christos const char *param,
274 1.1 christos int value
275 1.1 christos ) {
276 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
277 1.1 christos evTimer *timer = id.opaque;
278 1.1 christos int result=0;
279 1.1 christos
280 1.1 christos UNUSED(value);
281 1.1 christos
282 1.1 christos if (heap_element(ctx->timers, timer->index) != timer)
283 1.1 christos EV_ERR(ENOENT);
284 1.1 christos
285 1.1 christos if (strcmp(param, "rate") == 0)
286 1.1 christos timer->mode |= EV_TMR_RATE;
287 1.1 christos else if (strcmp(param, "interval") == 0)
288 1.1 christos timer->mode &= ~EV_TMR_RATE;
289 1.1 christos else
290 1.1 christos EV_ERR(EINVAL);
291 1.1 christos
292 1.1 christos return (result);
293 1.1 christos }
294 1.1 christos
295 1.1 christos int
296 1.1 christos evResetTimer(evContext opaqueCtx,
297 1.1 christos evTimerID id,
298 1.1 christos evTimerFunc func,
299 1.1 christos void *uap,
300 1.1 christos struct timespec due,
301 1.1 christos struct timespec inter
302 1.1 christos ) {
303 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
304 1.1 christos evTimer *timer = id.opaque;
305 1.1 christos struct timespec old_due;
306 1.1 christos int result=0;
307 1.1 christos
308 1.1 christos if (heap_element(ctx->timers, timer->index) != timer)
309 1.1 christos EV_ERR(ENOENT);
310 1.1 christos
311 1.1 christos #ifdef __hpux
312 1.1 christos /*
313 1.1 christos * tv_sec and tv_nsec are unsigned.
314 1.1 christos */
315 1.1 christos if (due.tv_nsec >= BILLION)
316 1.1 christos EV_ERR(EINVAL);
317 1.1 christos
318 1.1 christos if (inter.tv_nsec >= BILLION)
319 1.1 christos EV_ERR(EINVAL);
320 1.1 christos #else
321 1.1 christos if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
322 1.1 christos EV_ERR(EINVAL);
323 1.1 christos
324 1.1 christos if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
325 1.1 christos EV_ERR(EINVAL);
326 1.1 christos #endif
327 1.1 christos
328 1.1 christos old_due = timer->due;
329 1.1 christos
330 1.1 christos timer->func = func;
331 1.1 christos timer->uap = uap;
332 1.1 christos timer->due = due;
333 1.1 christos timer->inter = inter;
334 1.1 christos
335 1.1 christos switch (evCmpTime(due, old_due)) {
336 1.1 christos case -1:
337 1.1 christos result = heap_increased(ctx->timers, timer->index);
338 1.1 christos break;
339 1.1 christos case 0:
340 1.1 christos result = 0;
341 1.1 christos break;
342 1.1 christos case 1:
343 1.1 christos result = heap_decreased(ctx->timers, timer->index);
344 1.1 christos break;
345 1.1 christos }
346 1.1 christos
347 1.1 christos if (ctx->debug > 7) {
348 1.1 christos evPrintf(ctx, 7, "timers after evResetTimer:\n");
349 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
350 1.1 christos }
351 1.1 christos
352 1.1 christos return (result);
353 1.1 christos }
354 1.1 christos
355 1.1 christos int
356 1.1 christos evSetIdleTimer(evContext opaqueCtx,
357 1.1 christos evTimerFunc func,
358 1.1 christos void *uap,
359 1.1 christos struct timespec max_idle,
360 1.1 christos evTimerID *opaqueID
361 1.1 christos ) {
362 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
363 1.1 christos idle_timer *tt;
364 1.1 christos
365 1.1 christos /* Allocate and fill. */
366 1.1 christos OKNEW(tt);
367 1.1 christos tt->func = func;
368 1.1 christos tt->uap = uap;
369 1.1 christos tt->lastTouched = ctx->lastEventTime;
370 1.1 christos tt->max_idle = max_idle;
371 1.1 christos
372 1.1 christos if (evSetTimer(opaqueCtx, idle_timeout, tt,
373 1.1 christos evAddTime(ctx->lastEventTime, max_idle),
374 1.1 christos max_idle, opaqueID) < 0) {
375 1.1 christos FREE(tt);
376 1.1 christos return (-1);
377 1.1 christos }
378 1.1 christos
379 1.1 christos tt->timer = opaqueID->opaque;
380 1.1 christos
381 1.1 christos return (0);
382 1.1 christos }
383 1.1 christos
384 1.1 christos int
385 1.1 christos evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
386 1.1 christos evTimer *del = id.opaque;
387 1.1 christos idle_timer *tt = del->uap;
388 1.1 christos
389 1.1 christos FREE(tt);
390 1.1 christos return (evClearTimer(opaqueCtx, id));
391 1.1 christos }
392 1.1 christos
393 1.1 christos int
394 1.1 christos evResetIdleTimer(evContext opaqueCtx,
395 1.1 christos evTimerID opaqueID,
396 1.1 christos evTimerFunc func,
397 1.1 christos void *uap,
398 1.1 christos struct timespec max_idle
399 1.1 christos ) {
400 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
401 1.1 christos evTimer *timer = opaqueID.opaque;
402 1.1 christos idle_timer *tt = timer->uap;
403 1.1 christos
404 1.1 christos tt->func = func;
405 1.1 christos tt->uap = uap;
406 1.1 christos tt->lastTouched = ctx->lastEventTime;
407 1.1 christos tt->max_idle = max_idle;
408 1.1 christos
409 1.1 christos return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
410 1.1 christos evAddTime(ctx->lastEventTime, max_idle),
411 1.1 christos max_idle));
412 1.1 christos }
413 1.1 christos
414 1.1 christos int
415 1.1 christos evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
416 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
417 1.1 christos evTimer *t = id.opaque;
418 1.1 christos idle_timer *tt = t->uap;
419 1.1 christos
420 1.1 christos tt->lastTouched = ctx->lastEventTime;
421 1.1 christos
422 1.1 christos return (0);
423 1.1 christos }
424 1.1 christos
425 1.1 christos /* Public to the rest of eventlib. */
426 1.1 christos
427 1.1 christos heap_context
428 1.1 christos evCreateTimers(const evContext_p *ctx) {
429 1.1 christos
430 1.1 christos UNUSED(ctx);
431 1.1 christos
432 1.1 christos return (heap_new(due_sooner, set_index, 2048));
433 1.1 christos }
434 1.1 christos
435 1.1 christos void
436 1.1 christos evDestroyTimers(const evContext_p *ctx) {
437 1.1 christos (void) heap_for_each(ctx->timers, free_timer, NULL);
438 1.1 christos (void) heap_free(ctx->timers);
439 1.1 christos }
440 1.1 christos
441 1.1 christos /* Private. */
442 1.1 christos
443 1.1 christos static int
444 1.1 christos due_sooner(void *a, void *b) {
445 1.1 christos evTimer *a_timer, *b_timer;
446 1.1 christos
447 1.1 christos a_timer = a;
448 1.1 christos b_timer = b;
449 1.1 christos return (evCmpTime(a_timer->due, b_timer->due) < 0);
450 1.1 christos }
451 1.1 christos
452 1.1 christos static void
453 1.2 christos set_index(void *what, int idx) {
454 1.1 christos evTimer *timer;
455 1.1 christos
456 1.1 christos timer = what;
457 1.2 christos timer->index = idx;
458 1.1 christos }
459 1.1 christos
460 1.1 christos static void
461 1.1 christos free_timer(void *what, void *uap) {
462 1.1 christos evTimer *t = what;
463 1.1 christos
464 1.1 christos UNUSED(uap);
465 1.1 christos
466 1.1 christos FREE(t);
467 1.1 christos }
468 1.1 christos
469 1.1 christos static void
470 1.1 christos print_timer(void *what, void *uap) {
471 1.1 christos evTimer *cur = what;
472 1.1 christos evContext_p *ctx = uap;
473 1.1 christos
474 1.1 christos cur = what;
475 1.1 christos evPrintf(ctx, 7,
476 1.1 christos " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
477 1.1 christos cur->func, cur->uap,
478 1.1 christos (long)cur->due.tv_sec, cur->due.tv_nsec,
479 1.1 christos (long)cur->inter.tv_sec, cur->inter.tv_nsec);
480 1.1 christos }
481 1.1 christos
482 1.1 christos static void
483 1.1 christos idle_timeout(evContext opaqueCtx,
484 1.1 christos void *uap,
485 1.1 christos struct timespec due,
486 1.1 christos struct timespec inter
487 1.1 christos ) {
488 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
489 1.1 christos idle_timer *this = uap;
490 1.1 christos struct timespec idle;
491 1.1 christos
492 1.1 christos UNUSED(due);
493 1.1 christos UNUSED(inter);
494 1.1 christos
495 1.1 christos idle = evSubTime(ctx->lastEventTime, this->lastTouched);
496 1.1 christos if (evCmpTime(idle, this->max_idle) >= 0) {
497 1.1 christos (this->func)(opaqueCtx, this->uap, this->timer->due,
498 1.1 christos this->max_idle);
499 1.1 christos /*
500 1.1 christos * Setting the interval to zero will cause the timer to
501 1.1 christos * be cleaned up in evDrop().
502 1.1 christos */
503 1.2 christos this->timer->inter = evConsTime(0L, 0L);
504 1.1 christos FREE(this);
505 1.1 christos } else {
506 1.1 christos /* evDrop() will reschedule the timer. */
507 1.1 christos this->timer->inter = evSubTime(this->max_idle, idle);
508 1.1 christos }
509 1.1 christos }
510 1.2 christos #endif
511 1.3 christos
512 1.3 christos /*! \file */
513