⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpls_timer.c

📁 技术文件名称:MPLSv1.0软件模块测试规程
💻 C
字号:
/****************************************************************************/
/*      Product Name:   MPLS PACK1.0
 *      Module  Name:   LDP/CR_LDP
 *      File    Name:   mpls_timer.c
 *      Author  Name:   weng.qing f.jun
 *      Creat   Date:   2002-6-18
 *      Version     :   1.0
 *      Function    :   timer list manager
 *      Note        :   
*/      
/****************************************************************************/
#include"mpls_timer.h"
#include"ldpvars.h" 

void    mpls_callout_init(Q)
	struct mpls_timeout_q  **Q;
{
    (*Q) = (struct mpls_timeout_q *) 0;
}

void    mpls_free_all_callouts(Q)
struct mpls_timeout_q  **Q;
{
    struct mpls_timeout_q *p;
    while (*Q) 
    {
	p = *Q;
	(*Q) = (*Q)->next;
	MPLS_MEM_FREE(p,mem_mgr[IUMEM_LDP_TIMER]);
    }
}


/*
 * elapsed_time seconds have passed; perform all the events that should
 * happen.
 */
void    mpls_age_callout_queue(elapsed_time,Q)
    int elapsed_time;
    struct mpls_timeout_q  **Q;
{
    struct mpls_timeout_q *ptr;
    int    i = 0;

    for (ptr = *Q; ptr; ptr = *Q, i++) 
    {
	if (ptr->time > elapsed_time) 
	{
	    ptr->time -= elapsed_time;
	    return;
	} 
	else 
	{
	    elapsed_time -= ptr->time;
	    (*Q) = (*Q)->next;
	    if (ptr->func)
		ptr->func(ptr->data);
	    MPLS_MEM_FREE(ptr,mem_mgr[IUMEM_LDP_TIMER]);
	}
    }
}

/*
 * Return in how many seconds age_callout_queue() would like to be called.
 * Return -1 if there are no events pending.
 */
int    mpls_timer_nextTimer(Q)
    struct mpls_timeout_q  **Q;
{
    if (*Q) {
	if ((*Q)->time < 0) 
	{
	    return 0;
	}
	return (*Q)->time;
    }
    return -1;
}

/* 
 * sets the timer
 */
unsigned int mpls_timer_setTimer(delay, action, data,Q)
    int 	        delay;  /* number of units for timeout */
    mpls_cfunc_t	action; /* function to be called on timeout */
    void  	        *data;  /* what to call the timeout function with */
    struct mpls_timeout_q  **Q;
{
    struct     mpls_timeout_q  *ptr, *node, *prev;
    int        i = 0;
    static     unsigned int id = 65534;	/* global id as a static local variable */
    /* create a node */	
    MPLS_MEM_ALLOC(node,(struct mpls_timeout_q *),sizeof(struct mpls_timeout_q),mem_mgr[IUMEM_LDP_TIMER]);
    if (node == 0) 
    {
	/*return -1;*/
	return 0;
    }
    node->func = action; 
    node->data = data;
    node->time = delay; 
    node->next = 0;	
    node->id   = ++id;
    if ( id == 0 )
    	id = 65534;		/* set id = 0 as a invalid timer id */
    
    prev = ptr = *Q;
    /* insert node in the queue */
    /* if the queue is empty, insert the node and return */
    if (!*Q)
	*Q = node;
    else 
    {
	/* chase the pointer looking for the right place */
	while (ptr) 
	{    
	    if (delay < ptr->time) 
	    {
		/* right place */	
		node->next = ptr;
		if (ptr == *Q)
		    *Q = node;
		else
		    prev->next = node;
		ptr->time -= node->time;
		return node->id;
	    } 
	    else  
	    {
		/* keep moving */		
		delay -= ptr->time; 
		node->time = delay;
		prev = ptr;
		ptr = ptr->next;
	    }
	    i++;
	}
	prev->next = node;
    }
    return node->id;
}

/* returns the time until the timer is scheduled */
int    mpls_timer_leftTimer(timer_id,Q)
    unsigned int timer_id;
    struct mpls_timeout_q  **Q;
{
    struct mpls_timeout_q *ptr;
    int    left = 0;

    if (!timer_id)
	return -1;

    for (ptr = *Q; ptr; ptr = ptr->next) 
    {
	left += ptr->time;
	if (ptr->id == timer_id)
	    return left;
    }
    return -1;
}

/* clears the associated timer.  Returns 1 if succeeded. */
int    mpls_timer_clearTimer(timer_id,Q)
    unsigned int  timer_id;
    struct mpls_timeout_q  **Q;
{
    struct mpls_timeout_q  *ptr, *prev;
    int    i = 0;
    
    if (!timer_id)
	return 0;

    prev = ptr = *Q;
    
    /*
     * find the right node, delete it. the subsequent node's time
     * gets bumped up
     */
    while (ptr) 
    {
	if (ptr->id == timer_id) 
	{
	    /* got the right node */
	    /* unlink it from the queue */
	    if (ptr == *Q)
		(*Q) = (*Q)->next;
	    else
		prev->next = ptr->next;	    
	    /* increment next node if any */
	    if (ptr->next != 0)
		(ptr->next)->time += ptr->time;	    


	    MPLS_MEM_FREE(ptr,mem_mgr[IUMEM_LDP_TIMER]);
	    return 1;
	}
	prev = ptr;
	ptr = ptr->next;
	i++;
    }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -