/home/dko/projects/mobilec/trunk/src/barrier.c

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 #include "include/barrier.h"
00033 #include "include/mc_error.h"
00034 
00035 /* Node Funcs */
00036   barrier_node_p
00037 barrier_node_Initialize(int id, int num_registered) 
00038 {
00039   barrier_node_p node;
00040 
00041   /* Memory Allocation */
00042   node = (barrier_node_p)malloc(sizeof(barrier_node_t));
00043   CHECK_NULL(node, return NULL;);
00044   node->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00045   CHECK_NULL(node->lock, return NULL;);
00046   node->cond = (COND_T*)malloc(sizeof(COND_T));
00047   CHECK_NULL(node->cond, return NULL;);
00048 
00049   /* Sync Init */
00050   MUTEX_INIT(node->lock);
00051   COND_INIT(node->cond);
00052 
00053   /* Initial values */
00054   node->id = id;
00055   node->num_registered = num_registered;
00056   node->num_waiting = 0;
00057 
00058   return node;
00059 }
00060 
00061   int
00062 barrier_node_Destroy(barrier_node_p node)
00063 {
00064   /* Sync Destroy */
00065   MUTEX_DESTROY(node->lock);
00066   COND_DESTROY(node->cond);
00067 
00068   /* Free Memory */
00069   free(node->cond);
00070   free(node->lock);
00071   free(node);
00072 
00073   return MC_SUCCESS;
00074 }
00075 
00076 /* List Funcs */
00077 
00078   int
00079 barrier_queue_Add(barrier_queue_p list, barrier_node_p node)
00080 {
00081   /* Check for identical IDs and warn */
00082   int err_code = MC_SUCCESS;
00083   listNode_t *tmp;
00084   RWLOCK_WRLOCK(list->lock);
00085   tmp = (listNode_t*)list->list->listhead;
00086   while(tmp != NULL) {
00087     if (((barrier_node_p)(tmp->node_data))->id == node->id) {
00088       fprintf(stderr,
00089           "Warning: Barrier node id %d reregistered. %s:%d\n",
00090           node->id, __FILE__, __LINE__ );
00091       err_code = MC_WARN_DUPLICATE;
00092       continue;
00093     }
00094     tmp = tmp->next;
00095   }
00096   ListAdd(list->list, (DATA) node);
00097   list->size++;
00098   RWLOCK_WRUNLOCK(list->lock);
00099   return err_code;
00100 }
00101 
00102   int 
00103 barrier_queue_Delete(int id, barrier_queue_p list)
00104 {
00105   int i;
00106   barrier_node_p tmp;
00107   RWLOCK_WRLOCK(list->lock);
00108   for (i = 0; i < list->list->size; i++) {
00109     tmp = (barrier_node_p)ListSearch(list->list, i);
00110     if (tmp->id == id) {
00111       ListDelete(list->list, id);
00112       barrier_node_Destroy(tmp);
00113       list->size--;
00114       RWLOCK_WRUNLOCK(list->lock);
00115       return MC_SUCCESS;
00116     }
00117   }
00118   RWLOCK_WRUNLOCK(list->lock);
00119   return MC_ERR_NOT_FOUND;
00120 }
00121 
00122   int
00123 barrier_queue_Destroy(barrier_queue_p queue)
00124 {
00125   barrier_node_p node;
00126   while((node = barrier_queue_Pop(queue)) != NULL) {
00127     barrier_node_Destroy(node);
00128   }
00129   ListTerminate(queue->list);
00130   RWLOCK_DESTROY(queue->lock);
00131   free(queue->lock);
00132   free(queue);
00133   return MC_SUCCESS;
00134 }
00135 
00136   barrier_node_p
00137 barrier_queue_Get(barrier_queue_p list, int id)
00138 {
00139   listNode_t* tmp;
00140   RWLOCK_RDLOCK(list->lock);
00141   tmp = (listNode_t*)list->list->listhead;
00142   while (tmp != NULL) {
00143     if (((barrier_node_p)(tmp->node_data))->id == id) {
00144       RWLOCK_RDUNLOCK(list->lock);
00145       return ((barrier_node_p)tmp->node_data);
00146     }
00147     tmp = tmp->next;
00148   }
00149   RWLOCK_RDUNLOCK(list->lock);
00150   return NULL;
00151 }
00152 
00153   barrier_queue_p
00154 barrier_queue_New(void)
00155 {
00156   barrier_queue_p new_list;
00157   new_list = (barrier_queue_p)malloc(sizeof(barrier_queue_t));
00158   CHECK_NULL(new_list, return NULL;);
00159   new_list->lock = (RWLOCK_T*)malloc(sizeof(RWLOCK_T));
00160   CHECK_NULL(new_list->lock, return NULL;);
00161   RWLOCK_INIT(new_list->lock);
00162 
00163   new_list->list = ListInitialize();
00164   return new_list;
00165 }
00166 
00167   barrier_node_p
00168 barrier_queue_Pop(barrier_queue_p queue)
00169 {
00170   barrier_node_p node = (barrier_node_p)ListPop(queue->list);
00171   return node;
00172 }
00173 

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