# 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_node, * timerlinklist;
# define CFG_TIMER_1_TICK_N_MS 1
timerlinklist list_init ( ) ;
timer_node* node_create ( ) ;
void list_free ( timerlinklist L) ;
int list_empty ( timerlinklist L) ;
int list_length ( timerlinklist L) ;
void list_show ( timerlinklist L) ;
timerlinklist list_create_head_insert ( timerlinklist L) ;
timerlinklist list_create_tail_insert ( timerlinklist L) ;
int list_insert ( timerlinklist L, timer_node* data, int pos) ;
timer_node* list_get_elem ( timerlinklist L, int pos) ;
int list_delete ( timerlinklist L, int pos) ;
# endif
# include "timer_list.h"
# include <string.h>
# include <stdlib.h>
timerlinklist list_init ( )
{
timerlinklist L = ( timerlinklist) malloc ( sizeof ( timer_node) ) ;
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return NULL ;
}
L-> next = NULL ;
return L;
}
timer_node* node_create ( )
{
timer_node* pnode = ( timer_node* ) malloc ( sizeof ( timer_node) ) ;
if ( pnode== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return NULL ;
}
pnode-> next = NULL ;
return pnode;
}
void list_show ( timerlinklist L)
{
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return ;
}
timer_node* pnode = L-> next;
while ( pnode)
{
printf ( "%s %d repeat is %d\n" , __func__ , __LINE__ , pnode-> repeat) ;
pnode = pnode-> next;
}
}
int list_empty ( timerlinklist L)
{
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
timer_node* pnode = L-> next;
if ( pnode)
{
return 0 ;
}
else
{
return 1 ;
}
}
void list_free ( timerlinklist L)
{
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return ;
}
timer_node* pnode, * ptmp;
pnode = L-> next;
while ( pnode)
{
ptmp = pnode-> next;
free ( pnode) ;
memset ( pnode, 0 , sizeof ( timer_node) ) ;
pnode = ptmp;
}
L-> next = NULL ;
}
int list_length ( timerlinklist L)
{
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
timer_node* pnode = L-> next;
int i = 0 ;
while ( pnode)
{
pnode = pnode-> next;
i++ ;
}
return i;
}
timerlinklist list_create_head_insert ( timerlinklist L)
{
int i = 0 ;
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return NULL ;
}
timer_node* phead = L-> next;
timer_node* pnode = NULL ;
for ( i= 0 ; i< 100 ; i++ )
{
pnode = node_create ( ) ;
pnode-> repeat = i;
pnode-> next = phead;
phead = pnode;
}
L-> next = phead;
return L;
}
timerlinklist list_create_tail_insert ( timerlinklist L)
{
int i = 0 ;
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return NULL ;
}
timer_node* phead = L-> next;
timer_node* pnode = NULL ;
timer_node* prenode = NULL ;
for ( i= 0 ; i< 100 ; i++ )
{
pnode = node_create ( ) ;
pnode-> repeat = i;
while ( phead)
{
prenode = phead;
phead = phead-> next;
}
phead = pnode;
if ( ! L-> next)
{
L-> next = phead;
}
if ( prenode)
{
prenode-> next= pnode;
}
}
return L;
}
int list_insert ( timerlinklist L, timer_node* data, int pos)
{
int i = 0 ;
int length = - 1 ;
timer_node* phead = L-> next;
timer_node* prenode = NULL ;
if ( L== NULL || data== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
if ( pos< 0 )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
length = list_length ( L) ;
if ( pos>= length)
{
while ( phead)
{
prenode = phead;
phead = phead-> next;
}
phead = data;
if ( ! L-> next)
{
L-> next = phead;
}
if ( prenode)
{
prenode-> next= data;
}
}
else if ( pos== 0 )
{
data-> next = phead;
phead = data;
L-> next = phead;
}
else
{
printf ( "%s %d length is %d repeat is %d\n" , __func__ , __LINE__ , list_length ( L) , phead-> repeat) ;
while ( phead&& i< pos)
{
prenode = phead;
phead = phead-> next;
i++ ;
}
data-> next = prenode-> next;
prenode-> next = data;
}
return 0 ;
}
timer_node* list_get_elem ( timerlinklist L, int pos)
{
int i = 0 ;
int length = - 1 ;
timer_node* phead = L-> next;
timer_node* prenode = NULL ;
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return NULL ;
}
if ( pos< 0 )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return NULL ;
}
length = list_length ( L) ;
while ( phead&& i< pos)
{
prenode = phead;
phead = phead-> next;
i++ ;
}
return prenode;
}
int list_delete ( timerlinklist L, int pos)
{
int i = 0 ;
int length = - 1 ;
timer_node* phead = L-> next;
timer_node* prenode = NULL ;
if ( L== NULL )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
if ( pos<= 0 )
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
length = list_length ( L) ;
printf ( "%d length %d" , pos, length) ;
if ( pos> length)
{
printf ( "error create %s %d\n" , __func__ , __LINE__ ) ;
return - 1 ;
}
else if ( pos== length)
{
while ( phead&& i< pos- 1 )
{
prenode = phead;
phead = phead-> next;
i++ ;
}
if ( pos == 1 )
{
printf ( "%s %d \n" , __func__ , __LINE__ ) ;
free ( phead) ;
L-> next = NULL ;
return 0 ;
}
printf ( "%s %d \n" , __func__ , __LINE__ ) ;
free ( prenode-> next) ;
prenode-> next = NULL ;
}
else
{
printf ( "%s %d \n" , __func__ , __LINE__ ) ;
while ( phead&& i< pos- 1 )
{
prenode = phead;
phead = phead-> next;
i++ ;
}
if ( pos == 1 )
{
printf ( "%s %d \n" , __func__ , __LINE__ ) ;
phead = phead-> next;
free ( L-> next) ;
L-> next = phead;
return 0 ;
}
phead = phead-> next;
printf ( "%s %d \n" , __func__ , __LINE__ ) ;
free ( prenode-> next) ;
prenode-> next = phead;
}
return 0 ;
}
# include "timer_list.h"
# include <stdio.h>
# include <signal.h>
# include <signal.h>
# include <sys/time.h>
# include <time.h>
static void test_list_node_create ( void ) ;
static void test_list_list_create ( void ) ;
static void test_list_list_create2 ( ) ;
static void test_list_basic_op ( void ) ;
static void test_list_stack ( void ) ;
static void test_list_queue ( void ) ;
int main ( )
{
test_list_queue ( ) ;
while ( 1 )
{
if ( getchar ( ) == 'q' )
{
break ;
}
}
return 0 ;
}
static void test_list_node_create ( void )
{
int ret = - 1 ;
timer_node* ptimer_key;
timer_node* ptimer_led;
timerlinklist L = list_init ( ) ;
if ( L== NULL )
{
return ;
}
ret = list_empty ( L) ;
printf ( "%s %d ret is %d\n" , __func__ , __LINE__ , ret) ;
ptimer_key = node_create ( ) ;
if ( ptimer_key== NULL )
{
return ;
}
ptimer_key-> repeat = 1000 ;
ptimer_led = node_create ( ) ;
if ( ptimer_led== NULL )
{
return ;
}
ptimer_led-> repeat = 2000 ;
L-> next = ptimer_key;
ptimer_key-> next = ptimer_led;
list_show ( L) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
ret = list_empty ( L) ;
list_free ( L) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
}
static void test_list_list_create ( void )
{
timerlinklist L = list_init ( ) ;
if ( L== NULL )
{
return ;
}
L = list_create_head_insert ( L) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
list_free ( L) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
}
static void test_list_list_create2 ( void )
{
timerlinklist L = list_init ( ) ;
if ( L== NULL )
{
return ;
}
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
L = list_create_tail_insert ( L) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
list_free ( L) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
}
static void test_list_basic_op ( void )
{
timerlinklist L = list_init ( ) ;
timer_node* pnode = NULL ;
if ( L== NULL )
{
return ;
}
# if 0
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 2 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 3 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 4 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 5 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 6 ;
list_insert ( L, pnode, list_length ( L) ) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
list_free ( L) ;
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 2 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 3 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 4 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 5 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 6 ;
list_insert ( L, pnode, 0 ) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
list_free ( L) ;
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 2 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 4 ;
list_insert ( L, pnode, 2 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 5 ;
list_insert ( L, pnode, 3 ) ;
printf ( "%s %d length is %d\n" , __func__ , __LINE__ , list_length ( L) ) ;
list_show ( L) ;
list_free ( L) ;
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 2 ;
list_insert ( L, pnode, 0 ) ;
pnode = list_get_elem ( L, 2 ) ;
printf ( "%s %d repeat is %d\n" , __func__ , __LINE__ , pnode-> repeat) ;
list_show ( L) ;
list_free ( L) ;
# endif
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, 0 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_show ( L) ;
list_free ( L) ;
}
static void test_list_stack ( void )
{
timerlinklist L = list_init ( ) ;
timer_node* pnode = NULL ;
if ( L== NULL )
{
return ;
}
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 2 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 3 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 4 ;
list_insert ( L, pnode, 0 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_show ( L) ;
}
static void test_list_queue ( void )
{
timerlinklist L = list_init ( ) ;
timer_node* pnode = NULL ;
if ( L== NULL )
{
return ;
}
pnode = node_create ( ) ;
pnode-> repeat = 0 ;
list_insert ( L, pnode, 0 ) ;
pnode = node_create ( ) ;
pnode-> repeat = 1 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 2 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 3 ;
list_insert ( L, pnode, list_length ( L) ) ;
pnode = node_create ( ) ;
pnode-> repeat = 4 ;
list_insert ( L, pnode, list_length ( L) ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_delete ( L, 1 ) ;
list_show ( L) ;
}