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