home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03042a < prev    next >
Text File  |  1991-01-29  |  2KB  |  81 lines

  1. /***************************************************
  2.  * NAME       : insert_one
  3.  *
  4.  * DESCRIPTION: insert a item into the item queue.
  5.  *   This queue works as a circular buffer with two 
  6.  *   pointers, insert and delete. As a character is 
  7.  *   inserted the insert pointer is incremented 
  8.  *   modulo que length.  In other words after each 
  9.  *   increment the pointer is checked to see if it
  10.  *   equals the length of the que.  If so, then it 
  11.  *   is reset to zero.  When the pointers are equal 
  12.  *   then the que is full. The insert pointer is always 
  13.  *   incremented first then the item is inserted.
  14.  *   The empty flag is always set false if a item 
  15.  *   is inserted.
  16.  ******************************************************/
  17.  
  18. #include "que.h"
  19.  
  20. insert_one(item, k_que)
  21. unsigned char item;
  22. struct g_queue *k_que;
  23. {
  24.    unsigned int insert,remove;
  25.  
  26.    while (((k_que->insert)+1==k_que->remove) ||
  27.          ((k_que->insert)-(k_que->remove)==kqlength-1))
  28.                 suspend();      /* queue is full */
  29.    set_irq();
  30.    insert = k_que->insert;
  31.    remove = k_que->remove;
  32.    (*k_que).que[insert] = item;
  33.    if (++insert == kqlength)
  34.       insert = 0;
  35.    k_que->insert = insert;
  36.    k_que->empty = false;
  37.    clear_irq();
  38. }
  39.  
  40.  
  41. /************************************************
  42.  * NAME       : remove_one
  43.  *
  44.  * DESCRIPTION: remove a item from the item queue.
  45.  *  This queue works as a circular buffer with two 
  46.  *  pointers insert, and delete. as a character is 
  47.  *  removed the remove pointer is incremented modulo 
  48.  *  que length.  In other words after each increment 
  49.  *  the pointer is checked to see if it equals the 
  50.  *  length of the que.  If so, then it is reset
  51.  *  to zero.  When the pointers are equal then the 
  52.  *  que is empty. And the que empty flag is set 
  53.  *  true. The remove pointer is always pointing to 
  54.  *  the next character to be removed.  When the 
  55.  *  remove pointer is equal to the insert pointer 
  56.  *  - 1 then the queue is full
  57.  *
  58.  ****************************************************/
  59.  
  60. remove_one(k_que)
  61. struct g_queue *k_que;
  62. {
  63.    unsigned int insert,remove;
  64.    unsigned char item;
  65.  
  66.    while (k_que->empty == true)
  67.       suspend();
  68.    set_irq();
  69.    insert = k_que->insert;
  70.    remove = k_que->remove;
  71.    item = k_que->que[remove];
  72.    if (++remove == kqlength)
  73.       remove = 0;
  74.    if ( remove == insert )
  75.       k_que->empty = true;
  76.    k_que->remove = remove;
  77.    clear_irq();
  78.    return item;
  79. }
  80.  
  81.