ev_timers.c revision 1.9 1 1.9 christos /* $NetBSD: ev_timers.c,v 1.9 2012/03/13 21:13:38 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.6 christos static const char rcsid[] = "Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp";
28 1.2 christos #else
29 1.9 christos __RCSID("$NetBSD: ev_timers.c,v 1.9 2012/03/13 21:13:38 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.7 christos #define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
112 1.7 christos time_t s = a.tv_sec - b.tv_sec;
113 1.7 christos long n;
114 1.1 christos
115 1.7 christos if (s != 0)
116 1.7 christos return SGN(s);
117 1.7 christos
118 1.7 christos n = a.tv_nsec - b.tv_nsec;
119 1.7 christos return SGN(n);
120 1.1 christos }
121 1.1 christos
122 1.1 christos struct timespec
123 1.1 christos evNowTime() {
124 1.1 christos struct timeval now;
125 1.1 christos #ifdef CLOCK_REALTIME
126 1.1 christos struct timespec tsnow;
127 1.1 christos int m = CLOCK_REALTIME;
128 1.1 christos
129 1.1 christos #ifdef CLOCK_MONOTONIC
130 1.4 christos #ifndef _LIBC
131 1.1 christos if (__evOptMonoTime)
132 1.1 christos m = CLOCK_MONOTONIC;
133 1.1 christos #endif
134 1.4 christos #endif
135 1.1 christos if (clock_gettime(m, &tsnow) == 0)
136 1.1 christos return (tsnow);
137 1.1 christos #endif
138 1.1 christos if (gettimeofday(&now, NULL) < 0)
139 1.2 christos return (evConsTime(0L, 0L));
140 1.1 christos return (evTimeSpec(now));
141 1.1 christos }
142 1.1 christos
143 1.1 christos struct timespec
144 1.2 christos evUTCTime(void) {
145 1.1 christos struct timeval now;
146 1.1 christos #ifdef CLOCK_REALTIME
147 1.1 christos struct timespec tsnow;
148 1.1 christos if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
149 1.1 christos return (tsnow);
150 1.1 christos #endif
151 1.1 christos if (gettimeofday(&now, NULL) < 0)
152 1.2 christos return (evConsTime(0L, 0L));
153 1.1 christos return (evTimeSpec(now));
154 1.1 christos }
155 1.1 christos
156 1.2 christos #ifndef _LIBC
157 1.1 christos struct timespec
158 1.1 christos evLastEventTime(evContext opaqueCtx) {
159 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
160 1.1 christos
161 1.1 christos return (ctx->lastEventTime);
162 1.1 christos }
163 1.2 christos #endif
164 1.1 christos
165 1.1 christos struct timespec
166 1.1 christos evTimeSpec(struct timeval tv) {
167 1.1 christos struct timespec ts;
168 1.1 christos
169 1.1 christos ts.tv_sec = tv.tv_sec;
170 1.1 christos ts.tv_nsec = tv.tv_usec * 1000;
171 1.1 christos return (ts);
172 1.1 christos }
173 1.1 christos
174 1.1 christos struct timeval
175 1.1 christos evTimeVal(struct timespec ts) {
176 1.1 christos struct timeval tv;
177 1.1 christos
178 1.1 christos tv.tv_sec = ts.tv_sec;
179 1.9 christos tv.tv_usec = (suseconds_t)(ts.tv_nsec / 1000);
180 1.1 christos return (tv);
181 1.1 christos }
182 1.1 christos
183 1.2 christos #ifndef _LIBC
184 1.1 christos int
185 1.1 christos evSetTimer(evContext opaqueCtx,
186 1.1 christos evTimerFunc func,
187 1.1 christos void *uap,
188 1.1 christos struct timespec due,
189 1.1 christos struct timespec inter,
190 1.1 christos evTimerID *opaqueID
191 1.1 christos ) {
192 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
193 1.1 christos evTimer *id;
194 1.1 christos
195 1.1 christos evPrintf(ctx, 1,
196 1.1 christos "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
197 1.1 christos ctx, func, uap,
198 1.1 christos (long)due.tv_sec, due.tv_nsec,
199 1.1 christos (long)inter.tv_sec, inter.tv_nsec);
200 1.1 christos
201 1.1 christos #ifdef __hpux
202 1.1 christos /*
203 1.1 christos * tv_sec and tv_nsec are unsigned.
204 1.1 christos */
205 1.1 christos if (due.tv_nsec >= BILLION)
206 1.1 christos EV_ERR(EINVAL);
207 1.1 christos
208 1.1 christos if (inter.tv_nsec >= BILLION)
209 1.1 christos EV_ERR(EINVAL);
210 1.1 christos #else
211 1.1 christos if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
212 1.1 christos EV_ERR(EINVAL);
213 1.1 christos
214 1.1 christos if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
215 1.1 christos EV_ERR(EINVAL);
216 1.1 christos #endif
217 1.1 christos
218 1.1 christos /* due={0,0} is a magic cookie meaning "now." */
219 1.1 christos if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
220 1.1 christos due = evNowTime();
221 1.1 christos
222 1.1 christos /* Allocate and fill. */
223 1.1 christos OKNEW(id);
224 1.1 christos id->func = func;
225 1.1 christos id->uap = uap;
226 1.1 christos id->due = due;
227 1.1 christos id->inter = inter;
228 1.1 christos
229 1.1 christos if (heap_insert(ctx->timers, id) < 0)
230 1.1 christos return (-1);
231 1.1 christos
232 1.1 christos /* Remember the ID if the caller provided us a place for it. */
233 1.1 christos if (opaqueID)
234 1.1 christos opaqueID->opaque = id;
235 1.1 christos
236 1.1 christos if (ctx->debug > 7) {
237 1.1 christos evPrintf(ctx, 7, "timers after evSetTimer:\n");
238 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
239 1.1 christos }
240 1.1 christos
241 1.1 christos return (0);
242 1.1 christos }
243 1.1 christos
244 1.1 christos int
245 1.1 christos evClearTimer(evContext opaqueCtx, evTimerID id) {
246 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
247 1.1 christos evTimer *del = id.opaque;
248 1.1 christos
249 1.1 christos if (ctx->cur != NULL &&
250 1.1 christos ctx->cur->type == Timer &&
251 1.1 christos ctx->cur->u.timer.this == del) {
252 1.1 christos evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
253 1.1 christos /*
254 1.1 christos * Setting the interval to zero ensures that evDrop() will
255 1.1 christos * clean up the timer.
256 1.1 christos */
257 1.1 christos del->inter = evConsTime(0, 0);
258 1.1 christos return (0);
259 1.1 christos }
260 1.1 christos
261 1.1 christos if (heap_element(ctx->timers, del->index) != del)
262 1.1 christos EV_ERR(ENOENT);
263 1.1 christos
264 1.1 christos if (heap_delete(ctx->timers, del->index) < 0)
265 1.1 christos return (-1);
266 1.1 christos FREE(del);
267 1.1 christos
268 1.1 christos if (ctx->debug > 7) {
269 1.1 christos evPrintf(ctx, 7, "timers after evClearTimer:\n");
270 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
271 1.1 christos }
272 1.1 christos
273 1.1 christos return (0);
274 1.1 christos }
275 1.1 christos
276 1.1 christos int
277 1.1 christos evConfigTimer(evContext opaqueCtx,
278 1.1 christos evTimerID id,
279 1.1 christos const char *param,
280 1.1 christos int value
281 1.1 christos ) {
282 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
283 1.1 christos evTimer *timer = id.opaque;
284 1.1 christos int result=0;
285 1.1 christos
286 1.1 christos UNUSED(value);
287 1.1 christos
288 1.1 christos if (heap_element(ctx->timers, timer->index) != timer)
289 1.1 christos EV_ERR(ENOENT);
290 1.1 christos
291 1.1 christos if (strcmp(param, "rate") == 0)
292 1.1 christos timer->mode |= EV_TMR_RATE;
293 1.1 christos else if (strcmp(param, "interval") == 0)
294 1.1 christos timer->mode &= ~EV_TMR_RATE;
295 1.1 christos else
296 1.1 christos EV_ERR(EINVAL);
297 1.1 christos
298 1.1 christos return (result);
299 1.1 christos }
300 1.1 christos
301 1.1 christos int
302 1.1 christos evResetTimer(evContext opaqueCtx,
303 1.1 christos evTimerID id,
304 1.1 christos evTimerFunc func,
305 1.1 christos void *uap,
306 1.1 christos struct timespec due,
307 1.1 christos struct timespec inter
308 1.1 christos ) {
309 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
310 1.1 christos evTimer *timer = id.opaque;
311 1.1 christos struct timespec old_due;
312 1.1 christos int result=0;
313 1.1 christos
314 1.1 christos if (heap_element(ctx->timers, timer->index) != timer)
315 1.1 christos EV_ERR(ENOENT);
316 1.1 christos
317 1.1 christos #ifdef __hpux
318 1.1 christos /*
319 1.1 christos * tv_sec and tv_nsec are unsigned.
320 1.1 christos */
321 1.1 christos if (due.tv_nsec >= BILLION)
322 1.1 christos EV_ERR(EINVAL);
323 1.1 christos
324 1.1 christos if (inter.tv_nsec >= BILLION)
325 1.1 christos EV_ERR(EINVAL);
326 1.1 christos #else
327 1.1 christos if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
328 1.1 christos EV_ERR(EINVAL);
329 1.1 christos
330 1.1 christos if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
331 1.1 christos EV_ERR(EINVAL);
332 1.1 christos #endif
333 1.1 christos
334 1.1 christos old_due = timer->due;
335 1.1 christos
336 1.1 christos timer->func = func;
337 1.1 christos timer->uap = uap;
338 1.1 christos timer->due = due;
339 1.1 christos timer->inter = inter;
340 1.1 christos
341 1.1 christos switch (evCmpTime(due, old_due)) {
342 1.1 christos case -1:
343 1.1 christos result = heap_increased(ctx->timers, timer->index);
344 1.1 christos break;
345 1.1 christos case 0:
346 1.1 christos result = 0;
347 1.1 christos break;
348 1.1 christos case 1:
349 1.1 christos result = heap_decreased(ctx->timers, timer->index);
350 1.1 christos break;
351 1.1 christos }
352 1.1 christos
353 1.1 christos if (ctx->debug > 7) {
354 1.1 christos evPrintf(ctx, 7, "timers after evResetTimer:\n");
355 1.1 christos (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
356 1.1 christos }
357 1.1 christos
358 1.1 christos return (result);
359 1.1 christos }
360 1.1 christos
361 1.1 christos int
362 1.1 christos evSetIdleTimer(evContext opaqueCtx,
363 1.1 christos evTimerFunc func,
364 1.1 christos void *uap,
365 1.1 christos struct timespec max_idle,
366 1.1 christos evTimerID *opaqueID
367 1.1 christos ) {
368 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
369 1.1 christos idle_timer *tt;
370 1.1 christos
371 1.1 christos /* Allocate and fill. */
372 1.1 christos OKNEW(tt);
373 1.1 christos tt->func = func;
374 1.1 christos tt->uap = uap;
375 1.1 christos tt->lastTouched = ctx->lastEventTime;
376 1.1 christos tt->max_idle = max_idle;
377 1.1 christos
378 1.1 christos if (evSetTimer(opaqueCtx, idle_timeout, tt,
379 1.1 christos evAddTime(ctx->lastEventTime, max_idle),
380 1.1 christos max_idle, opaqueID) < 0) {
381 1.1 christos FREE(tt);
382 1.1 christos return (-1);
383 1.1 christos }
384 1.1 christos
385 1.1 christos tt->timer = opaqueID->opaque;
386 1.1 christos
387 1.1 christos return (0);
388 1.1 christos }
389 1.1 christos
390 1.1 christos int
391 1.1 christos evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
392 1.1 christos evTimer *del = id.opaque;
393 1.1 christos idle_timer *tt = del->uap;
394 1.1 christos
395 1.1 christos FREE(tt);
396 1.1 christos return (evClearTimer(opaqueCtx, id));
397 1.1 christos }
398 1.1 christos
399 1.1 christos int
400 1.1 christos evResetIdleTimer(evContext opaqueCtx,
401 1.1 christos evTimerID opaqueID,
402 1.1 christos evTimerFunc func,
403 1.1 christos void *uap,
404 1.1 christos struct timespec max_idle
405 1.1 christos ) {
406 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
407 1.1 christos evTimer *timer = opaqueID.opaque;
408 1.1 christos idle_timer *tt = timer->uap;
409 1.1 christos
410 1.1 christos tt->func = func;
411 1.1 christos tt->uap = uap;
412 1.1 christos tt->lastTouched = ctx->lastEventTime;
413 1.1 christos tt->max_idle = max_idle;
414 1.1 christos
415 1.1 christos return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
416 1.1 christos evAddTime(ctx->lastEventTime, max_idle),
417 1.1 christos max_idle));
418 1.1 christos }
419 1.1 christos
420 1.1 christos int
421 1.1 christos evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
422 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
423 1.1 christos evTimer *t = id.opaque;
424 1.1 christos idle_timer *tt = t->uap;
425 1.1 christos
426 1.1 christos tt->lastTouched = ctx->lastEventTime;
427 1.1 christos
428 1.1 christos return (0);
429 1.1 christos }
430 1.1 christos
431 1.1 christos /* Public to the rest of eventlib. */
432 1.1 christos
433 1.1 christos heap_context
434 1.1 christos evCreateTimers(const evContext_p *ctx) {
435 1.1 christos
436 1.1 christos UNUSED(ctx);
437 1.1 christos
438 1.1 christos return (heap_new(due_sooner, set_index, 2048));
439 1.1 christos }
440 1.1 christos
441 1.1 christos void
442 1.1 christos evDestroyTimers(const evContext_p *ctx) {
443 1.1 christos (void) heap_for_each(ctx->timers, free_timer, NULL);
444 1.1 christos (void) heap_free(ctx->timers);
445 1.1 christos }
446 1.1 christos
447 1.1 christos /* Private. */
448 1.1 christos
449 1.1 christos static int
450 1.1 christos due_sooner(void *a, void *b) {
451 1.1 christos evTimer *a_timer, *b_timer;
452 1.1 christos
453 1.1 christos a_timer = a;
454 1.1 christos b_timer = b;
455 1.1 christos return (evCmpTime(a_timer->due, b_timer->due) < 0);
456 1.1 christos }
457 1.1 christos
458 1.1 christos static void
459 1.2 christos set_index(void *what, int idx) {
460 1.1 christos evTimer *timer;
461 1.1 christos
462 1.1 christos timer = what;
463 1.2 christos timer->index = idx;
464 1.1 christos }
465 1.1 christos
466 1.1 christos static void
467 1.1 christos free_timer(void *what, void *uap) {
468 1.1 christos evTimer *t = what;
469 1.1 christos
470 1.1 christos UNUSED(uap);
471 1.1 christos
472 1.1 christos FREE(t);
473 1.1 christos }
474 1.1 christos
475 1.1 christos static void
476 1.1 christos print_timer(void *what, void *uap) {
477 1.1 christos evTimer *cur = what;
478 1.1 christos evContext_p *ctx = uap;
479 1.1 christos
480 1.1 christos cur = what;
481 1.1 christos evPrintf(ctx, 7,
482 1.1 christos " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
483 1.1 christos cur->func, cur->uap,
484 1.1 christos (long)cur->due.tv_sec, cur->due.tv_nsec,
485 1.1 christos (long)cur->inter.tv_sec, cur->inter.tv_nsec);
486 1.1 christos }
487 1.1 christos
488 1.1 christos static void
489 1.1 christos idle_timeout(evContext opaqueCtx,
490 1.1 christos void *uap,
491 1.1 christos struct timespec due,
492 1.1 christos struct timespec inter
493 1.1 christos ) {
494 1.1 christos evContext_p *ctx = opaqueCtx.opaque;
495 1.1 christos idle_timer *this = uap;
496 1.1 christos struct timespec idle;
497 1.1 christos
498 1.1 christos UNUSED(due);
499 1.1 christos UNUSED(inter);
500 1.1 christos
501 1.1 christos idle = evSubTime(ctx->lastEventTime, this->lastTouched);
502 1.1 christos if (evCmpTime(idle, this->max_idle) >= 0) {
503 1.1 christos (this->func)(opaqueCtx, this->uap, this->timer->due,
504 1.1 christos this->max_idle);
505 1.1 christos /*
506 1.1 christos * Setting the interval to zero will cause the timer to
507 1.1 christos * be cleaned up in evDrop().
508 1.1 christos */
509 1.2 christos this->timer->inter = evConsTime(0L, 0L);
510 1.1 christos FREE(this);
511 1.1 christos } else {
512 1.1 christos /* evDrop() will reschedule the timer. */
513 1.1 christos this->timer->inter = evSubTime(this->max_idle, idle);
514 1.1 christos }
515 1.1 christos }
516 1.2 christos #endif
517 1.3 christos
518 1.3 christos /*! \file */
519