Home | History | Annotate | Line # | Download | only in rtadvd
timer.c revision 1.10.6.1
      1  1.10.6.1     tls /*	$NetBSD: timer.c,v 1.10.6.1 2013/02/25 00:30:49 tls Exp $	*/
      2       1.9  rpaulo /*	$KAME: timer.c,v 1.11 2005/04/14 06:22:35 suz Exp $	*/
      3       1.2  itojun 
      4       1.1  itojun /*
      5       1.1  itojun  * Copyright (C) 1998 WIDE Project.
      6       1.1  itojun  * All rights reserved.
      7       1.1  itojun  *
      8       1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9       1.1  itojun  * modification, are permitted provided that the following conditions
     10       1.1  itojun  * are met:
     11       1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12       1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13       1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16       1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     17       1.1  itojun  *    may be used to endorse or promote products derived from this software
     18       1.1  itojun  *    without specific prior written permission.
     19       1.1  itojun  *
     20       1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21       1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22       1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23       1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24       1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25       1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26       1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27       1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28       1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29       1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30       1.1  itojun  * SUCH DAMAGE.
     31       1.1  itojun  */
     32       1.1  itojun 
     33      1.10     roy #include <sys/queue.h>
     34       1.1  itojun #include <sys/time.h>
     35       1.1  itojun 
     36       1.1  itojun #include <unistd.h>
     37       1.1  itojun #include <syslog.h>
     38       1.1  itojun #include <stdlib.h>
     39       1.1  itojun #include <string.h>
     40       1.1  itojun #include <search.h>
     41       1.1  itojun #include "timer.h"
     42       1.1  itojun 
     43      1.10     roy struct rtadvd_timer_head_t ra_timer = TAILQ_HEAD_INITIALIZER(ra_timer);
     44       1.1  itojun 
     45       1.1  itojun #define MILLION 1000000
     46       1.4  itojun #define TIMEVAL_EQUAL(t1,t2) ((t1)->tv_sec == (t2)->tv_sec &&\
     47       1.5  itojun  (t1)->tv_usec == (t2)->tv_usec)
     48       1.1  itojun 
     49      1.10     roy static struct timeval tm_limit = {0x7fffffff, 0x7fffffff};
     50      1.10     roy static struct timeval tm_max;
     51       1.1  itojun 
     52       1.1  itojun void
     53      1.10     roy rtadvd_timer_init(void)
     54       1.1  itojun {
     55       1.1  itojun 
     56      1.10     roy 	TAILQ_INIT(&ra_timer);
     57      1.10     roy 	tm_max = tm_limit;
     58       1.1  itojun }
     59       1.1  itojun 
     60       1.1  itojun struct rtadvd_timer *
     61      1.10     roy rtadvd_add_timer(struct rtadvd_timer *(*timeout) (void *),
     62      1.10     roy     void (*update) (void *, struct timeval *),
     63       1.9  rpaulo     void *timeodata, void *updatedata)
     64       1.1  itojun {
     65       1.1  itojun 	struct rtadvd_timer *newtimer;
     66       1.1  itojun 
     67       1.1  itojun 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
     68       1.1  itojun 		syslog(LOG_ERR,
     69       1.8  itojun 		       "<%s> can't allocate memory", __func__);
     70       1.1  itojun 		exit(1);
     71       1.1  itojun 	}
     72       1.1  itojun 
     73       1.1  itojun 	memset(newtimer, 0, sizeof(*newtimer));
     74       1.1  itojun 
     75       1.1  itojun 	if (timeout == NULL) {
     76       1.1  itojun 		syslog(LOG_ERR,
     77       1.8  itojun 		       "<%s> timeout function unspecified", __func__);
     78       1.1  itojun 		exit(1);
     79       1.1  itojun 	}
     80       1.1  itojun 	newtimer->expire = timeout;
     81       1.1  itojun 	newtimer->update = update;
     82       1.1  itojun 	newtimer->expire_data = timeodata;
     83       1.1  itojun 	newtimer->update_data = updatedata;
     84       1.1  itojun 	newtimer->tm = tm_max;
     85       1.1  itojun 
     86       1.1  itojun 	/* link into chain */
     87      1.10     roy 	TAILQ_INSERT_TAIL(&ra_timer, newtimer, next);
     88       1.1  itojun 
     89       1.1  itojun 	return(newtimer);
     90       1.1  itojun }
     91       1.1  itojun 
     92       1.1  itojun void
     93       1.4  itojun rtadvd_remove_timer(struct rtadvd_timer **timer)
     94       1.4  itojun {
     95      1.10     roy 
     96  1.10.6.1     tls 	if (*timer) {
     97  1.10.6.1     tls 		TAILQ_REMOVE(&ra_timer, *timer, next);
     98  1.10.6.1     tls 		free(*timer);
     99  1.10.6.1     tls 		*timer = NULL;
    100  1.10.6.1     tls 	}
    101       1.4  itojun }
    102       1.4  itojun 
    103       1.4  itojun void
    104       1.1  itojun rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *timer)
    105       1.1  itojun {
    106       1.1  itojun 	struct timeval now;
    107       1.1  itojun 
    108       1.1  itojun 	/* reset the timer */
    109       1.1  itojun 	gettimeofday(&now, NULL);
    110       1.1  itojun 
    111       1.1  itojun 	TIMEVAL_ADD(&now, tm, &timer->tm);
    112       1.1  itojun 
    113       1.1  itojun 	/* update the next expiration time */
    114      1.10     roy 	if (TIMEVAL_LT(timer->tm, tm_max))
    115      1.10     roy 		tm_max = timer->tm;
    116       1.1  itojun 
    117       1.1  itojun 	return;
    118       1.1  itojun }
    119       1.1  itojun 
    120       1.1  itojun /*
    121       1.9  rpaulo  * Check expiration for each timer. If a timer expires,
    122       1.1  itojun  * call the expire function for the timer and update the timer.
    123       1.1  itojun  * Return the next interval for select() call.
    124       1.1  itojun  */
    125       1.1  itojun struct timeval *
    126      1.10     roy rtadvd_check_timer(void)
    127       1.1  itojun {
    128       1.1  itojun 	static struct timeval returnval;
    129       1.1  itojun 	struct timeval now;
    130  1.10.6.1     tls 	struct rtadvd_timer *tm, *tmn;
    131       1.1  itojun 
    132       1.1  itojun 	gettimeofday(&now, NULL);
    133      1.10     roy 	tm_max = tm_limit;
    134       1.1  itojun 
    135  1.10.6.1     tls 	TAILQ_FOREACH_SAFE(tm, &ra_timer, next, tmn) {
    136       1.1  itojun 		if (TIMEVAL_LEQ(tm->tm, now)) {
    137       1.9  rpaulo 			if ((*tm->expire)(tm->expire_data) == NULL)
    138       1.9  rpaulo 				continue; /* the timer was removed */
    139       1.9  rpaulo 			if (tm->update)
    140       1.9  rpaulo 				(*tm->update)(tm->update_data, &tm->tm);
    141       1.1  itojun 			TIMEVAL_ADD(&tm->tm, &now, &tm->tm);
    142       1.1  itojun 		}
    143       1.1  itojun 
    144      1.10     roy 		if (TIMEVAL_LT(tm->tm, tm_max))
    145      1.10     roy 			tm_max = tm->tm;
    146       1.1  itojun 	}
    147       1.1  itojun 
    148      1.10     roy 	if (TIMEVAL_EQUAL(&tm_max, &tm_limit)) {
    149       1.4  itojun 		/* no need to timeout */
    150       1.4  itojun 		return(NULL);
    151      1.10     roy 	} else if (TIMEVAL_LT(tm_max, now)) {
    152       1.1  itojun 		/* this may occur when the interval is too small */
    153       1.1  itojun 		returnval.tv_sec = returnval.tv_usec = 0;
    154       1.5  itojun 	} else
    155      1.10     roy 		TIMEVAL_SUB(&tm_max, &now, &returnval);
    156       1.1  itojun 	return(&returnval);
    157       1.1  itojun }
    158       1.1  itojun 
    159       1.1  itojun struct timeval *
    160       1.1  itojun rtadvd_timer_rest(struct rtadvd_timer *timer)
    161       1.1  itojun {
    162       1.1  itojun 	static struct timeval returnval, now;
    163       1.1  itojun 
    164       1.1  itojun 	gettimeofday(&now, NULL);
    165       1.1  itojun 	if (TIMEVAL_LEQ(timer->tm, now)) {
    166       1.1  itojun 		syslog(LOG_DEBUG,
    167       1.1  itojun 		       "<%s> a timer must be expired, but not yet",
    168       1.8  itojun 		       __func__);
    169       1.1  itojun 		returnval.tv_sec = returnval.tv_usec = 0;
    170       1.1  itojun 	}
    171       1.1  itojun 	else
    172       1.1  itojun 		TIMEVAL_SUB(&timer->tm, &now, &returnval);
    173       1.1  itojun 
    174       1.1  itojun 	return(&returnval);
    175       1.1  itojun }
    176       1.1  itojun 
    177       1.1  itojun /* result = a + b */
    178       1.1  itojun void
    179       1.1  itojun TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
    180       1.1  itojun {
    181       1.1  itojun 	long l;
    182       1.1  itojun 
    183       1.1  itojun 	if ((l = a->tv_usec + b->tv_usec) < MILLION) {
    184       1.1  itojun 		result->tv_usec = l;
    185       1.1  itojun 		result->tv_sec = a->tv_sec + b->tv_sec;
    186       1.1  itojun 	}
    187       1.1  itojun 	else {
    188       1.1  itojun 		result->tv_usec = l - MILLION;
    189       1.1  itojun 		result->tv_sec = a->tv_sec + b->tv_sec + 1;
    190       1.1  itojun 	}
    191       1.1  itojun }
    192       1.1  itojun 
    193       1.1  itojun /*
    194       1.1  itojun  * result = a - b
    195       1.1  itojun  * XXX: this function assumes that a >= b.
    196       1.1  itojun  */
    197       1.1  itojun void
    198       1.1  itojun TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
    199       1.1  itojun {
    200       1.1  itojun 	long l;
    201       1.1  itojun 
    202       1.1  itojun 	if ((l = a->tv_usec - b->tv_usec) >= 0) {
    203       1.1  itojun 		result->tv_usec = l;
    204       1.1  itojun 		result->tv_sec = a->tv_sec - b->tv_sec;
    205       1.1  itojun 	}
    206       1.1  itojun 	else {
    207       1.1  itojun 		result->tv_usec = MILLION + l;
    208       1.1  itojun 		result->tv_sec = a->tv_sec - b->tv_sec - 1;
    209       1.1  itojun 	}
    210       1.1  itojun }
    211