timer.c

Go to the documentation of this file.
00001 
00055 #include <time.h> 
00056 #include <stdlib.h>
00057 #include <unistd.h>
00058 
00059 #include "utils.h"
00060 #include "globals.h"
00061 #include "worker.h"
00062 
00063 #include "timer.h"
00064 
00065 
00066 /* defined in ../diameter_peer.c */
00067 int dp_add_pid(pid_t pid);
00068 void dp_del_pid(pid_t pid);
00069 
00070 
00071 timer_cb_list_t *timers=0;  
00072 gen_lock_t *timers_lock=0;  
00075 #define TIMER_RESOLUTION 1
00076 
00084 void timer_loop()
00085 {
00086     time_t now;
00087     timer_cb_t *i;
00088     callback_f cb=0;
00089     void *ptr=0;
00090     
00091     while(1){
00092         if (shutdownx && *shutdownx) break;
00093         now = time(0);
00094         //LOG(L_DBG,"DBG:timer_loop(): The time is %u\n",(unsigned int)now);
00095     
00096         do {
00097             cb = 0;
00098             lock_get(timers_lock);
00099             i = timers->head;
00100             while(i && i->expires>now) i = i->next;
00101             if (i){
00102                 cb = i->cb;
00103                 ptr = *(i->ptr);
00104                 if (i->one_time){
00105                     if (i->prev) i->prev->next = i->next;
00106                     else timers->head = i->next;
00107                     if (i->next) i->next->prev = i->prev;
00108                     else timers->tail = i->next;
00109                     shm_free(i);
00110                 }else{
00111                     i->expires = now + i->interval;
00112                 }
00113             }
00114             lock_release(timers_lock);
00115     
00116             if (cb) cb(now,ptr);
00117     
00118         } while(cb);
00119                 
00120         sleep(TIMER_RESOLUTION);
00121     }
00122 }
00123 
00132 int add_timer(int expires_in,int one_time,callback_f cb,void *ptr)
00133 {
00134     timer_cb_t *n;
00135     if (expires_in==0){
00136         LOG(L_ERR,"ERROR:add_timer(): Minimum expiration time is 1 second!\n");
00137         return 0;
00138     }
00139     n = shm_malloc(sizeof(timer_cb_t));
00140     if (!n){
00141         LOG_NO_MEM("shm",sizeof(timer_cb_t));
00142         return 0;
00143     }
00144     n->ptr = shm_malloc(sizeof(void*));
00145     if (!n){
00146         LOG_NO_MEM("shm",sizeof(void*));
00147         shm_free(n);
00148         return 0;
00149     }
00150     n->expires = expires_in + time(0);
00151     n->one_time = one_time;
00152     n->interval = expires_in;
00153     n->cb = cb;
00154     *(n->ptr) = ptr;
00155 
00156     lock_get(timers_lock);
00157         n->prev = timers->tail;
00158         n->next = 0;
00159         if (!timers->head) timers->head = n;
00160         if (timers->tail) timers->tail->next = n;
00161         timers->tail = n;
00162     lock_release(timers_lock);
00163     return 1;
00164 }
00165 
00169 void timer_cdp_init()
00170 {
00171     timers = shm_malloc(sizeof(timer_cb_list_t));
00172     timers->head=0;
00173     timers->tail=0;
00174     timers_lock = lock_alloc();
00175     timers_lock = lock_init(timers_lock);   
00176 }
00177 
00181 void timer_cdp_destroy()
00182 {
00183     timer_cb_t *n,*i;
00184 /*  lock_get(timers_lock);*/
00185     i = timers->head;
00186     while(i){
00187         n = i->next;
00188         if (i->ptr) shm_free(i->ptr);
00189         shm_free(i);
00190         i = n;
00191     }
00192     shm_free(timers);
00193     lock_destroy(timers_lock);
00194     lock_dealloc((void*)timers_lock);
00195 }
00196 
00203 void timer_process(int returns)
00204 {
00205     LOG(L_INFO,"INFO:Timer process starting up...\n");
00206         
00207     timer_loop();
00208     
00209     LOG(L_INFO,"INFO:... Timer process finished\n");
00210     if (!returns) {
00211 #ifdef CDP_FOR_SER
00212 #else
00213 #ifdef PKG_MALLOC
00214     #ifdef PKG_MALLOC
00215         LOG(memlog, "Timer Memory status (pkg):\n");
00216         //pkg_status();
00217         #ifdef pkg_sums
00218             pkg_sums();
00219         #endif 
00220     #endif
00221 #endif
00222         dp_del_pid(getpid());       
00223 #endif      
00224         
00225         exit(0);
00226     }
00227 }
00228 
00229 

Generated on Sat Sep 6 04:17:47 2008 for Open IMS Core CSCFs by  doxygen 1.5.2