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