00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _WIN32
00035 #include "config.h"
00036 #else
00037 #include "winconfig.h"
00038 #endif
00039
00040 #include "include/barrier.h"
00041 #include "include/mc_error.h"
00042
00043
00044 barrier_node_p
00045 barrier_node_Initialize(int id, int num_registered)
00046 {
00047 barrier_node_p node;
00048
00049
00050 node = (barrier_node_p)malloc(sizeof(barrier_node_t));
00051 CHECK_NULL(node, return NULL;);
00052 node->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00053 CHECK_NULL(node->lock, return NULL;);
00054 node->cond = (COND_T*)malloc(sizeof(COND_T));
00055 CHECK_NULL(node->cond, return NULL;);
00056
00057
00058 MUTEX_INIT(node->lock);
00059 COND_INIT(node->cond);
00060
00061
00062 node->id = id;
00063 node->num_registered = num_registered;
00064 node->num_waiting = 0;
00065
00066 return node;
00067 }
00068
00069 int
00070 barrier_node_Destroy(barrier_node_p node)
00071 {
00072
00073 MUTEX_DESTROY(node->lock);
00074 COND_DESTROY(node->cond);
00075
00076
00077 free(node->cond);
00078 free(node->lock);
00079 free(node);
00080
00081 return MC_SUCCESS;
00082 }
00083
00084
00085
00086 int
00087 barrier_queue_Add(barrier_queue_p list, barrier_node_p node)
00088 {
00089
00090 int err_code = MC_SUCCESS;
00091 listNode_t *tmp;
00092 RWLOCK_WRLOCK(list->lock);
00093 tmp = (listNode_t*)list->list->listhead;
00094 while(tmp != NULL) {
00095 if (((barrier_node_p)(tmp->node_data))->id == node->id) {
00096 fprintf(stderr,
00097 "Warning: Barrier node id %d reregistered. %s:%d\n",
00098 node->id, __FILE__, __LINE__ );
00099 err_code = MC_WARN_DUPLICATE;
00100 continue;
00101 }
00102 tmp = tmp->next;
00103 }
00104 ListAdd(list->list, (DATA) node);
00105 list->size++;
00106 RWLOCK_WRUNLOCK(list->lock);
00107 return err_code;
00108 }
00109
00110 int
00111 barrier_queue_Delete(int id, barrier_queue_p list)
00112 {
00113 int i;
00114 barrier_node_p tmp;
00115 RWLOCK_WRLOCK(list->lock);
00116 for (i = 0; i < list->list->size; i++) {
00117 tmp = (barrier_node_p)ListSearch(list->list, i);
00118 if (tmp->id == id) {
00119 ListDelete(list->list, i);
00120 barrier_node_Destroy(tmp);
00121 list->size--;
00122 RWLOCK_WRUNLOCK(list->lock);
00123 return MC_SUCCESS;
00124 }
00125 }
00126 RWLOCK_WRUNLOCK(list->lock);
00127 return MC_ERR_NOT_FOUND;
00128 }
00129
00130 int
00131 barrier_queue_Destroy(barrier_queue_p queue)
00132 {
00133 barrier_node_p node;
00134 while((node = barrier_queue_Pop(queue)) != NULL) {
00135 barrier_node_Destroy(node);
00136 }
00137 ListTerminate(queue->list);
00138 RWLOCK_DESTROY(queue->lock);
00139 free(queue->lock);
00140 free(queue);
00141 return MC_SUCCESS;
00142 }
00143
00144 barrier_node_p
00145 barrier_queue_Get(barrier_queue_p list, int id)
00146 {
00147 listNode_t* tmp;
00148 RWLOCK_RDLOCK(list->lock);
00149 tmp = (listNode_t*)list->list->listhead;
00150 while (tmp != NULL) {
00151 if (((barrier_node_p)(tmp->node_data))->id == id) {
00152 RWLOCK_RDUNLOCK(list->lock);
00153 return ((barrier_node_p)tmp->node_data);
00154 }
00155 tmp = tmp->next;
00156 }
00157 RWLOCK_RDUNLOCK(list->lock);
00158 return NULL;
00159 }
00160
00161 barrier_queue_p
00162 barrier_queue_New(void)
00163 {
00164 barrier_queue_p new_list;
00165 new_list = (barrier_queue_p)malloc(sizeof(barrier_queue_t));
00166 CHECK_NULL(new_list, return NULL;);
00167 new_list->lock = (RWLOCK_T*)malloc(sizeof(RWLOCK_T));
00168 CHECK_NULL(new_list->lock, return NULL;);
00169 RWLOCK_INIT(new_list->lock);
00170
00171 new_list->list = ListInitialize();
00172 return new_list;
00173 }
00174
00175 barrier_node_p
00176 barrier_queue_Pop(barrier_queue_p queue)
00177 {
00178 barrier_node_p node = (barrier_node_p)ListPop(queue->list);
00179 return node;
00180 }
00181