/home/dko/projects/mobilec/trunk/src/include/ap_queue_template.h

Go to the documentation of this file.
00001 /*[
00002  * Copyright (c) 2007 Integration Engineering Laboratory
00003                       University of California, Davis
00004  *
00005  * Permission to use, copy, and distribute this software and its
00006  * documentation for any purpose with or without fee is hereby granted,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.
00010  *
00011  * Permission to modify the software is granted, but not the right to
00012  * distribute the complete modified source code.  Modifications are to
00013  * be distributed as patches to the released version.  Permission to
00014  * distribute binaries produced by compiling modified sources is granted,
00015  * provided you
00016  *   1. distribute the corresponding source modifications from the
00017  *    released version in the form of a patch file along with the binaries,
00018  *   2. add special version identification to distinguish your version
00019  *    in addition to the base release version number,
00020  *   3. provide your name and address as the primary contact for the
00021  *    support of your modified version, and
00022  *   4. retain our contact information in regard to use of the base
00023  *    software.
00024  * Permission to distribute the released version of the source code along
00025  * with corresponding source modifications in the form of a patch file is
00026  * granted with same provisions 2 through 4 for binary distributions.
00027  *
00028  * This software is provided "as is" without express or implied warranty
00029  * to the extent permitted by applicable law.
00030 ]*/
00031 
00032 #ifndef _AP_QUEUE_TEMPLATE_H_
00033 #define _AP_QUEUE_TEMPLATE_H_
00034 #include "macros.h"
00035 #include "mc_error.h"
00036 /* Fields will be: NAME */
00037 #define AP_QUEUE_DECL_TEMPLATE( name, node_type) \
00038 typedef struct name##_s \
00039 { \
00040   int size; \
00041   list_p list; \
00042   MUTEX_T* lock; \
00043   COND_T* cond; \
00044 } name##_t; \
00045   \
00046 typedef name##_t* name##_p; \
00047   \
00048 name##_p name##_New( void ); \
00049 int name##_Destroy( name##_p name ); \
00050 int name##_Add( name##_p name, node_type##_t* node ); \
00051 node_type##_t* name##_Pop( name##_p name ); \
00052 node_type##_t* name##_SearchIndex( name##_p name, int index ); \
00053 int name##_RemoveIndex(name##_p name, int index); 
00054 
00055 #define AP_QUEUE_GENERIC_DECL_TEMPLATE(name, func_name, return_type, search_type) \
00056 return_type name##_##func_name(name##_p name, const search_type key);
00057 
00058 #define AP_QUEUE_STD_DEFN_TEMPLATE( name, node_type) \
00059 name##_p name##_New( void ) \
00060 { \
00061   name##_p temp; \
00062   temp = (name##_p)malloc(sizeof(name##_t)); \
00063   temp->size = 0; \
00064   temp->list = ListInitialize(); \
00065   \
00066   temp->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T)); \
00067   temp->cond = (COND_T*)malloc(sizeof(COND_T)); \
00068   MUTEX_INIT(temp->lock); \
00069   COND_INIT(temp->cond); \
00070   return temp; \
00071 } \
00072   \
00073 int name##_Destroy( name##_p name ) \
00074 { \
00075   node_type##_t* node; \
00076   while ((node = name##_Pop(name)) != NULL) { \
00077     node_type##_Destroy(node); \
00078   } \
00079   ListTerminate(name->list); \
00080   MUTEX_DESTROY(name->lock); \
00081   COND_DESTROY(name->cond); \
00082   free(name->lock); \
00083   free(name->cond); \
00084   free(name); \
00085   return 0; \
00086 } \
00087   \
00088 int name##_Add( name##_p name, node_type##_t* node ) \
00089 { \
00090   MUTEX_LOCK(name->lock); \
00091   ListAdd(name->list, node); \
00092   name->size++; \
00093   COND_SIGNAL(name->cond); \
00094   MUTEX_UNLOCK(name->lock); \
00095   return 0; \
00096 } \
00097   \
00098 node_type##_t* name##_Pop( name##_p name ) \
00099 { \
00100   node_type##_t* ret; \
00101   MUTEX_LOCK(name->lock); \
00102   if (name->size <= 0) { \
00103     MUTEX_UNLOCK(name->lock); \
00104     return NULL; \
00105   } \
00106   ret = ListPop(name->list); \
00107   name->size--; \
00108   COND_SIGNAL(name->cond); \
00109   MUTEX_UNLOCK(name->lock); \
00110   return ret; \
00111 } \
00112   \
00113 node_type##_t* name##_SearchIndex( name##_p name, int index ) \
00114 { \
00115   node_type##_t* node; \
00116   MUTEX_LOCK(name->lock); \
00117   node = (node_type##_t*)ListSearch(name->list, index); \
00118   MUTEX_UNLOCK(name->lock); \
00119   return node; \
00120 } \
00121   \
00122 int name##_RemoveIndex( name##_p name, int index ) \
00123 { \
00124   node_type##_t* node; \
00125   MUTEX_LOCK(name->lock); \
00126   node = ListDelete(name->list, index); \
00127   node_type##_Destroy(node); \
00128   name->size--; \
00129   MUTEX_UNLOCK(name->lock); \
00130   return 0; \
00131 } 
00132 
00133 #define AP_QUEUE_SEARCH_TEMPLATE( name, func_name, node_type, \
00134     search_type, search_expression ) \
00135 node_type##_t* name##_##func_name( name##_p name, const search_type key ) \
00136 { \
00137   listNode_t* parsenode; \
00138   node_type##_t* node; \
00139   node_type##_t* ret = NULL; \
00140   node = NULL; \
00141   \
00142   MUTEX_LOCK(name->lock); \
00143   if (name->list->listhead == NULL) { \
00144     MUTEX_UNLOCK(name->lock); \
00145     return NULL; \
00146   } \
00147   for( \
00148       parsenode = (listNode_t*)name->list->listhead; \
00149       parsenode != NULL; \
00150       parsenode = (listNode_t*)parsenode->next \
00151      ) \
00152   { \
00153     node = (node_type##_t*)parsenode->node_data; \
00154     if (search_expression){ \
00155       ret = node; \
00156       break; \
00157     } \
00158   } \
00159   MUTEX_UNLOCK(name->lock); \
00160   return ret; \
00161 } 
00162 
00163 #define AP_QUEUE_REMOVE_TEMPLATE( name, func_name, node_type, \
00164     search_type, search_expression) \
00165 int name##_##func_name( name##_p name, const search_type key ) \
00166 { \
00167   int err_code = MC_ERR_NOT_FOUND; \
00168   listNode_t* parsenode; \
00169   node_type##_t* node; \
00170   node = NULL; \
00171   \
00172   MUTEX_LOCK(name->lock); \
00173   if (name->list->listhead == NULL) { \
00174     MUTEX_UNLOCK(name->lock); \
00175     return MC_ERR_NOT_FOUND; \
00176   } \
00177   for( \
00178       parsenode = (listNode_t*)name->list->listhead; \
00179       parsenode->next != NULL; \
00180       parsenode = (listNode_t*)parsenode->next \
00181      ) \
00182   { \
00183     node = (node_type##_t*)parsenode->node_data; \
00184     if (search_expression) { \
00185       break; \
00186       err_code = MC_SUCCESS; \
00187     } \
00188   } \
00189   MUTEX_UNLOCK(name->lock); \
00190   return err_code; \
00191 }
00192 #endif

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4