#ifndef _MULTI_TIMER_H_
#define _MULTI_TIMER_H_
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
typedef struct Timer {
uint32_t cur_ticks;
uint32_t cur_expired_time;
uint32_t timeout;
uint32_t repeat;
void * arg;
void (*timeout_cb)(void *arg);
struct Timer* next;
} Timer;
struct _Timer_list
{
struct Timer* next;
};
#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;
struct Timer*ptimer_tmp= 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;
if(!Timer_list.next)
{
Timer_list.next = handle;
handle->next = NULL;
return ;
}
ptimer_head = Timer_list.next;
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_tmp = ptimer_head;
ptimer_head = ptimer_head->next;
}
handle->next = NULL;
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;
while(ptimer_head)
{
if(ptimer_head==handle)
{
*ptimer_pre = handle->next;
printf("timer node exit %s %d\n",__func__,__LINE__);
return 0;
}
*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)
{
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;
}
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;
}
}
```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;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4a86f3a24d3add506dfede41d594744b.png)