Defines | Functions

queue.c File Reference

#include "queue.h"
Include dependency graph for queue.c:

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)

Detailed Description

Implementation of a queue.

Definition in file queue.c.


Define Documentation

 
#define DESTROY_QUEUE (  ) 
Value:
{ if(q->mutex) free(q->mutex); \
    if(q->notFull) free(q->notFull); \
    if(q->notEmpty) free(q->notEmpty); \
    free(q); }

Frees the memory associated with the queue

Definition at line 13 of file queue.c.


Function Documentation

void* dequeue ( QUEUE *  q  ) 

Removes an item from the end of the queue.

Parameters:
q -- the queue whose last item should be removed
Returns:
pointer to the item removed from the queue

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;
}

Here is the caller graph for this function:

int destroy_queue ( QUEUE *  q  ) 

Frees the memory associated with the specified queue.

Parameters:
q -- the queue to be destroyed
Returns:
0 on success, -1 on failure

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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int enqueue ( QUEUE *  q,
void *  val 
)

Adds an item to the front of the queue.

Parameters:
q -- the queue to which the item should be added
val -- pointer to the item to be added to the queue
Returns:
0 on success, -1 on failure

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;
}

Here is the caller graph for this function:

QUEUE* new_queue (  ) 

Creates a new queue.

Returns:
pointer to the 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;
}

Here is the caller graph for this function: