1156952Sume/*
2156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3156952Sume * Copyright (c) 1995-1999 by Internet Software Consortium
4156952Sume *
5156952Sume * Permission to use, copy, modify, and distribute this software for any
6156952Sume * purpose with or without fee is hereby granted, provided that the above
7156952Sume * copyright notice and this permission notice appear in all copies.
8156952Sume *
9156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16156952Sume */
17156952Sume
18156952Sume/* ev_timers.c - implement timers for the eventlib
19156952Sume * vix 09sep95 [initial]
20156952Sume */
21156952Sume
22156952Sume#if !defined(LINT) && !defined(CODECENTER)
23269867Sumestatic const char rcsid[] = "$Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp $";
24156952Sume#endif
25156956Sume#include <sys/cdefs.h>
26156956Sume__FBSDID("$FreeBSD$");
27156952Sume
28156952Sume/* Import. */
29156952Sume
30156952Sume#include "port_before.h"
31156956Sume#ifndef _LIBC
32156952Sume#include "fd_setsize.h"
33156956Sume#endif
34156952Sume
35156952Sume#include <errno.h>
36156952Sume
37156956Sume#ifndef _LIBC
38156952Sume#include <isc/assertions.h>
39156956Sume#endif
40156952Sume#include <isc/eventlib.h>
41156952Sume#include "eventlib_p.h"
42156952Sume
43156952Sume#include "port_after.h"
44156952Sume
45156952Sume/* Constants. */
46156952Sume
47156952Sume#define	MILLION 1000000
48156952Sume#define BILLION 1000000000
49156952Sume
50156952Sume/* Forward. */
51156952Sume
52156956Sume#ifdef _LIBC
53156956Sumestatic int	__evOptMonoTime;
54156956Sume#else
55156952Sumestatic int due_sooner(void *, void *);
56156952Sumestatic void set_index(void *, int);
57156952Sumestatic void free_timer(void *, void *);
58156952Sumestatic void print_timer(void *, void *);
59156952Sumestatic void idle_timeout(evContext, void *, struct timespec, struct timespec);
60156952Sume
61156952Sume/* Private type. */
62156952Sume
63156952Sumetypedef struct {
64156952Sume	evTimerFunc	func;
65156952Sume	void *		uap;
66156952Sume	struct timespec	lastTouched;
67156952Sume	struct timespec	max_idle;
68156952Sume	evTimer *	timer;
69156952Sume} idle_timer;
70156956Sume#endif
71156952Sume
72156952Sume/* Public. */
73156952Sume
74156952Sumestruct timespec
75156952SumeevConsTime(time_t sec, long nsec) {
76156952Sume	struct timespec x;
77156952Sume
78156952Sume	x.tv_sec = sec;
79156952Sume	x.tv_nsec = nsec;
80156952Sume	return (x);
81156952Sume}
82156952Sume
83156952Sumestruct timespec
84156952SumeevAddTime(struct timespec addend1, struct timespec addend2) {
85156952Sume	struct timespec x;
86156952Sume
87156952Sume	x.tv_sec = addend1.tv_sec + addend2.tv_sec;
88156952Sume	x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
89156952Sume	if (x.tv_nsec >= BILLION) {
90156952Sume		x.tv_sec++;
91156952Sume		x.tv_nsec -= BILLION;
92156952Sume	}
93156952Sume	return (x);
94156952Sume}
95156952Sume
96156952Sumestruct timespec
97156952SumeevSubTime(struct timespec minuend, struct timespec subtrahend) {
98156952Sume	struct timespec x;
99156952Sume
100156952Sume	x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
101156952Sume	if (minuend.tv_nsec >= subtrahend.tv_nsec)
102156952Sume		x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
103156952Sume	else {
104156952Sume		x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
105156952Sume		x.tv_sec--;
106156952Sume	}
107156952Sume	return (x);
108156952Sume}
109156952Sume
110156952Sumeint
111156952SumeevCmpTime(struct timespec a, struct timespec b) {
112156952Sume	long x = a.tv_sec - b.tv_sec;
113156952Sume
114156952Sume	if (x == 0L)
115156952Sume		x = a.tv_nsec - b.tv_nsec;
116156952Sume	return (x < 0L ? (-1) : x > 0L ? (1) : (0));
117156952Sume}
118156952Sume
119156952Sumestruct timespec
120288147SrodrigcevNowTime(void) {
121156952Sume	struct timeval now;
122156952Sume#ifdef CLOCK_REALTIME
123156952Sume	struct timespec tsnow;
124156952Sume	int m = CLOCK_REALTIME;
125156952Sume
126156952Sume#ifdef CLOCK_MONOTONIC
127156952Sume	if (__evOptMonoTime)
128156952Sume		m = CLOCK_MONOTONIC;
129156952Sume#endif
130156952Sume	if (clock_gettime(m, &tsnow) == 0)
131156952Sume		return (tsnow);
132156952Sume#endif
133156952Sume	if (gettimeofday(&now, NULL) < 0)
134156952Sume		return (evConsTime(0, 0));
135156952Sume	return (evTimeSpec(now));
136156952Sume}
137156952Sume
138156952Sumestruct timespec
139288147SrodrigcevUTCTime(void) {
140156952Sume	struct timeval now;
141156952Sume#ifdef CLOCK_REALTIME
142156952Sume	struct timespec tsnow;
143156952Sume	if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
144156952Sume		return (tsnow);
145156952Sume#endif
146156952Sume	if (gettimeofday(&now, NULL) < 0)
147156952Sume		return (evConsTime(0, 0));
148156952Sume	return (evTimeSpec(now));
149156952Sume}
150156952Sume
151156956Sume#ifndef _LIBC
152156952Sumestruct timespec
153156952SumeevLastEventTime(evContext opaqueCtx) {
154156952Sume	evContext_p *ctx = opaqueCtx.opaque;
155156952Sume
156156952Sume	return (ctx->lastEventTime);
157156952Sume}
158156956Sume#endif
159156952Sume
160156952Sumestruct timespec
161156952SumeevTimeSpec(struct timeval tv) {
162156952Sume	struct timespec ts;
163156952Sume
164156952Sume	ts.tv_sec = tv.tv_sec;
165156952Sume	ts.tv_nsec = tv.tv_usec * 1000;
166156952Sume	return (ts);
167156952Sume}
168156952Sume
169156956Sume#if !defined(USE_KQUEUE) || !defined(_LIBC)
170156952Sumestruct timeval
171156952SumeevTimeVal(struct timespec ts) {
172156952Sume	struct timeval tv;
173156952Sume
174156952Sume	tv.tv_sec = ts.tv_sec;
175156952Sume	tv.tv_usec = ts.tv_nsec / 1000;
176156952Sume	return (tv);
177156952Sume}
178156956Sume#endif
179156952Sume
180156956Sume#ifndef _LIBC
181156952Sumeint
182156952SumeevSetTimer(evContext opaqueCtx,
183156952Sume	   evTimerFunc func,
184156952Sume	   void *uap,
185156952Sume	   struct timespec due,
186156952Sume	   struct timespec inter,
187156952Sume	   evTimerID *opaqueID
188156952Sume) {
189156952Sume	evContext_p *ctx = opaqueCtx.opaque;
190156952Sume	evTimer *id;
191156952Sume
192156952Sume	evPrintf(ctx, 1,
193156952Sume"evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
194156952Sume		 ctx, func, uap,
195156952Sume		 (long)due.tv_sec, due.tv_nsec,
196156952Sume		 (long)inter.tv_sec, inter.tv_nsec);
197156952Sume
198156952Sume#ifdef __hpux
199156952Sume	/*
200156952Sume	 * tv_sec and tv_nsec are unsigned.
201156952Sume	 */
202156952Sume	if (due.tv_nsec >= BILLION)
203156952Sume		EV_ERR(EINVAL);
204156952Sume
205156952Sume	if (inter.tv_nsec >= BILLION)
206156952Sume		EV_ERR(EINVAL);
207156952Sume#else
208156952Sume	if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
209156952Sume		EV_ERR(EINVAL);
210156952Sume
211156952Sume	if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
212156952Sume		EV_ERR(EINVAL);
213156952Sume#endif
214156952Sume
215156952Sume	/* due={0,0} is a magic cookie meaning "now." */
216156952Sume	if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
217156952Sume		due = evNowTime();
218156952Sume
219156952Sume	/* Allocate and fill. */
220156952Sume	OKNEW(id);
221156952Sume	id->func = func;
222156952Sume	id->uap = uap;
223156952Sume	id->due = due;
224156952Sume	id->inter = inter;
225156952Sume
226156952Sume	if (heap_insert(ctx->timers, id) < 0)
227156952Sume		return (-1);
228156952Sume
229156952Sume	/* Remember the ID if the caller provided us a place for it. */
230156952Sume	if (opaqueID)
231156952Sume		opaqueID->opaque = id;
232156952Sume
233156952Sume	if (ctx->debug > 7) {
234156952Sume		evPrintf(ctx, 7, "timers after evSetTimer:\n");
235156952Sume		(void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
236156952Sume	}
237156952Sume
238156952Sume	return (0);
239156952Sume}
240156952Sume
241156952Sumeint
242156952SumeevClearTimer(evContext opaqueCtx, evTimerID id) {
243156952Sume	evContext_p *ctx = opaqueCtx.opaque;
244156952Sume	evTimer *del = id.opaque;
245156952Sume
246156952Sume	if (ctx->cur != NULL &&
247156952Sume	    ctx->cur->type == Timer &&
248156952Sume	    ctx->cur->u.timer.this == del) {
249156952Sume		evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
250156952Sume		/*
251156952Sume		 * Setting the interval to zero ensures that evDrop() will
252156952Sume		 * clean up the timer.
253156952Sume		 */
254156952Sume		del->inter = evConsTime(0, 0);
255156952Sume		return (0);
256156952Sume	}
257156952Sume
258156952Sume	if (heap_element(ctx->timers, del->index) != del)
259156952Sume		EV_ERR(ENOENT);
260156952Sume
261156952Sume	if (heap_delete(ctx->timers, del->index) < 0)
262156952Sume		return (-1);
263156952Sume	FREE(del);
264156952Sume
265156952Sume	if (ctx->debug > 7) {
266156952Sume		evPrintf(ctx, 7, "timers after evClearTimer:\n");
267156952Sume		(void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
268156952Sume	}
269156952Sume
270156952Sume	return (0);
271156952Sume}
272156952Sume
273156952Sumeint
274156952SumeevConfigTimer(evContext opaqueCtx,
275156952Sume	     evTimerID id,
276156952Sume	     const char *param,
277156952Sume	     int value
278156952Sume) {
279156952Sume	evContext_p *ctx = opaqueCtx.opaque;
280156952Sume	evTimer *timer = id.opaque;
281156952Sume	int result=0;
282156952Sume
283156952Sume	UNUSED(value);
284156952Sume
285156952Sume	if (heap_element(ctx->timers, timer->index) != timer)
286156952Sume		EV_ERR(ENOENT);
287156952Sume
288156952Sume	if (strcmp(param, "rate") == 0)
289156952Sume		timer->mode |= EV_TMR_RATE;
290156952Sume	else if (strcmp(param, "interval") == 0)
291156952Sume		timer->mode &= ~EV_TMR_RATE;
292156952Sume	else
293156952Sume		EV_ERR(EINVAL);
294156952Sume
295156952Sume	return (result);
296156952Sume}
297156952Sume
298156952Sumeint
299156952SumeevResetTimer(evContext opaqueCtx,
300156952Sume	     evTimerID id,
301156952Sume	     evTimerFunc func,
302156952Sume	     void *uap,
303156952Sume	     struct timespec due,
304156952Sume	     struct timespec inter
305156952Sume) {
306156952Sume	evContext_p *ctx = opaqueCtx.opaque;
307156952Sume	evTimer *timer = id.opaque;
308156952Sume	struct timespec old_due;
309156952Sume	int result=0;
310156952Sume
311156952Sume	if (heap_element(ctx->timers, timer->index) != timer)
312156952Sume		EV_ERR(ENOENT);
313156952Sume
314156952Sume#ifdef __hpux
315156952Sume	/*
316156952Sume	 * tv_sec and tv_nsec are unsigned.
317156952Sume	 */
318156952Sume	if (due.tv_nsec >= BILLION)
319156952Sume		EV_ERR(EINVAL);
320156952Sume
321156952Sume	if (inter.tv_nsec >= BILLION)
322156952Sume		EV_ERR(EINVAL);
323156952Sume#else
324156952Sume	if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
325156952Sume		EV_ERR(EINVAL);
326156952Sume
327156952Sume	if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
328156952Sume		EV_ERR(EINVAL);
329156952Sume#endif
330156952Sume
331156952Sume	old_due = timer->due;
332156952Sume
333156952Sume	timer->func = func;
334156952Sume	timer->uap = uap;
335156952Sume	timer->due = due;
336156952Sume	timer->inter = inter;
337156952Sume
338156952Sume	switch (evCmpTime(due, old_due)) {
339156952Sume	case -1:
340156952Sume		result = heap_increased(ctx->timers, timer->index);
341156952Sume		break;
342156952Sume	case 0:
343156952Sume		result = 0;
344156952Sume		break;
345156952Sume	case 1:
346156952Sume		result = heap_decreased(ctx->timers, timer->index);
347156952Sume		break;
348156952Sume	}
349156952Sume
350156952Sume	if (ctx->debug > 7) {
351156952Sume		evPrintf(ctx, 7, "timers after evResetTimer:\n");
352156952Sume		(void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
353156952Sume	}
354156952Sume
355156952Sume	return (result);
356156952Sume}
357156952Sume
358156952Sumeint
359156952SumeevSetIdleTimer(evContext opaqueCtx,
360156952Sume		evTimerFunc func,
361156952Sume		void *uap,
362156952Sume		struct timespec max_idle,
363156952Sume		evTimerID *opaqueID
364156952Sume) {
365156952Sume	evContext_p *ctx = opaqueCtx.opaque;
366156952Sume	idle_timer *tt;
367156952Sume
368156952Sume	/* Allocate and fill. */
369156952Sume	OKNEW(tt);
370156952Sume	tt->func = func;
371156952Sume	tt->uap = uap;
372156952Sume	tt->lastTouched = ctx->lastEventTime;
373156952Sume	tt->max_idle = max_idle;
374156952Sume
375156952Sume	if (evSetTimer(opaqueCtx, idle_timeout, tt,
376156952Sume		       evAddTime(ctx->lastEventTime, max_idle),
377156952Sume		       max_idle, opaqueID) < 0) {
378156952Sume		FREE(tt);
379156952Sume		return (-1);
380156952Sume	}
381156952Sume
382156952Sume	tt->timer = opaqueID->opaque;
383156952Sume
384156952Sume	return (0);
385156952Sume}
386156952Sume
387156952Sumeint
388156952SumeevClearIdleTimer(evContext opaqueCtx, evTimerID id) {
389156952Sume	evTimer *del = id.opaque;
390156952Sume	idle_timer *tt = del->uap;
391156952Sume
392156952Sume	FREE(tt);
393156952Sume	return (evClearTimer(opaqueCtx, id));
394156952Sume}
395156952Sume
396156952Sumeint
397156952SumeevResetIdleTimer(evContext opaqueCtx,
398156952Sume		 evTimerID opaqueID,
399156952Sume		 evTimerFunc func,
400156952Sume		 void *uap,
401156952Sume		 struct timespec max_idle
402156952Sume) {
403156952Sume	evContext_p *ctx = opaqueCtx.opaque;
404156952Sume	evTimer *timer = opaqueID.opaque;
405156952Sume	idle_timer *tt = timer->uap;
406156952Sume
407156952Sume	tt->func = func;
408156952Sume	tt->uap = uap;
409156952Sume	tt->lastTouched = ctx->lastEventTime;
410156952Sume	tt->max_idle = max_idle;
411156952Sume
412156952Sume	return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
413156952Sume			     evAddTime(ctx->lastEventTime, max_idle),
414156952Sume			     max_idle));
415156952Sume}
416156952Sume
417156952Sumeint
418156952SumeevTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
419156952Sume	evContext_p *ctx = opaqueCtx.opaque;
420156952Sume	evTimer *t = id.opaque;
421156952Sume	idle_timer *tt = t->uap;
422156952Sume
423156952Sume	tt->lastTouched = ctx->lastEventTime;
424156952Sume
425156952Sume	return (0);
426156952Sume}
427156952Sume
428156952Sume/* Public to the rest of eventlib. */
429156952Sume
430156952Sumeheap_context
431156952SumeevCreateTimers(const evContext_p *ctx) {
432156952Sume
433156952Sume	UNUSED(ctx);
434156952Sume
435156952Sume	return (heap_new(due_sooner, set_index, 2048));
436156952Sume}
437156952Sume
438156952Sumevoid
439156952SumeevDestroyTimers(const evContext_p *ctx) {
440156952Sume	(void) heap_for_each(ctx->timers, free_timer, NULL);
441156952Sume	(void) heap_free(ctx->timers);
442156952Sume}
443156952Sume
444156952Sume/* Private. */
445156952Sume
446156952Sumestatic int
447156952Sumedue_sooner(void *a, void *b) {
448156952Sume	evTimer *a_timer, *b_timer;
449156952Sume
450156952Sume	a_timer = a;
451156952Sume	b_timer = b;
452156952Sume	return (evCmpTime(a_timer->due, b_timer->due) < 0);
453156952Sume}
454156952Sume
455156952Sumestatic void
456156952Sumeset_index(void *what, int index) {
457156952Sume	evTimer *timer;
458156952Sume
459156952Sume	timer = what;
460156952Sume	timer->index = index;
461156952Sume}
462156952Sume
463156952Sumestatic void
464156952Sumefree_timer(void *what, void *uap) {
465156952Sume	evTimer *t = what;
466156952Sume
467156952Sume	UNUSED(uap);
468156952Sume
469156952Sume	FREE(t);
470156952Sume}
471156952Sume
472156952Sumestatic void
473156952Sumeprint_timer(void *what, void *uap) {
474156952Sume	evTimer *cur = what;
475156952Sume	evContext_p *ctx = uap;
476156952Sume
477156952Sume	cur = what;
478156952Sume	evPrintf(ctx, 7,
479156952Sume	    "  func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
480156952Sume		 cur->func, cur->uap,
481156952Sume		 (long)cur->due.tv_sec, cur->due.tv_nsec,
482156952Sume		 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
483156952Sume}
484156952Sume
485156952Sumestatic void
486156952Sumeidle_timeout(evContext opaqueCtx,
487156952Sume	     void *uap,
488156952Sume	     struct timespec due,
489156952Sume	     struct timespec inter
490156952Sume) {
491156952Sume	evContext_p *ctx = opaqueCtx.opaque;
492156952Sume	idle_timer *this = uap;
493156952Sume	struct timespec idle;
494156952Sume
495156952Sume	UNUSED(due);
496156952Sume	UNUSED(inter);
497156952Sume
498156952Sume	idle = evSubTime(ctx->lastEventTime, this->lastTouched);
499156952Sume	if (evCmpTime(idle, this->max_idle) >= 0) {
500156952Sume		(this->func)(opaqueCtx, this->uap, this->timer->due,
501156952Sume			     this->max_idle);
502156952Sume		/*
503156952Sume		 * Setting the interval to zero will cause the timer to
504156952Sume		 * be cleaned up in evDrop().
505156952Sume		 */
506156952Sume		this->timer->inter = evConsTime(0, 0);
507156952Sume		FREE(this);
508156952Sume	} else {
509156952Sume		/* evDrop() will reschedule the timer. */
510156952Sume		this->timer->inter = evSubTime(this->max_idle, idle);
511156952Sume	}
512156952Sume}
513156956Sume#endif
514170244Sume
515170244Sume/*! \file */
516