Listing 10 (que_hand.c)

/**************************************************
 * NAME       : insert_one
 *
 * DESCRIPTION: insert a item into the item queue.
 *   This queue works as a circular buffer with two
 *   pointers, insert and delete. As a character is
 *   inserted the insert pointer is incremented
 *   modulo que length. In other words after each
 *   increment the pointer is checked to see if it
 *   equals the length of the que. If so, then it
 *   is reset to zero. When the pointers are equal
 *   then the que is full. The insert pointer is always
 *   incremented first then the item is inserted.
 *   The empty flag is always set false if a item
 *   is inserted.
 ******************************************************/

#include "que.h"

insert_one(item, k_que)
unsigned char item;
struct g_queue *k_que;
{
   
   unsigned int insert,remove;
   
   while (((k_que->insert)+1==k_que->remove) 
        ((k_que->insert)-(k_que->remove)==kqlength-1))
              suspend();      /* queue is full */
   
   set_irq();
   insert = k_que->insert;
   remove = k_que->remove;
   (*k_que).que[insert] = item;
   if (++insert == kqlength)
      insert = 0;
   k_que->insert = insert;
   k_que->empty = false;
   clear_irq();
}

/***********************************************
 * NAME       : remove_one
 *
 * DESCRIPTION: remove a item from the item queue.
 *  This queue works as a circular buffer with two
 *  pointers insert, and delete. as a character is
 *  removed the remove pointer is incremented modulo
 *  que length. In other words after each increment
 *  the pointer is checked to see if it equals the
 *  length of the que. If so, then it is reset
 *  to zero. When the pointers are equal then the
 *  que is empty. And the que empty flag is set
 *  true. The remove pointer is always pointing to
 *  the next character to be removed. When the
 *  remove pointer is equal to the insert pointer
 *  - 1 then the queue is full
 *
 ***************************************************/

remove_one(k_que)
struct g_queue *k_que;
{

   unsigned int insert,remove;
   unsigned char item;
   
   while (k_que->empty == true)
      suspend();
   set_irq();
   insert = k_que->insert;
   remove = k_que->remove;
   item = k_que->que[remove];
   if (++remove == kqlength)
      remove = 0;
   if ( remove == insert )
      k_que->empty = true;
   
   k_que->remove = remove;
   clear_irq();
   return item;
}