7 // -------------------------
8 List* createEmptyList (void) {
13 l=malloc(sizeof(List));
19 pthread_cond_init(&l->cond, NULL);
20 pthread_mutex_init(&l->mutex, NULL);
30 // ------------------------------------------------
31 void destroyList (List *l, void (*freefunc)(void*)) {
43 pthread_mutex_destroy(&l->mutex);
44 pthread_cond_destroy(&l->cond);
47 for (ln=l->first; ln!=NULL; ) {
51 if ( freefunc!=NULL ) {
67 // -------------------------------------
68 void pushFrontList (List *l, void* data) {
79 pthread_mutex_lock(&l->mutex);
82 ln=malloc(sizeof(ListNode));
87 if ( l->first==NULL ) {
97 pthread_mutex_unlock(&l->mutex);
98 pthread_cond_broadcast(&l->cond);
105 // ------------------------------------
106 void pushBackList (List *l, void* data) {
117 pthread_mutex_lock(&l->mutex);
120 ln=malloc(sizeof(ListNode));
125 if ( l->last==NULL ) {
135 pthread_mutex_unlock(&l->mutex);
136 pthread_cond_broadcast(&l->cond);
143 // -------------------------
144 void* popFrontList (List *l) {
156 pthread_mutex_lock(&l->mutex);
159 if ( (ln=l->first)==NULL ) {
161 pthread_mutex_unlock(&l->mutex);
169 if ( ln->next==NULL ) {
179 pthread_mutex_unlock(&l->mutex);
180 pthread_cond_broadcast(&l->cond);
190 // ------------------------
191 void* popBackList (List *l) {
204 pthread_mutex_lock(&l->mutex);
207 if ( (ln=l->last)==NULL ) {
209 pthread_mutex_unlock(&l->mutex);
217 if ( ln->prev==NULL ) {
227 pthread_mutex_unlock(&l->mutex);
228 pthread_cond_broadcast(&l->cond);
238 // ----------------------------------------------
239 void clearList (List *l, void (*freefunc)(void*)) {
250 pthread_mutex_lock(&l->mutex);
253 for (ln=l->first; ln!=NULL; ) {
257 if ( freefunc!=NULL ) {
271 pthread_mutex_unlock(&l->mutex);
272 pthread_cond_broadcast(&l->cond);
279 static void __destroyElement (List *l, ListNode *ln, void (*freefunc)(void*)) {
282 if ( ln->prev==NULL ) {
285 ln->prev->next=ln->next;
288 if ( ln->next==NULL ) {
291 ln->next->prev=ln->prev;
295 if ( freefunc!=NULL ) {
306 // -----------------------------------------------------------------
307 bool destroyElement (List *l, ListNode *ln, void (*freefunc)(void*)) {
310 if ( l==NULL || ln==NULL ) {
316 pthread_mutex_lock(&l->mutex);
319 __destroyElement(l, ln, freefunc);
322 pthread_mutex_unlock(&l->mutex);
332 // ---------------------------------------------------------------
333 bool findAndDestroy (List *l, void* data, void (*freefunc)(void*)) {
345 pthread_mutex_lock(&l->mutex);
348 for (ln=l->first; ln!=NULL && ln->data!=data; ln=ln->next);
353 pthread_mutex_unlock(&l->mutex);
358 __destroyElement(l, ln, freefunc);
361 pthread_mutex_unlock(&l->mutex);
362 pthread_cond_broadcast(&l->cond);
375 // -------------------------------------------------
376 void browseList (List *l, void (*browsefunc)(void*)) {
382 if ( l==NULL || browsefunc==NULL ) {
388 pthread_mutex_lock(&l->mutex);
391 for (ln=l->first; ln!=NULL; ln=ln->next) {
392 browsefunc(ln->data);
396 pthread_mutex_unlock(&l->mutex);
402 // --------------------------------------
403 void* convertToArray (List *l, size_t sz) {
410 if ( l==NULL || sz==0 ) {
416 pthread_mutex_lock(&l->mutex);
421 tab=malloc(l->count*sz);
424 for (ln=l->first; ln!=NULL; ln=ln->next) {
425 memcpy(p, ln->data, sz);
432 pthread_mutex_unlock(&l->mutex);