/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/mc_sync/sync_list.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 207 $ : Last Committed Revision
00003  * $Date: 2008-07-11 17:55:19 -0700 (Fri, 11 Jul 2008) $ : Last Committed Date */
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* new;
00075     new = (struct syncList_s*)malloc(sizeof(struct syncList_s));
00076     new->lock = (RWLOCK_T*)malloc(sizeof(RWLOCK_T));
00077     RWLOCK_INIT(new->lock);
00078 
00079     new->giant_lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00080     MUTEX_INIT(new->giant_lock);
00081 
00082     new->list = ListInitialize();
00083     return new;
00084 } /*}}}*/
00085 
00086 int syncListAddNode(struct syncListNode_s *node, struct syncList_s *list) { /*{{{*/
00087     /* Check to see if there are identical ID nums */
00088     listNode_t *tmp;
00089     RWLOCK_WRLOCK(list->lock);
00090     tmp = (listNode_t *)list->list->listhead;
00091     while (tmp != NULL) {
00092         if (((syncListNode_t*)(tmp->node_data))->id == node->id) {
00093             fprintf(stderr, 
00094                     "Warning: Identical COND ID's! %s:%d\n",
00095                     __FILE__, __LINE__);
00096             continue;
00097         }
00098         tmp = tmp->next;
00099     }
00100     ListAdd( list->list, (DATA) node);
00101     RWLOCK_WRUNLOCK(list->lock);
00102     return 0;
00103 } /*}}}*/
00104 
00105 int syncListNew(int id, struct syncList_s *list) { /*{{{*/
00106     syncListNode_t *node;
00107     node = (syncListNode_t *)malloc(sizeof(syncListNode_t));
00108     syncListNodeInit(node);
00109     syncListAddNode(
00110             node,
00111             list);
00112     return id;
00113 } /*}}}*/
00114 
00115 int syncListDelete(int id, struct syncList_s *list) { /*{{{*/
00116     int i;
00117     syncListNode_t *tmp;
00118     RWLOCK_WRLOCK(list->lock);
00119     for(i = 0; i < list->list->size; i++) {
00120         tmp = (syncListNode_t*)ListSearch(list->list, i);
00121         if(tmp->id == id) {
00122             ListDelete(list->list, i);
00123             syncListNodeDestroy(tmp);
00124             RWLOCK_WRUNLOCK(list->lock);
00125             return 0;
00126         }
00127     }
00128     RWLOCK_WRUNLOCK(list->lock);
00129     return MC_ERR_NOT_FOUND;
00130 }
00131         
00132 syncListNode_t* syncListRemove(int id, struct syncList_s *list) { /*{{{*/
00133     int i;
00134     syncListNode_t *tmp;
00135     RWLOCK_WRLOCK(list->lock);
00136     for(i = 0; i < list->list->size; i++) {
00137         tmp = (syncListNode_t*)ListSearch(list->list, i);
00138         if (tmp == NULL) {
00139             /* Not found */
00140             RWLOCK_WRUNLOCK(list->lock);
00141             return NULL;
00142         }
00143         if(tmp->id == id) {
00144             if (ListDelete(list->list, i) == NULL) {
00145                 fprintf(stderr, "Fatal error. %s:%d\n",
00146                         __FILE__,
00147                         __LINE__ );
00148                 exit(1);
00149             }
00150             RWLOCK_WRUNLOCK(list->lock);
00151             return tmp;
00152         }
00153     }
00154     RWLOCK_WRUNLOCK(list->lock);
00155     return NULL;
00156 }

Generated on Fri Jul 11 17:59:45 2008 for Mobile-C by  doxygen 1.5.4