Skip to content

Commit

Permalink
added lock and unlock macros, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Young committed May 8, 2015
1 parent 312d4d0 commit 396e6e7
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/queue.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef QUEUE_H_
#define QUEUE_H_

#include <stdlib.h> /* malloc, free */
#include <stdlib.h>
#include <pthread.h>

typedef struct {
Expand All @@ -18,21 +18,6 @@ typedef struct queue_handle {
void *next;
} queue_handle;

/*
pthread_mutex_lock(&queue_mutex);
while (QUEUE_SIZE(queue) == 0) {
pthread_cond_wait(&queue_cond, &queue_mutex);
}
QUEUE_POP(queue, client);
pthread_mutex_unlock(&queue_mutex);
pthread_mutex_lock(&queue_mutex);
QUEUE_PUSH(queue, client);
pthread_mutex_unlock(&queue_mutex);
pthread_cond_signal(&queue_cond); // broadcast to all consumers??
*/

#define QUEUE_INIT(q) \
do { \
(q)->qh.qc = malloc(sizeof (queue_core)); \
Expand All @@ -53,14 +38,14 @@ pthread_cond_signal(&queue_cond); // broadcast to all consumers??
pthread_mutex_lock(&qc->mutex); \
(e)->qh.qc = qc; \
(e)->qh.next = NULL; \
backqh = qc->backqh; \
backqh = qc->backqh; \
if (!qc->front) { /* empty queue */ \
qc->front = qc->back = (e); \
} else { /* non-empty queue */ \
backqh->next = (e); \
backqh->next = (e); \
qc->back = (e); \
} \
backqh = &(e)->qh; \
backqh = &(e)->qh; \
qc->size++; \
pthread_mutex_unlock(&qc->mutex); \
pthread_cond_signal(&qc->cond); /* broadcast to all? */ \
Expand All @@ -85,9 +70,15 @@ pthread_cond_signal(&queue_cond); // broadcast to all consumers??
} \
} while (0)

#define QUEUE_LOCK(q) \
pthread_mutex_lock(&q->qh.qc->mutex);

#define QUEUE_SIZE(q) \
((q) ? (q)->qh.qc->size : 0U)

#define QUEUE_UNLOCK(q) \
pthread_mutex_unlock(&q->qh.qc->mutex);

#define QUEUE_FREE(q) \
do { \
if ((q) && (q)->qh.qc) { \
Expand Down

0 comments on commit 396e6e7

Please sign in to comment.