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)
23static 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
53static int	__evOptMonoTime;
54#else
55static int due_sooner(void *, void *);
56static void set_index(void *, int);
57static void free_timer(void *, void *);
58static void print_timer(void *, void *);
59static void idle_timeout(evContext, void *, struct timespec, struct timespec);
60
61/* Private type. */
62
63typedef 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
74struct timespec
75evConsTime(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
83struct timespec
84evAddTime(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
96struct timespec
97evSubTime(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
110int
111evCmpTime(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
119struct timespec
120evNowTime(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
138struct timespec
139evUTCTime(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
152struct timespec
153evLastEventTime(evContext opaqueCtx) {
154	evContext_p *ctx = opaqueCtx.opaque;
155
156	return (ctx->lastEventTime);
157}
158#endif
159
160struct timespec
161evTimeSpec(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)
170struct timeval
171evTimeVal(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
181int
182evSetTimer(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
241int
242evClearTimer(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
273int
274evConfigTimer(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
298int
299evResetTimer(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
358int
359evSetIdleTimer(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
387int
388evClearIdleTimer(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
396int
397evResetIdleTimer(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
417int
418evTouchIdleTimer(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
430heap_context
431evCreateTimers(const evContext_p *ctx) {
432
433	UNUSED(ctx);
434
435	return (heap_new(due_sooner, set_index, 2048));
436}
437
438void
439evDestroyTimers(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
446static int
447due_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
455static void
456set_index(void *what, int index) {
457	evTimer *timer;
458
459	timer = what;
460	timer->index = index;
461}
462
463static void
464free_timer(void *what, void *uap) {
465	evTimer *t = what;
466
467	UNUSED(uap);
468
469	FREE(t);
470}
471
472static void
473print_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
485static void
486idle_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