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