1 /************************************************************************* 2 > File Name: time_queue.h 3 > Author: likeyi 4 > Mail: likeyiyy@sina.com 5 > Created Time: Fri 04 Apr 2014 05:47:42 PM CST 6 ************************************************************************/ 7 8 #ifndef TIME_QUEUE_H 9 #define TIME_QUEUE_H 10 #include "dulist.h" 11 typedef void (Function) (void * arg); 12 typedef struct time_node 13 { 14 unsigned long remain_time; 15 unsigned long id; 16 Function * fun; 17 void * arg; 18 }time_node_s; 19 typedef dulnode multi_timer; 20 void print_time_node(struct time_node * time); 21 /* 22 * 初始化一個定時器 23 * */ 24 struct time_node * timer_init(unsigned long remain_time,Function * fun,void * arg); 25 /* 26 * 初始化一個定時器隊列。 27 * */ 28 multi_timer * multi_timer_init(); 29 /* 30 * 往隊列插入一個定時器。 31 * */ 32 int multi_timer_insert(multi_timer * timer_queue,struct time_node * time); 33 /* 34 * 遍歷定時器,而且執行到期的定時器的函數 35 * */ 36 int multi_timer_traverse(multi_timer * timer_queue); 37 /* 38 * 在隊列上刪除一個定時器 39 * */ 40 int multi_timer_delete(multi_timer * timer_queue, unsigned long id); 41 /* 42 * 重置一個定時器的值 43 * */ 44 int multi_timer_reset(multi_timer * timer_queue,unsigned long id,unsigned long value); 45 #endif
/************************************************************************* > File Name: time_queue.c > Author: likeyi > Mail: likeyiyy@sina.com > Created Time: Fri 04 Apr 2014 05:47:48 PM CST ************************************************************************/ #include "includes.h" static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t get_id_mutex = PTHREAD_MUTEX_INITIALIZER; static unsigned long id; static multi_timer * timer_queue; static void sig_fun(int id) { //printf("hello I am \n"); multi_timer_traverse(timer_queue); } static unsigned long get_id() { return ++id; } void print_time_node(struct time_node * time) { printf("remain_time:%lu ",time->remain_time); printf("id:%lu ",time->id); printf("fun:%p\n",time->fun); } /* * 初始化一個定時器 * */ struct time_node * timer_init(unsigned long remain_time,Function * fun,void * arg) { pthread_mutex_lock(&get_id_mutex); unsigned long nid = get_id(); pthread_mutex_unlock(&get_id_mutex); struct time_node * ntimer = (void *)malloc(sizeof(struct time_node)); if(ntimer == NULL) { printf("Error,when malloc time node\n"); exit(-1); } ntimer->remain_time = remain_time; ntimer->id = nid; ntimer->fun = fun; ntimer->arg = arg; return ntimer; } /* * 初始化一個定時器隊列。 * */ multi_timer * multi_timer_init() { pthread_mutex_lock(&mutex); list_init(&timer_queue); signal(SIGALRM,sig_fun); struct itimerval value, ovalue; value.it_value.tv_sec = 1; value.it_value.tv_usec = 0; value.it_interval.tv_sec = 1; value.it_interval.tv_usec = 0; setitimer(ITIMER_REAL, &value, &ovalue); pthread_mutex_unlock(&mutex); return timer_queue; } /* * 往隊列插入一個定時器。 * 插入定時器這個操做,應該加鎖。 * */ int multi_timer_insert(multi_timer * timer_queue,struct time_node * time) { int i = 0; multi_timer * timer = timer_queue->next; while(timer != timer_queue) { struct time_node * t = (struct time_node *)timer->data; if(time->remain_time > t->remain_time) { timer = timer->next; } else { break; } i++; } pthread_mutex_lock(&mutex); list_insert(timer_queue,i+1,(void*)time); pthread_mutex_unlock(&mutex); } /* * 遍歷定時器,而且執行到期的定時器的函數 * */ int multi_timer_traverse(multi_timer * timer_queue) { assert(timer_queue != NULL); multi_timer * t = timer_queue->next; while(t != timer_queue) { struct time_node * ntime = (struct time_node *)t->data; if((--ntime->remain_time <= 0) && (ntime->fun != NULL)) { ntime->fun(ntime->arg); multi_timer_delete(timer_queue,ntime->id); } t = t->next; } //list_traverse(timer_queue, print_time_node); } /* * 在隊列上刪除一個定時器 * */ int multi_timer_delete(multi_timer * timer_queue, unsigned long id) { multi_timer * t = timer_queue->next; int i = 0; struct time_node * ntime = NULL; while(t != timer_queue) { i++; ntime = (struct time_node *)t->data; if(ntime->id != id) { t =t -> next; } else { //printf("I can at here \n"); list_delete(timer_queue,i,&ntime); free(ntime); return 0; } } //printf("I am leaver\n"); } /* * 重置一個定時器的值 * */ int multi_timer_reset(multi_timer * timer_queue,unsigned long id,unsigned long value) { multi_timer * t = timer_queue->next; int i = 0; struct time_node * ntime = NULL; pthread_mutex_lock(&mutex); while(t != timer_queue) { i++; ntime = (struct time_node *)t->data; if(ntime->id != id) { t = t->next; } else { ntime->remain_time = value; pthread_mutex_unlock(&mutex); return 0; } } pthread_mutex_unlock(&mutex); }