Home | History | Annotate | Line # | Download | only in mrouted
callout.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: callout.c,v 1.2 1995/10/09 03:51:34 thorpej Exp $	*/
      2  1.2  thorpej 
      3  1.1  mycroft /*
      4  1.1  mycroft  * The mrouted program is covered by the license in the accompanying file
      5  1.1  mycroft  * named "LICENSE".  Use of the mrouted program represents acceptance of
      6  1.1  mycroft  * the terms and conditions listed in that file.
      7  1.1  mycroft  *
      8  1.1  mycroft  * The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
      9  1.1  mycroft  * Leland Stanford Junior University.
     10  1.1  mycroft  */
     11  1.1  mycroft 
     12  1.1  mycroft #include "defs.h"
     13  1.1  mycroft 
     14  1.1  mycroft /* the code below implements a callout queue */
     15  1.1  mycroft static int id = 0;
     16  1.1  mycroft static struct timeout_q  *Q = 0; /* pointer to the beginning of timeout queue */
     17  1.1  mycroft 
     18  1.1  mycroft static int in_callout= 0;
     19  1.1  mycroft 
     20  1.1  mycroft typedef void (* cfunc_t)();
     21  1.1  mycroft 
     22  1.1  mycroft struct timeout_q {
     23  1.1  mycroft 	struct timeout_q *next;		/* next event */
     24  1.1  mycroft 	int        	 id;
     25  1.1  mycroft 	cfunc_t          func ;    	/* function to call */
     26  1.1  mycroft 	char	   	 *data;		/* func's data */
     27  1.1  mycroft 	int            	 time;		/* time offset to next event*/
     28  1.1  mycroft };
     29  1.1  mycroft 
     30  1.1  mycroft static void print_Q();
     31  1.1  mycroft 
     32  1.1  mycroft void callout_init()
     33  1.1  mycroft {
     34  1.1  mycroft     Q = (struct timeout_q *) 0;
     35  1.1  mycroft }
     36  1.1  mycroft 
     37  1.1  mycroft 
     38  1.1  mycroft /*
     39  1.1  mycroft  * signal handler for SIGALARM that is called once every second
     40  1.1  mycroft  */
     41  1.1  mycroft void age_callout_queue()
     42  1.1  mycroft {
     43  1.1  mycroft     struct timeout_q *ptr;
     44  1.1  mycroft 
     45  1.1  mycroft     if (in_callout)
     46  1.1  mycroft 	return;
     47  1.1  mycroft 
     48  1.1  mycroft     in_callout = 1;
     49  1.1  mycroft     ptr = Q;
     50  1.1  mycroft 
     51  1.1  mycroft     while (ptr){
     52  1.1  mycroft 	if (!ptr->time ) {
     53  1.1  mycroft 	    /* timeout has happened */
     54  1.1  mycroft 	    if(ptr->func)
     55  1.1  mycroft 		ptr->func(ptr->data);
     56  1.1  mycroft 	    Q = Q->next;
     57  1.1  mycroft 
     58  1.1  mycroft 	    free(ptr);
     59  1.1  mycroft 	    ptr = Q;
     60  1.1  mycroft 	}
     61  1.1  mycroft 	else {
     62  1.1  mycroft 	    ptr->time --;
     63  1.1  mycroft #ifdef IGMP_DEBUG
     64  1.1  mycroft 	    log(LOG_DEBUG,0,"[callout, age_callout_queue] -- time (%d)", ptr->time);
     65  1.1  mycroft #endif IGMP_DEBUG
     66  1.1  mycroft 	    in_callout = 0; return;
     67  1.1  mycroft 	}
     68  1.1  mycroft     }
     69  1.1  mycroft     in_callout = 0;
     70  1.1  mycroft     return;
     71  1.1  mycroft }
     72  1.1  mycroft 
     73  1.1  mycroft 
     74  1.1  mycroft /*
     75  1.1  mycroft  * sets the timer
     76  1.1  mycroft  */
     77  1.1  mycroft int timer_setTimer(delay, action, data)
     78  1.1  mycroft     int 	delay;  	/* number of units for timeout */
     79  1.1  mycroft     cfunc_t	action; 	/* function to be called on timeout */
     80  1.1  mycroft     char  	*data;  	/* what to call the timeout function with */
     81  1.1  mycroft {
     82  1.1  mycroft     struct     timeout_q  *ptr, *node, *prev;
     83  1.1  mycroft 
     84  1.1  mycroft     if (in_callout)
     85  1.1  mycroft 	return -1;
     86  1.1  mycroft 
     87  1.1  mycroft     in_callout = 1;
     88  1.1  mycroft 
     89  1.1  mycroft     /* create a node */
     90  1.1  mycroft     node   = (struct timeout_q *)malloc(sizeof(struct timeout_q));
     91  1.1  mycroft     if (node == 0) {
     92  1.1  mycroft 	log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
     93  1.1  mycroft 	in_callout = 0;
     94  1.1  mycroft 	return -1;
     95  1.1  mycroft     }
     96  1.1  mycroft     node->func = action;
     97  1.1  mycroft     node->data = data;
     98  1.1  mycroft     node->time = delay;
     99  1.1  mycroft     node->next = 0;
    100  1.1  mycroft     node->id   = ++id;
    101  1.1  mycroft 
    102  1.1  mycroft     prev = ptr = Q;
    103  1.1  mycroft 
    104  1.1  mycroft     /* insert node in the queue */
    105  1.1  mycroft 
    106  1.1  mycroft     /* if the queue is empty, insert the node and return */
    107  1.1  mycroft     if (!Q)
    108  1.1  mycroft 	Q = node;
    109  1.1  mycroft     else {
    110  1.1  mycroft 	/* chase the pointer looking for the right place */
    111  1.1  mycroft 	while (ptr){
    112  1.1  mycroft 
    113  1.1  mycroft 	    if (delay < ptr->time){
    114  1.1  mycroft 		/* right place */
    115  1.1  mycroft 
    116  1.1  mycroft 		node->next = ptr;
    117  1.1  mycroft 		if (ptr == Q)
    118  1.1  mycroft 		    Q = node;
    119  1.1  mycroft 		else
    120  1.1  mycroft 		    prev->next = node;
    121  1.1  mycroft 		ptr->time -= node->time;
    122  1.1  mycroft 		print_Q();
    123  1.1  mycroft 		in_callout = 0;
    124  1.1  mycroft 		return node->id;
    125  1.1  mycroft 	    }
    126  1.1  mycroft 	    else  {
    127  1.1  mycroft 		/* keep moving */
    128  1.1  mycroft 
    129  1.1  mycroft 		delay -= ptr->time; node->time = delay;
    130  1.1  mycroft 		prev = ptr;
    131  1.1  mycroft 		ptr = ptr->next;
    132  1.1  mycroft 	    }
    133  1.1  mycroft 	}
    134  1.1  mycroft 	prev->next = node;
    135  1.1  mycroft     }
    136  1.1  mycroft     print_Q();
    137  1.1  mycroft     in_callout = 0;
    138  1.1  mycroft     return node->id;
    139  1.1  mycroft }
    140  1.1  mycroft 
    141  1.1  mycroft 
    142  1.1  mycroft /* clears the associated timer */
    143  1.1  mycroft void timer_clearTimer( timer_id)
    144  1.1  mycroft     int  timer_id;
    145  1.1  mycroft {
    146  1.1  mycroft     struct timeout_q  *ptr, *prev;
    147  1.1  mycroft 
    148  1.1  mycroft     if (in_callout)  return;
    149  1.1  mycroft     in_callout = 1;
    150  1.1  mycroft 
    151  1.1  mycroft 
    152  1.1  mycroft     if ( !timer_id ) {in_callout = 0; return;}
    153  1.1  mycroft 
    154  1.1  mycroft     prev = ptr = Q;
    155  1.1  mycroft 
    156  1.1  mycroft     /*
    157  1.1  mycroft      * find the right node, delete it. the subsequent node's time
    158  1.1  mycroft      * gets bumped up
    159  1.1  mycroft      */
    160  1.1  mycroft 
    161  1.1  mycroft     print_Q();
    162  1.1  mycroft     while (ptr){
    163  1.1  mycroft 	if (ptr->id == timer_id){
    164  1.1  mycroft 	    /* got the right node */
    165  1.1  mycroft 
    166  1.1  mycroft 	    /* unlink it from the queue */
    167  1.1  mycroft 	    if ( ptr == Q)
    168  1.1  mycroft 		Q = Q->next;
    169  1.1  mycroft 	    else
    170  1.1  mycroft 		prev->next = ptr->next;
    171  1.1  mycroft 
    172  1.1  mycroft 	    /* increment next node if any */
    173  1.1  mycroft 	    if (ptr->next != 0)
    174  1.1  mycroft 		(ptr->next)->time += ptr->time;
    175  1.1  mycroft 
    176  1.1  mycroft 	    free(ptr->data);
    177  1.1  mycroft 	    free(ptr);
    178  1.1  mycroft 	    print_Q();
    179  1.1  mycroft 	    in_callout = 0;
    180  1.1  mycroft 	    return;
    181  1.1  mycroft 	}
    182  1.1  mycroft 	prev = ptr;
    183  1.1  mycroft 	ptr = ptr->next;
    184  1.1  mycroft     }
    185  1.1  mycroft     print_Q();
    186  1.1  mycroft     in_callout = 0;
    187  1.1  mycroft }
    188  1.1  mycroft 
    189  1.1  mycroft /*
    190  1.1  mycroft  * debugging utility
    191  1.1  mycroft  */
    192  1.1  mycroft static void print_Q()
    193  1.1  mycroft {
    194  1.1  mycroft #ifdef IGMP_DEBUG
    195  1.1  mycroft     struct timeout_q  *ptr;
    196  1.1  mycroft 
    197  1.1  mycroft     for(ptr = Q; ptr; ptr = ptr->next)
    198  1.1  mycroft 	log(LOG_DEBUG,0,"(%d,%d) ", ptr->id, ptr->time);
    199  1.1  mycroft #endif IGMP_DEBUG
    200  1.1  mycroft }
    201  1.1  mycroft 
    202