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