]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink_queue.h
Don't use assert() to check the results of pthread_*() calls.
[meshlink] / src / meshlink_queue.h
index c86bf660ed22af5c4ea262932cf4e0a3de640ab0..665c4d6e8d7553ec090d56268445224cc0fdb4bd 100644 (file)
@@ -1,6 +1,9 @@
+#ifndef MESHLINK_QUEUE_H
+#define MESHLINK_QUEUE_H
+
 /*
     queue.h -- Thread-safe queue
-    Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
+    Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -17,9 +20,6 @@
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef MESHLINK_QUEUE_H
-#define MESHLINK_QUEUE_H
-
 #include <pthread.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -36,32 +36,83 @@ typedef struct meshlink_queue_item {
        struct meshlink_queue_item *next;
 } meshlink_queue_item_t;
 
-static inline bool meshlink_queue_push(meshlink_queue_t *queue, void *data) {
-       meshlink_queue_item_t *item = malloc(sizeof *item);
-       if(!item)
+static inline void meshlink_queue_init(meshlink_queue_t *queue) {
+       queue->head = NULL;
+       queue->tail = NULL;
+       pthread_mutex_init(&queue->mutex, NULL);
+}
+
+static inline void meshlink_queue_exit(meshlink_queue_t *queue) {
+       pthread_mutex_destroy(&queue->mutex);
+}
+
+static inline __attribute__((__warn_unused_result__)) bool meshlink_queue_push(meshlink_queue_t *queue, void *data) {
+       meshlink_queue_item_t *item = malloc(sizeof(*item));
+
+       if(!item) {
                return false;
+       }
+
        item->data = data;
        item->next = NULL;
-       pthread_mutex_lock(&queue->mutex);
-       if(!queue->tail)
+
+       if(pthread_mutex_lock(&queue->mutex) != 0) {
+               abort();
+       }
+
+       if(!queue->tail) {
                queue->head = queue->tail = item;
-       else
+       } else {
                queue->tail = queue->tail->next = item;
+       }
+
        pthread_mutex_unlock(&queue->mutex);
        return true;
 }
 
-static inline void *meshlink_queue_pop(meshlink_queue_t *queue) {
+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;
-               if(!queue->head)
+
+               if(!queue->head) {
                        queue->tail = NULL;
+               }
+       }
+
+       pthread_mutex_unlock(&queue->mutex);
+
+       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);
-       data = item ? item->data : NULL;
+
+       void *data = item->data;
        free(item);
        return data;
 }