struct sort_timer{
timer head;
timer tail;
};
static sort_timer timer_list;
typedef struct timer* ti;
struct timer{
time_t expire;
timer* pre;
timer next;
void (fn *)();
char *file_name[];
};
void init_add_timer(timer* timer,sort_timer* timer_list){
if(!timer){
return ;
}
if(!timer_list){
head=tail=timer;
return ;
}
if(timer->expire<timer_list.head){
timer->next=head;
head->pre=timer;
head=timer;
return ;
}
add_timer(timer,timer_list.head);
}
void adjust_timer(timer* timer,sort_timer* timer_list){
if(!timer){
return ;
}
timer* tmp=timer->next;
if(!tmp || (timer->expireexpire)){
return ;
}
if(timer==head){
head=head->next;
head->pre=NULL;
timer->next=NULL;
add_timer(timer,timer_list.head);
}
else
{
timer->pre->next=timer->next;
timer->next->pre=timer->pre;
add_timer(timer,timer->next);
}
}
void del_timer(timer* timer){
if(!timer){
return ;
}
if((timerhead)&&(timertail))
{
head=NULL;
tail=NULL;
return ;
}
if(timer==head){
head=head->next;
head->pre=NULL;
return ;
}
if(timer==tail){
tail=tail->pre;
tail->next=NULL;//是否需要做释放内存操作!
return;
}
timer->pre->next=timer->next;
timer->next->pre=timer-pre;
}
void(){
if(!timer_list->head)
return;
printf(“timer tick \n”);
time_t cur=time(NULL);
timer* tmp=head;
while(!tmp){
if(tmp->expire>cur){
break;
}
tmp->fn();//需要参数不
head=tmp->next;
if(head){
head->pre=NULL;
}
tmp=head;
}
}
void add_timer(timer* timer,timer* lst_head){
timer* pre=lst_head;
timer* tmp=pre->next;
while(tmp){
if(timer->expireexpire){
pre->next=timer;
timer->next=tmp;
tmp->pre=timer;
timer->pre=pre;
bread;
}
pre=tmp;
tmp=tmp->next;
}
if(!tmp){
pre->next=timer;
timer->pre=pre;
timer->next=NULL;
tail=timer;
}
}