alc_list.c

Go to the documentation of this file.
00001 
00034 #ifdef _MSC_VER
00035 #else
00036 #include <sys/time.h>
00037 #endif
00038 
00039 #include <assert.h>
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <time.h>
00043 
00044 #include "alc_list.h"
00045 
00046 #ifdef _MSC_VER
00047 
00057 int get_time_of_day(struct timeval *tv) {
00058   time_t tempo_t;
00059   time(&tempo_t);
00060   
00061   tv->tv_sec = tempo_t;
00062   tv->tv_usec = 0;
00063   
00064   return 0;
00065 }
00066 #else
00067 
00077 int get_time_of_day(struct timeval *tv){
00078   return gettimeofday(tv, NULL);
00079 }
00080 
00081 #endif
00082 
00090 void lock_list(alc_list_t *a_list) {
00091 #ifdef _MSC_VER
00092   EnterCriticalSection(&(a_list->session_variables_semaphore));
00093 #else
00094   pthread_mutex_lock(&(a_list->session_variables_semaphore));
00095 #endif
00096 }
00097 
00105 void unlock_list(alc_list_t *a_list) {
00106 #ifdef _MSC_VER
00107   LeaveCriticalSection(&(a_list->session_variables_semaphore));
00108 #else
00109   pthread_mutex_unlock(&(a_list->session_variables_semaphore));
00110 #endif
00111 }
00112 
00122 int is_empty_private(const alc_list_t *a_list) {
00123         return a_list->first_elem == NULL;
00124 }
00125 
00135 struct alc_list_node* prepare_insert(alc_rcv_container_t *a_data) {
00136   struct alc_list_node *my_node;
00137   struct timeval now;
00138   double container_age;
00139 
00140   assert(a_data != NULL);
00141   
00142   if(a_data->time_stamp.tv_sec == 0) {
00143     /* mark the timestamp first time the container is inserted */
00144     get_time_of_day(&(a_data->time_stamp));
00145     assert(a_data->time_stamp.tv_sec != 0);
00146   }
00147   else {
00148     /* check if the container is too old, in case I discard it */
00149     get_time_of_day(&now);
00150     
00151     container_age =  now.tv_sec  - a_data->time_stamp.tv_sec + 
00152       (now.tv_usec - a_data->time_stamp.tv_usec ) / 1E6;
00153     assert(container_age >= 0);
00154 
00155     if(container_age > 3) {
00156       /* containers older than 3 secs are discarded */
00157       return NULL;
00158     } 
00159   }
00160   
00161   my_node = malloc(1 * sizeof(struct alc_list_node));
00162   my_node->next = NULL;
00163   my_node->data = a_data;
00164   
00165   return my_node;
00166 } 
00167 
00168 void push_back(alc_list_t *a_list, alc_rcv_container_t *a_data) {
00169   struct alc_list_node *my_node;
00170   
00171   my_node = prepare_insert(a_data);
00172   
00173   if(my_node == NULL) {
00174     return;
00175   }
00176   
00177   lock_list(a_list);
00178   
00179   if(is_empty_private(a_list)) {
00180     a_list->first_elem = my_node;
00181     a_list->last_elem = my_node;
00182   }
00183   else {
00184     a_list->last_elem->next = my_node;
00185     a_list->last_elem = my_node;
00186   }
00187   
00188   unlock_list(a_list);
00189 }
00190 
00191 void push_front(alc_list_t *a_list, alc_rcv_container_t *a_data) {
00192   struct alc_list_node *my_node;
00193   
00194   my_node = prepare_insert(a_data);
00195   
00196   if(my_node == NULL) {
00197     return;
00198   }
00199   
00200   lock_list(a_list);
00201   
00202   if(is_empty_private(a_list)) {
00203     a_list->first_elem = my_node;
00204     a_list->last_elem = my_node;
00205   }
00206   else {
00207     my_node->next = a_list->first_elem;
00208     a_list->first_elem = my_node;
00209   }
00210   
00211   unlock_list(a_list);
00212 }
00213 
00214 alc_rcv_container_t* pop_front(alc_list_t *a_list) {
00215   struct alc_list_node *my_node = NULL;
00216   void *my_ret = NULL;
00217   
00218   lock_list(a_list);
00219   
00220   if(is_empty_private(a_list)) {
00221   }
00222   else {
00223     my_node = a_list->first_elem;
00224     
00225     if(a_list->first_elem == a_list->last_elem) {  /* last element in list */
00226       assert(my_node->next == NULL);
00227       
00228       a_list->first_elem = NULL;
00229       a_list->last_elem = NULL;
00230     }
00231     else {
00232       a_list->first_elem = my_node->next;
00233     }
00234     
00235     my_ret = my_node->data;
00236   }
00237   
00238   unlock_list(a_list);
00239   free(my_node);
00240   
00241   return my_ret;
00242 }
00243 
00244 int is_empty(const alc_list_t *a_list) {
00245   int my_ret;
00246   
00247   lock_list((alc_list_t*)a_list);
00248   
00249   my_ret = (a_list->first_elem == NULL);
00250   
00251         unlock_list((alc_list_t*)a_list);
00252         
00253         return my_ret;
00254 }
00255 
00256 alc_list_t* build_list(void) {
00257   alc_list_t *my_list;
00258   
00259   my_list = calloc(1, sizeof(alc_list_t));
00260   
00261 #ifdef _MSC_VER
00262   InitializeCriticalSection(&(my_list->session_variables_semaphore));
00263 #else
00264   my_list->session_variables_semaphore = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
00265 #endif
00266   
00267   return my_list;
00268 }
00269 
00270 void destroy_list(alc_list_t *a_list) {
00271   struct alc_list_node *my_node = NULL;
00272   struct alc_list_node *my_currentnode = NULL;
00273   
00274   if(a_list == NULL) {
00275     return;
00276   }
00277   
00278   lock_list(a_list);
00279   
00280   for(my_node = a_list->first_elem; my_node != NULL;) {
00281     my_currentnode = my_node;
00282     my_node = my_node->next;
00283     
00284     free(my_currentnode->data);
00285     free(my_currentnode);
00286   }
00287   
00288   unlock_list(a_list);
00289   
00290 #ifdef _MSC_VER
00291   DeleteCriticalSection(&(a_list->session_variables_semaphore));
00292 #else
00293 #endif
00294   
00295   free(a_list);
00296 }

Generated on Fri Mar 9 19:59:51 2007 for MAD-FCL by  doxygen 1.5.0