#include "queue.h"
Go to the source code of this file.
Defines | |
| #define | DESTROY_QUEUE() |
Functions | |
| QUEUE * | new_queue () |
| int | destroy_queue (QUEUE *q) |
| int | enqueue (QUEUE *q, void *val) |
| void * | dequeue (QUEUE *q) |
Implementation of a queue.
Definition in file queue.c.
| #define DESTROY_QUEUE | ( | ) |
| void* dequeue | ( | QUEUE * | q | ) |
Removes an item from the end of the queue.
| q | -- the queue whose last item should be removed |
Definition at line 129 of file queue.c.
{
void *retval;
Q_ITEM *tail;
if (!q->tail)
return NULL;
tail = q->tail;
if (q->tail->prev)
q->tail->prev->next = NULL;
q->tail = q->tail->prev;
if (!q->tail)
q->head = NULL;
retval = tail->val;
free(tail);
return retval;
}

| int destroy_queue | ( | QUEUE * | q | ) |
Frees the memory associated with the specified queue.
| q | -- the queue to be destroyed |
Definition at line 75 of file queue.c.
{
pthread_mutex_destroy(q->mutex);
pthread_cond_destroy(q->notFull);
pthread_cond_destroy(q->notEmpty);
while (dequeue(q)) /* spin */
;
DESTROY_QUEUE();
return 0;
}


| int enqueue | ( | QUEUE * | q, | |
| void * | val | |||
| ) |
Adds an item to the front of the queue.
| q | -- the queue to which the item should be added | |
| val | -- pointer to the item to be added to the queue |
Definition at line 97 of file queue.c.
{
Q_ITEM *item;
item = (Q_ITEM *) malloc(sizeof(Q_ITEM));
if (!item)
return -1;
item->val = val;
item->next = q->head;
item->prev = NULL;
if (q->head)
q->head->prev = item;
q->head = item;
if (!q->tail)
q->tail = item;
return 0;
}

| QUEUE* new_queue | ( | ) |
Creates a new queue.
Definition at line 25 of file queue.c.
{
QUEUE *q;
q = (QUEUE *) malloc(sizeof(QUEUE));
if (!q)
return NULL;
q->head = q->tail = NULL;
q->mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
q->notFull = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
q->notEmpty = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
if (!q->mutex || !q->notFull || !q->notEmpty) {
DESTROY_QUEUE();
return NULL;
}
if (pthread_mutex_init(q->mutex, NULL)) {
DESTROY_QUEUE();
return NULL;
}
if (pthread_cond_init(q->notFull, NULL)) {
pthread_mutex_destroy(q->mutex);
DESTROY_QUEUE();
return NULL;
}
if (pthread_cond_init(q->notEmpty, NULL)) {
pthread_mutex_destroy(q->mutex);
pthread_cond_destroy(q->notFull);
DESTROY_QUEUE();
return NULL;
}
return q;
}

1.6.3-20100507