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