timer.c File Reference


Detailed Description

CDiameterPeer Timer Process.

Author:
Dragos Vingarzan vingarzan -at- fokus dot fraunhofer dot de

Definition in file timer.c.

#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include "utils.h"
#include "globals.h"
#include "worker.h"
#include "timer.h"

Go to the source code of this file.

Defines

#define TIMER_RESOLUTION   1
 how many seconds to sleep on each timer iteration

Functions

int dp_add_pid (pid_t pid)
 Add a pid to the local process list.
void dp_del_pid (pid_t pid)
 Delete a pid from the process list.
void timer_loop ()
 Loop that checks every TIMER_RESOLUTION seconds if some timer expired.
int add_timer (int expires_in, int one_time, callback_f cb, void *ptr)
 Adds a timer to the timer list.
void timer_cdp_init ()
 Init the timer structures.
void timer_cdp_destroy ()
 Destroy the timer structures.
void timer_process (int returns)
 Timer Process function.

Variables

timer_cb_list_ttimers = 0
 list of timers
gen_lock_t * timers_lock = 0
 lock for the list of timers


Define Documentation

#define TIMER_RESOLUTION   1

how many seconds to sleep on each timer iteration

Definition at line 75 of file timer.c.

Referenced by timer_loop().


Function Documentation

int dp_add_pid ( pid_t  pid  )  [inline]

Add a pid to the local process list.

Parameters:
pid newly forked pid
Returns:
1 on success or 0 on error

Definition at line 95 of file diameter_peer.c.

00096 {
00097     pid_list_t *n;
00098     lock_get(pid_list_lock);
00099     n = shm_malloc(sizeof(pid_list_t));
00100     if (!n){
00101         LOG_NO_MEM("shm",sizeof(pid_list_t));
00102         lock_release(pid_list_lock);
00103         return 0;
00104     }
00105     n->pid = pid;
00106     n->next = 0;
00107     n->prev = pid_list->tail;
00108     if (!pid_list->head) pid_list->head = n;
00109     if (pid_list->tail) pid_list->tail->next = n;
00110     pid_list->tail = n;
00111     lock_release(pid_list_lock);
00112     return 1;
00113 }

void dp_del_pid ( pid_t  pid  )  [inline]

Delete a pid from the process list.

Parameters:
pid - the pid to remove

Definition at line 132 of file diameter_peer.c.

00133 {   
00134     pid_list_t *i;
00135     lock_get(pid_list_lock);
00136     i = pid_list->head;
00137     if (!i) {
00138         lock_release(pid_list_lock);
00139         return;
00140     }
00141     while(i && i->pid!=pid) i = i->next;
00142     if (i){
00143         if (i->prev) i->prev->next = i->next;
00144         else pid_list->head = i->next;
00145         if (i->next) i->next->prev = i->prev;
00146         else pid_list->tail = i->prev;
00147         shm_free(i);
00148     }
00149     lock_release(pid_list_lock);
00150 }

void timer_loop (  ) 

Loop that checks every TIMER_RESOLUTION seconds if some timer expired.

On expires, the callback is called. The callback should return rapidly in order to avoid blocking the timer process. If the timer is "one_time", then it is removed from the timers list.

Returns:
on shutdown

Definition at line 84 of file timer.c.

References _timer_cb_t::cb, _timer_cb_t::expires, timer_cb_list_t::head, _timer_cb_t::interval, _timer_cb_t::next, _timer_cb_t::one_time, _timer_cb_t::prev, _timer_cb_t::ptr, shutdownx, timer_cb_list_t::tail, TIMER_RESOLUTION, timers, and timers_lock.

Referenced by timer_process().

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 }

int add_timer ( int  expires_in,
int  one_time,
callback_f  cb,
void *  ptr 
)

Adds a timer to the timer list.

Parameters:
expires_in - time until expiration in seconds
one_time - if after expiration it should be removed or kept in the timers list
cb - callback function to be called on expiration
ptr - generic pointer to pass to the callback on expiration
Returns:
1 on success or 0 on failure

Definition at line 132 of file timer.c.

References _timer_cb_t::cb, _timer_cb_t::expires, timer_cb_list_t::head, _timer_cb_t::interval, LOG_NO_MEM, _timer_cb_t::next, _timer_cb_t::one_time, _timer_cb_t::prev, _timer_cb_t::ptr, timer_cb_list_t::tail, timers, and timers_lock.

Referenced by peer_manager_init(), and trans_init().

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 }

void timer_cdp_init (  ) 

Init the timer structures.

Definition at line 169 of file timer.c.

References timer_cb_list_t::head, timer_cb_list_t::tail, timers, and timers_lock.

Referenced by diameter_peer_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 }

void timer_cdp_destroy (  ) 

Destroy the timer structures.

Definition at line 181 of file timer.c.

References timer_cb_list_t::head, _timer_cb_t::next, _timer_cb_t::ptr, timers, and timers_lock.

Referenced by diameter_peer_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 }

void timer_process ( int  returns  ) 

Timer Process function.

It calls timer_loop().

Parameters:
returns - whether on shutdown this function should return or exit
Returns:
if returns is set then on shutdown, else never and on shutdown it exits

Definition at line 203 of file timer.c.

References dp_del_pid(), memlog, and timer_loop().

Referenced by diameter_peer_start().

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 }


Variable Documentation

timer_cb_list_t* timers = 0

list of timers

Definition at line 71 of file timer.c.

Referenced by add_timer(), timer_cdp_destroy(), timer_cdp_init(), and timer_loop().

gen_lock_t* timers_lock = 0

lock for the list of timers

Definition at line 72 of file timer.c.

Referenced by add_timer(), timer_cdp_destroy(), timer_cdp_init(), and timer_loop().


Generated on Fri Jul 18 04:14:01 2008 for Open IMS Core CSCFs by  doxygen 1.5.2