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