通过链表与定时器的搭配,实现同一进程中多定时器
#define NULL 0
typedef struct{
int timer_type;
int timer_status;
struct timeval end_time;
process_fun fun;
long int time;
}ama_timer_t;
#define C_MS_TO_S 1000
#define C_US_TO_S 1000000
typedef enum{
C_BT_RUN_TIMER_ONE,
C_BT_RUN_TIMER_CYCLE
}ama_run_timer_type_e;
typedef enum{
C_BT_RUN_TIMER_OFF,
C_BT_RUN_TIMER_ON
}ama_run_timer_status_e;
typedef enum{
C_CONFIG_SUCCESS,
C_CONFIG_FALSE,
C_CONFIG_ENDS
}ma_event_result_e;
void (*process_fun)(void);
int get_timer_list_null_id(int* id);
int ama_time_compare(struct timeval* now_time,struct timeval* target_time);
void ama_endtime_process(struct timeval*start_time,long int time);
int ama_create_timer(int type,long int time,process_fun fun);
void am_destroy_timer(int id);
int am_timer_check(void);
Glist* timer_list;
1.int ama_create_timer(int type,long int time,process_fun fun)
{
ama_timer_t* time_info =NULL;
gettimeofday(&sys_time,NULL);
Glist* iterator;
time_info =(ama_timer_t*)ama_malloc(ama_timer_t);
if(time_info !=NULL){
count=get_timer_list_null_id(&(timer_info->id));
time_info->timer_type=type;
time_info->time=time;
time_info->fun=fun;
time_info->status=C_BT_RUN_TIMER_ON;
ama_endtime_process(&sys_time,time);
time_info->end_time.tv_sec= sys_time.tv_sec;
time_info->end_time.tv_usec= sys_time.tv_usec;
result= timer_info->id;
timer_list=g_list_insert(timer_list,count);
}else{
result=NULL;
}
return result;
}
2 void am_destroy_timer(int id)
{
Glist* iterator;
int list_count;
ama_timer_t* time_info =NULL;
list_count =g_list_length(timer_list);
if(list_count){
for(iterator =timer_list;iterator;iterator++){
time_info =(ama_timer_t*)iterator->data;
if(time_info->id ==id){
timer_list= g_list_remove(timer_list,time_info);
ama_free(time_info);
break;
}else{
}
}
}else{
}
}
3.int am_timer_check(void)
{
int result;
Glist* iterator;
int list_count;
ama_timer_t* time_info =NULL;
struct timeval sys_time;
result=C_BT_FALSE;
list_count =g_list_length(timer_list);
if(list_count){
return result;
}else{
}
gettimeofday(&sys_time,NULL);
for(iterator =timer_list;iterator;iterator++){
time_info =(ama_bt_timer_t*)iterator->data;
if(time_info->status==C_BT_RUN_TIMER_ON){
result=ama_time_compare(&sys_time,&(time_info->end_time));
if(result ==C_CONFIG_SUCCESS){
if(time_info->type==C_BT_RUN_TIMER_ONE){
time_info->status=C_BT_RUN_TIMER_OFF;
time_info->fun();
am_destroy_timer(time_info->id);
}elseif(time_info->type==C_BT_RUN_TIMER_CYCLE){
ama_endtime_process(&sys_time,&(time_info->time));
time_info->end_time.tv_sec= sys_time.tv_sec;
time_info->end_time.tv_usec= sys_time.tv_usec;
time_info->fun();
gettimeofday(&sys_time,NULL);
}else{
}
}
}else{
}
}
result=C_CONFIG_SUCCESS;
return result;
}
void ama_endtime_process(struct timeval*start_time,long int time)
{
start_time->tv_usec=(start_time->tv_usec+time*C_MS_TO_S)%C_US_TO_S;
start_time->tv_sec=start_time.tv->sec+(start_time->tv_usec+time*C_MS_TO_S)/C_US_TO_S;
}
int ama_time_compare(struct timeval* now_time,struct timeval* target_time)
{
int result;
if(now_time->tv_sec>target_time->tv_sec){
result=C_CONFIG_SUCCESS;
}elseif((now_time->tv_sec==target_time->tv_sec)&&(now_time->tv_usec>=target_time->tv_usec)){
result=C_CONFIG_SUCCESS;
}else{
result=C_CONFIG_FALSE;
}
return result;
}
int get_timer_list_null_id(int* id)
{
int list_count;
int result;
int tmp_id;
result=0;
tmp_id=0;
ama_timer_t* time_info =NULL;
list_count =g_list_length(timer_list);
if(list_count){
for(iterator =timer_list;iterator;iterator++){
time_info =(ama_bt_timer_t*)iterator->data;
if(time_info->id >(tmp_id+1)){
id[0]=tmp_id+1;
}else{
}
tmp_id++;
result++;
}
}else{
id[0]=1;
result=1;
}
if(list_count==result){
id[0]=result+1;
result=result+1;
}
return result;
}
void mian()
{
struct itimerval tick;
signal(SIGALRM, am_timer_check);
tick.it_value.tv_sec = 1; //1秒钟后将启动定时器
tick.it_value.tv_usec= 0;
// Interval time to run function
tick.it_interval.tv_sec = 0 ;
tick.it_interval.tv_usec =10000; //定时器启动后,每隔0.01s将执行相应的函数
setitimer(ITIMER_REAL, &tick, NULL); //setitimer将触发SIGALRM信号
while(1){
}
}