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