xref: /netbsd-src/lib/libc/isc/ev_timers.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$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
27 static 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 "port_before.h"
36 #include "fd_setsize.h"
37 
38 #include <errno.h>
39 
40 #include <isc/assertions.h>
41 #include <isc/eventlib.h>
42 #include "eventlib_p.h"
43 
44 #include "port_after.h"
45 
46 /* Constants. */
47 
48 #define	MILLION 1000000
49 #define BILLION 1000000000
50 
51 /* Forward. */
52 
53 #ifndef _LIBC
54 static int due_sooner(void *, void *);
55 static void set_index(void *, int);
56 static void free_timer(void *, void *);
57 static void print_timer(void *, void *);
58 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
59 
60 /* Private type. */
61 
62 typedef struct {
63 	evTimerFunc	func;
64 	void *		uap;
65 	struct timespec	lastTouched;
66 	struct timespec	max_idle;
67 	evTimer *	timer;
68 } idle_timer;
69 #endif
70 
71 /* Public. */
72 
73 struct timespec
74 evConsTime(time_t sec, long nsec) {
75 	struct timespec x;
76 
77 	x.tv_sec = sec;
78 	x.tv_nsec = nsec;
79 	return (x);
80 }
81 
82 struct timespec
83 evAddTime(struct timespec addend1, struct timespec addend2) {
84 	struct timespec x;
85 
86 	x.tv_sec = addend1.tv_sec + addend2.tv_sec;
87 	x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
88 	if (x.tv_nsec >= BILLION) {
89 		x.tv_sec++;
90 		x.tv_nsec -= BILLION;
91 	}
92 	return (x);
93 }
94 
95 struct timespec
96 evSubTime(struct timespec minuend, struct timespec subtrahend) {
97 	struct timespec x;
98 
99 	x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
100 	if (minuend.tv_nsec >= subtrahend.tv_nsec)
101 		x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
102 	else {
103 		x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
104 		x.tv_sec--;
105 	}
106 	return (x);
107 }
108 
109 int
110 evCmpTime(struct timespec a, struct timespec b) {
111 	long x = a.tv_sec - b.tv_sec;
112 
113 	if (x == 0L)
114 		x = a.tv_nsec - b.tv_nsec;
115 	return (x < 0L ? (-1) : x > 0L ? (1) : (0));
116 }
117 
118 struct timespec
119 evNowTime() {
120 	struct timeval now;
121 #ifdef CLOCK_REALTIME
122 	struct timespec tsnow;
123 	int m = CLOCK_REALTIME;
124 
125 #ifdef CLOCK_MONOTONIC
126 	if (__evOptMonoTime)
127 		m = CLOCK_MONOTONIC;
128 #endif
129 	if (clock_gettime(m, &tsnow) == 0)
130 		return (tsnow);
131 #endif
132 	if (gettimeofday(&now, NULL) < 0)
133 		return (evConsTime(0L, 0L));
134 	return (evTimeSpec(now));
135 }
136 
137 struct timespec
138 evUTCTime(void) {
139 	struct timeval now;
140 #ifdef CLOCK_REALTIME
141 	struct timespec tsnow;
142 	if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
143 		return (tsnow);
144 #endif
145 	if (gettimeofday(&now, NULL) < 0)
146 		return (evConsTime(0L, 0L));
147 	return (evTimeSpec(now));
148 }
149 
150 #ifndef _LIBC
151 struct timespec
152 evLastEventTime(evContext opaqueCtx) {
153 	evContext_p *ctx = opaqueCtx.opaque;
154 
155 	return (ctx->lastEventTime);
156 }
157 #endif
158 
159 struct timespec
160 evTimeSpec(struct timeval tv) {
161 	struct timespec ts;
162 
163 	ts.tv_sec = tv.tv_sec;
164 	ts.tv_nsec = tv.tv_usec * 1000;
165 	return (ts);
166 }
167 
168 struct timeval
169 evTimeVal(struct timespec ts) {
170 	struct timeval tv;
171 
172 	tv.tv_sec = ts.tv_sec;
173 	tv.tv_usec = ts.tv_nsec / 1000;
174 	return (tv);
175 }
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 idx) {
454 	evTimer *timer;
455 
456 	timer = what;
457 	timer->index = idx;
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(0L, 0L);
504 		FREE(this);
505 	} else {
506 		/* evDrop() will reschedule the timer. */
507 		this->timer->inter = evSubTime(this->max_idle, idle);
508 	}
509 }
510 #endif
511