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 int syncListAddNode(struct syncListNode_s *node, list_t *list) {
00057
00058 syncListNode_t *tmp;
00059 tmp = (syncListNode_t*)ListSearchCB(
00060 list,
00061 &node->id,
00062 (ListSearchFunc_t)syncListNode_CmpID);
00063 if(tmp) {
00064 fprintf(stderr,
00065 "Warning: Identical COND ID's! %s:%d\n",
00066 __FILE__, __LINE__);
00067 }
00068 ListAdd( list, (void*) node);
00069 return 0;
00070 }
00071
00072 int syncListNew(int id, list_t *list) {
00073 syncListNode_t *node;
00074 node = (syncListNode_t *)malloc(sizeof(syncListNode_t));
00075 syncListNodeInit(node);
00076 node->id = id;
00077 syncListAddNode(
00078 node,
00079 list);
00080 return id;
00081 }
00082
00083 int syncListDelete(int id, list_t *list) {
00084 syncListNode_t *tmp;
00085 tmp = (syncListNode_t*)ListDeleteCB(
00086 list, &id, (ListSearchFunc_t)syncListNode_CmpID);
00087 if(tmp) {
00088 syncListNodeDestroy(tmp);
00089 return 0;
00090 }
00091 return MC_ERR_NOT_FOUND;
00092 }
00093
00094 syncListNode_t* syncListRemove(int id, list_t *list) {
00095 syncListNode_t *tmp;
00096 tmp = (syncListNode_t*)ListDeleteCB(
00097 list, &id, (ListSearchFunc_t)syncListNode_CmpID);
00098 return tmp;
00099 }
00100
00101 int syncListNode_CmpID(int* key, syncListNode_t* node)
00102 {
00103 return *key - node->id;
00104 }