X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink_queue.h;h=b6e63c67da833ce48f1bfb02caf5ee28e158f89e;hb=5d583af29b3ba76f9acaeb77c0e0457268776dcb;hp=d075f1c8497b887ea17cc0e171c39a6c4fcda7f0;hpb=fe5563f92021618b4a8b41e412c73d8364fcaf6e;p=meshlink diff --git a/src/meshlink_queue.h b/src/meshlink_queue.h index d075f1c8..b6e63c67 100644 --- a/src/meshlink_queue.h +++ b/src/meshlink_queue.h @@ -72,7 +72,7 @@ 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((item = queue->head)) { @@ -84,7 +84,31 @@ 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; + + pthread_mutex_lock(&queue->mutex); + + 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; }