linux c语言之静态链表的使用一

#ifndef _MULTI_TIMER_H_
#define _MULTI_TIMER_H_
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>

typedef struct Timer {
    uint32_t        cur_ticks;          /* Record current timer start tick */
    uint32_t        cur_expired_time;   /* Record current timer expired time */
    uint32_t        timeout;    /* Delay (xx ms) time to start tiemr */
    uint32_t        repeat;     /* Timer interval expired time (xx ms) */
    void *          arg;        /* Input argument for timeout_cb function */
    void            (*timeout_cb)(void *arg); /* Timer expired callback function */
    struct Timer*   next;       /* Pointer to next timer */
} Timer;



struct _Timer_list
{
   struct Timer*   next;
};

/*
It means 1 tick for 1ms. 
Your can configurate for your tick time such as 5ms/10ms and so on.
*/
#define CFG_TIMER_1_TICK_N_MS   1


void timer_debug(void);
void timer_loop(void);
void timer_ticks(void);
void timer_start(struct Timer*handle,void(*timeout_cb)(void*arg), uint32_t timeout, uint32_t repeat, void *arg);
#endif

```c
#include "timer_list.h"

static struct _Timer_list Timer_list;
static  uint32_t  _timer_ticks = 0;

void timer_start(struct Timer*handle,void(*timeout_cb)(void*arg), uint32_t timeout, uint32_t repeat, void *arg)
{
       struct Timer*ptimer_head = NULL; // Timer_list.next;
       struct Timer*ptimer_tmp= NULL; 
     //  struct Timer**ptimer_pre = NULL;
	   
	handle->timeout_cb = timeout_cb;
	handle->arg = arg;
	handle->timeout = timeout;
	handle->repeat =  repeat;
	handle->cur_ticks = _timer_ticks;
	handle->cur_expired_time  = handle->timeout;

	//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u, timeout: %u\r\n",  handle->cur_ticks, handle->cur_expired_time, _timer_ticks, timeout);

	if(!Timer_list.next)
	{
		Timer_list.next = handle;
		handle->next = NULL;
		return ;
	}
	
	ptimer_head  = Timer_list.next;

      //printf(" handle is %p\n",handle);

      
      while(ptimer_head)
	{
		if(ptimer_head==handle)
		{
			printf("timer node exit\n");
			handle->timeout_cb = timeout_cb;
			handle->arg = arg;
			handle->timeout = timeout;
			handle->repeat =  repeat;
			handle->cur_ticks = _timer_ticks;
			handle->cur_expired_time  = handle->timeout;
			return ;
		}
	    //  ptimer_pre = &(ptimer_head->next);
	      ptimer_tmp = ptimer_head;
	      //printf(" ptimer_pre is %p\n",ptimer_pre);
	      ptimer_head = ptimer_head->next;
	}

	handle->next = NULL;
	//*ptimer_pre = handle ;
	ptimer_tmp->next = handle ;
}

int timer_stop(struct Timer* handle)
{
	struct Timer*ptimer_head = Timer_list.next; 
	struct Timer**ptimer_pre  = NULL ; 
	
	if((!handle)||(!ptimer_head))
	{
	    return -1 ;
	}

	ptimer_pre = &Timer_list.next;
	//printf(" handle is %p ptimer_head->next is%p\n",handle,ptimer_head->next);
	while(ptimer_head)
	{
		if(ptimer_head==handle)
		{
			//ptimer_head = handle->next;
			*ptimer_pre = handle->next;
			//printf("  ptimer_head->next is%p\n",Timer_list.next);
			printf("timer node exit %s %d\n",__func__,__LINE__);
			return 0;
		}
		//ptimer_tmp = ptimer_head;
		*ptimer_pre = ptimer_head;
		ptimer_head = ptimer_head->next;
	}
	printf("timer node exit\n");
      return 0;
}

void timer_loop(void)
{
    	struct Timer* target;
	for(target = Timer_list.next; target; target = target->next) 
	{

		if(_timer_ticks - target->cur_ticks >= target->cur_expired_time)
		{
		//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u\r\n", 
		//        target->cur_ticks, target->cur_expired_time, _timer_ticks);
			if(target->repeat == 0)
			{
				timer_stop(target);
			} 
			else 
			{
				target->cur_ticks = _timer_ticks;
				target->cur_expired_time = target->repeat;
			}            
				target->timeout_cb(target->arg);
		}
	}
}

void timer_ticks(void)
{
	_timer_ticks += CFG_TIMER_1_TICK_N_MS;
	//printf("%s %d\n",__func__,__LINE__);
}

	
void timer_debug(void)
{
	struct Timer*ptimer_head = Timer_list.next;
	printf("timer ptimer_head %s %d\n",__func__,__LINE__);
	 while(ptimer_head)
	{
		printf(" list timeout is %d\n",ptimer_head->repeat);
	      ptimer_head = ptimer_head->next;
	      //printf(" ptimer_head is %p\n",ptimer_head);
	}
}


```c
#include "timer_list.h"
#include <stdio.h>
#include <signal.h>
#include <signal.h>
#include <sys/time.h>
#include <time.h>

static Timer  timer_key;
static Timer  timer_key2;
static Timer  timer_key3;

void timer1_callback(void *arg)
{  
    //printf_timestamp("timer1 timeout! arg: %p\r\n", arg);
    printf("%s%d\n",__func__,__LINE__);
}


void signalHandler(int signo)
{    
	switch(signo)    
	{     
		case SIGALRM:  
			timer_ticks();		
			timer_loop();      
			//printf_timestamp("Caught the SIGALRM signal every 1ms !\n");    
		break; 
	}
}

int main()
{
	signal(SIGALRM, signalHandler);  
	struct itimerval new_value, old_value;   
	new_value.it_value.tv_sec     = 0; 
	new_value.it_value.tv_usec    = 1; 
	new_value.it_interval.tv_sec  = 0;    
	new_value.it_interval.tv_usec = 1000 * CFG_TIMER_1_TICK_N_MS; // 1ms    setitimer(ITIMER_REAL, &new_value, &old_value);
	setitimer(ITIMER_REAL, &new_value, &old_value);

	timer_start(&timer_key2, timer1_callback, 0, 1000, &timer_key2);  
	//timer_start(&timer_key3, timer1_callback, 4000, 1000, &timer_key3);  
	//timer_start(&timer_key2, timer1_callback, 4000, 1000, &timer_key2);  
	
	timer_debug();	
	
	sleep(3);
	timer_stop(&timer_key2);
	timer_debug();	
	
	while(1) 
	{
		if(getchar() == 'q')
		{
		  break;
		}
	}
	
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天6点半起床10点半睡觉和今天早晚运动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值