home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / MsgOps / fifoCode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  9.0 KB  |  251 lines

  1.  
  2. /* File: /u1/Eden/Kernel/MsgOps/fifoCode.c */
  3.  
  4. /*
  5.  * $Header:   /u1/Eden/Kernel/MsgOps/RCS/fifoCode.v  Revision 1.6  85/03/14 11:42:44  eric Exp$
  6.  * INTERFACE:    Defined by exported procedures.
  7.  *
  8.  * FUNCTION:    Provides the Message Module FIFO Queue Services.
  9.  *
  10.  * IMPORTS:    Eden/Kernel/MsgOps/Types.h,
  11.  *              /u1/Eden/ErrCodes/MMcodes.h,
  12.  *              /u1/Eden/Kernel/MsgOps/BufferTypes.h, 
  13.  *              /u1/Eden/Kernel/MsgOps/FifoTypes.h.
  14.  *
  15.  * EXPORTS:     MMInitQ, MMEnterInQ, MMGetFromQ, MMPurgeQ.
  16.  *
  17.  * DESIGN:      FIFO queues are structured as linked lists where
  18.  *              entries are placed at the end of the queue and
  19.  *              removed from the head of the queue.  Each queue
  20.  *              is of type FifoQueue containing a pointer to
  21.  *              the head and tail of the linked list.  In addition,
  22.  *              the tail of the queue points back to the head.
  23.  *              Each queue entry contains the linkage to the entry
  24.  *              behind it in the queue, a search key, and two words
  25.  *              of caller-defined data.  The search key allows
  26.  *              entries to be removed from the list out of order.
  27.  *
  28.  * $Log:    /u1/Eden/Kernel/MsgOps/RCS/FifoCode.v $
  29.  * Revision 1.6  85/03/14  11:42:44  eric
  30.  * Slight mods to MXTraceMsg.
  31.  *
  32.  * Revision 1.5  83/03/19  11:42:44  cady
  33.  * Modified to allow NULL data and key pointer parameters.
  34.  * 
  35.  * Revision 1.4  83/02/25  12:15:45  cady
  36.  * Added new trace levels.
  37.  * 
  38.  * Revision 1.3  83/02/24  16:39:51  cady
  39.  * Replaced conditional debug trace with dynamic trace.
  40.  * Added conditional Kernel compilation.
  41.  * 
  42.  * Revision 1.2  83/02/22  11:54:21  cady
  43.  * Changed internal routines to be static.
  44.  * Replaced #if Debug with #ifdef Debug.
  45.  * 
  46.  * Revision 1.1  83/01/06  14:08:45  cady
  47.  * Initial revision
  48.  * 
  49.  * 4-Nov-1982    Initial implementation. S. Cady
  50.  */
  51.  
  52. /****************************************************************/
  53. /*                                                              */
  54. /*              Message Module FIFO Queue Library               */
  55. /*                                                              */
  56. /****************************************************************/
  57.  
  58. #include "Kernel/h/mmCodes.h"
  59. #include "Kernel/h/mmBufTypes.h"
  60. #include "Kernel/h/mmFifoTypes.h"
  61. #include "Kernel/h/mmMsgTypes.h"
  62.  
  63. /****************************************************************/
  64. /*                                                              */
  65. /*                        MMInitQ                               */
  66. /*                                                              */
  67. /*   MMInitQ defines the specified FIFO queue to be empty.      */
  68. /*                                                              */
  69. /****************************************************************/
  70.  
  71. void  MMInitQ( fQueue  /* FIFO Queue */
  72.              )
  73.   register FifoQueue *fQueue;
  74. {
  75.   MXTraceMsg(5, "MMInitQ( %d )\n", fQueue);
  76.  
  77.   fQueue->FifoHead = NULL;
  78.   fQueue->FifoTail = NULL;
  79. }
  80.  
  81. /****************************************************************/
  82. /*                                                              */
  83. /*                           MMEnterInQ                         */
  84. /*                                                              */
  85. /*  MMEnterInQ creates an entry containing the specified search */
  86. /*  key and data and inserts it at the end of the FIFO queue.   */
  87. /*                                                              */
  88. /*  Possible status codes:                                      */
  89. /*      MMSS_Success, MMSK_NoMem                                */
  90. /*                                                              */
  91. /****************************************************************/
  92.  
  93. KKStatus  MMEnterInQ( fQueue,   /* FIFO Queue    */
  94.                       fKey,     /* Search Key    */
  95.                       fData1,   /* Entry Data    */
  96.                       fData2
  97.                     )
  98.   register FifoQueue        *fQueue;
  99.            UnsignedInteger   fKey;
  100.            integer           fData1, fData2;
  101. {
  102.   FifoEltPtr NewEntry;
  103.   KKStatus   status;
  104.  
  105.   MXTraceMsg(5, "MMEnterInQ( %d, %d, %d, %d )\n", fQueue,
  106.                        fKey, fData1, fData2);
  107.  
  108.   /* Allocate and initialize new queue element */
  109.  
  110.   status = MMAllocateData(sizeof(FifoElt), (char **) &NewEntry);
  111.   if ( ! mSUCCESS(status) )
  112.     return status;
  113.   NewEntry->FifoKey  = fKey;
  114.   NewEntry->FifoData[0] = fData1;
  115.   NewEntry->FifoData[1] = fData2;
  116.  
  117.   /* Insert new element at end of queue */
  118.  
  119.   if ( fQueue->FifoTail == NULL )
  120.     {   fQueue->FifoHead = NewEntry;
  121.         NewEntry->FifoNext = NewEntry;
  122.     }
  123.   else
  124.     {   fQueue->FifoTail->FifoNext = NewEntry;
  125.         NewEntry->FifoNext = fQueue->FifoHead;
  126.     };
  127.   fQueue->FifoTail = NewEntry;
  128.   return MMSS_Success;
  129. }
  130.  
  131. /****************************************************************/
  132. /*                                                              */
  133. /*                          MMGetFromQ                          */
  134. /*                                                              */
  135. /*   MMGetFromQ removes the entry at the front of the queue and */
  136. /*  returns the entry key and data.                             */
  137. /*                                                              */
  138. /*  Possible status codes:                                      */
  139. /*      MMSS_Success, MMSF_NoEntry                              */
  140. /*                                                              */
  141. /****************************************************************/
  142.  
  143. KKStatus  MMGetFromQ( fQueue,   /* FIFO Queue     */
  144.                       fKey,     /* Search Key     */
  145.                       fData1,   /* Entry Data     */
  146.                       fData2
  147.                     )
  148.   register FifoQueue        *fQueue;
  149.            UnsignedInteger  *fKey;
  150.            integer          *fData1, *fData2;
  151. {
  152.   register FifoEltPtr OldEntry;
  153.  
  154.   MXTraceMsg(5, "MMGetFromQ( %d )\n", fQueue);
  155.  
  156.   /* Get element at head of queue */
  157.  
  158.   if ( fQueue->FifoHead == NULL )
  159.     return MMSF_NoEntry;
  160.   OldEntry = fQueue->FifoHead;
  161.   if ( fKey != NULL )
  162.     *fKey = OldEntry->FifoKey;
  163.   if ( fData1 != NULL )
  164.     *fData1 = OldEntry->FifoData[0];
  165.   if ( fData2 != NULL )
  166.     *fData2 = OldEntry->FifoData[1];
  167.  
  168.   /* Remove from the queue */
  169.  
  170.   if ( fQueue->FifoTail == OldEntry )
  171.     {   fQueue->FifoHead = NULL;
  172.         fQueue->FifoTail = NULL;
  173.     }
  174.   else
  175.     {   fQueue->FifoHead = OldEntry->FifoNext;
  176.         fQueue->FifoTail->FifoNext = fQueue->FifoHead;
  177.     };
  178.  
  179.   /* Deallocate old entry */
  180.  
  181.  
  182.   MMDeallocateData((sizeof(FifoElt)), (char *) OldEntry);
  183.  
  184.   MXTraceMsg(5, "Dequeued: %d, %d, %d\n", *fKey, *fData1, *fData2);
  185.  
  186.   return MMSS_Success;
  187. }
  188.  
  189. /****************************************************************/
  190. /*                                                              */
  191. /*                           MMPurgeQ                           */
  192. /*                                                              */
  193. /*  MMPurgeQ searches the queue for an entry with the specified */
  194. /*  key.  If the key is found, the corresponding entry is       */
  195. /*  removed from the queue.  If the key is not found, no action */
  196. /*  is taken.                                                   */
  197. /*                                                              */
  198. /*  Possible status codes:                                      */
  199. /*      MMSS_Success, MMSF_NoEntry.                             */
  200. /*                                                              */
  201. /****************************************************************/
  202.  
  203. KKStatus  MMPurgeQ( fQueue,    /* FIFO Queue   */
  204.                     fKey,      /* Search Key   */
  205.                     fData1,    /* Entry Data   */
  206.                     fData2
  207.                   )
  208.   register FifoQueue        *fQueue;
  209.            UnsignedInteger   fKey;
  210.            integer          *fData1, *fData2;
  211. {
  212.   register FifoEltPtr Entry;
  213.   register FifoEltPtr PrevEntry;
  214.  
  215.   MXTraceMsg(5, "MMPurgeQ( %d, %d )\n", fQueue, fKey);
  216.  
  217.   /* Search queue for matching entry */
  218.  
  219.   Entry = fQueue->FifoTail;
  220.   if ( Entry != NULL )
  221.     do {
  222.         PrevEntry = Entry;
  223.         Entry = Entry->FifoNext;
  224.         if ( Entry->FifoKey == fKey )
  225.           { /* Remove from queue and deallocate */
  226.             if ( fQueue->FifoHead == fQueue->FifoTail )
  227.               { fQueue->FifoHead = NULL;
  228.                 fQueue->FifoTail = NULL;
  229.               }
  230.             else
  231.               { PrevEntry->FifoNext = Entry->FifoNext;
  232.                 if ( Entry == fQueue->FifoHead )
  233.                   fQueue->FifoHead = Entry->FifoNext;
  234.                 if ( Entry == fQueue->FifoTail )
  235.                   fQueue->FifoTail = PrevEntry;
  236.               };
  237.             if ( fData1 != NULL )
  238.               *fData1 = Entry->FifoData[0];
  239.             if ( fData2 != NULL )
  240.               *fData2 = Entry->FifoData[1];
  241.             MMDeallocateData((sizeof(FifoElt)), (char *) Entry);
  242.             return MMSS_Success;
  243.           }
  244.        }
  245.   while ( Entry != fQueue->FifoTail );
  246.   return MMSF_NoEntry;
  247. }
  248. /****************************************************************/
  249. /*          End of Message Module FIFO Queue Library            */
  250. /****************************************************************/
  251.