X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink_queue.h;h=665c4d6e8d7553ec090d56268445224cc0fdb4bd;hb=4b6c01b1d5383b1a7417244a31ad4652aab2d5db;hp=d075f1c8497b887ea17cc0e171c39a6c4fcda7f0;hpb=fe5563f92021618b4a8b41e412c73d8364fcaf6e;p=meshlink diff --git a/src/meshlink_queue.h b/src/meshlink_queue.h index d075f1c8..665c4d6e 100644 --- a/src/meshlink_queue.h +++ b/src/meshlink_queue.h @@ -39,10 +39,7 @@ typedef struct meshlink_queue_item { static inline void meshlink_queue_init(meshlink_queue_t *queue) { queue->head = NULL; queue->tail = NULL; - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT); - pthread_mutex_init(&queue->mutex, &attr); + pthread_mutex_init(&queue->mutex, NULL); } static inline void meshlink_queue_exit(meshlink_queue_t *queue) { @@ -58,7 +55,10 @@ static inline __attribute__((__warn_unused_result__)) bool meshlink_queue_push(m item->data = data; item->next = NULL; - pthread_mutex_lock(&queue->mutex); + + if(pthread_mutex_lock(&queue->mutex) != 0) { + abort(); + } if(!queue->tail) { queue->head = queue->tail = item; @@ -72,8 +72,10 @@ static inline __attribute__((__warn_unused_result__)) bool meshlink_queue_push(m static inline __attribute__((__warn_unused_result__)) void *meshlink_queue_pop(meshlink_queue_t *queue) { meshlink_queue_item_t *item; - void *data; - pthread_mutex_lock(&queue->mutex); + + if(pthread_mutex_lock(&queue->mutex) != 0) { + abort(); + } if((item = queue->head)) { queue->head = item->next; @@ -84,7 +86,33 @@ static inline __attribute__((__warn_unused_result__)) void *meshlink_queue_pop(m } pthread_mutex_unlock(&queue->mutex); - data = item ? item->data : NULL; + + void *data = item ? item->data : NULL; + free(item); + return data; +} + +static inline __attribute__((__warn_unused_result__)) void *meshlink_queue_pop_cond(meshlink_queue_t *queue, pthread_cond_t *cond) { + meshlink_queue_item_t *item; + + if(pthread_mutex_lock(&queue->mutex) != 0) { + abort(); + } + + while(!queue->head) { + pthread_cond_wait(cond, &queue->mutex); + } + + item = queue->head; + queue->head = item->next; + + if(!queue->head) { + queue->tail = NULL; + } + + pthread_mutex_unlock(&queue->mutex); + + void *data = item->data; free(item); return data; }