home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / Q.C < prev    next >
Text File  |  1994-12-09  |  10KB  |  211 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   q.c                                                                     */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Queue function for multiple process support.                             */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   03/05/94 Created.                                                       */
  12. /*                                                                           */
  13. /*****************************************************************************/
  14.  
  15. #include "all.h"
  16.  
  17. #define  QUE_STACK_SIZE 0x4000          /* read queue thread stack size.     */
  18.  
  19. /*****************************************************************************/
  20. /* StartQue()                                                                */
  21. /*                                                                           */
  22. /* Description:                                                              */
  23. /*                                                                           */
  24. /*   Create a queue.                                                         */
  25. /*                                                                           */
  26. /* Parameters:                                                               */
  27. /*                                                                           */
  28. /*   pQueName     -> queue name.                                             */
  29. /*   pQueReader   -> function that will read and process queue messages.     */
  30. /*                                                                           */
  31. /* Return:                                                                   */
  32. /*                                                                           */
  33. /*   rc                                                                      */
  34. /*                                                                           */
  35. /* Assumptions:                                                              */
  36. /*                                                                           */
  37. /*  All queues are FIFO.                                                     */
  38. /*                                                                           */
  39. /*****************************************************************************/
  40. APIRET StartQue( char *pQueName, void (* _Optlink pQueReader)(void *) )
  41. {
  42.  APIRET   rc;
  43.  LHANDLE  QueHandle = 0;
  44.  
  45.  /****************************************************************************/
  46.  /* Create a termination/session notification queue.                         */
  47.  /****************************************************************************/
  48.  rc=DosCreateQueue((PHQUEUE)&QueHandle, QUE_FIFO, pQueName);
  49.  if( rc != 0)
  50.   return(rc);
  51.  
  52.  /****************************************************************************/
  53.  /* Start a thread to read the queue.                                        */
  54.  /****************************************************************************/
  55.  _beginthread(pQueReader , NULL, QUE_STACK_SIZE, (void*)QueHandle);
  56.  return(0);
  57. }
  58.  
  59. /*****************************************************************************/
  60. /* SendMsgToQue()                                                            */
  61. /*                                                                           */
  62. /* Description:                                                              */
  63. /*                                                                           */
  64. /*   Send a message to a queue.                                              */
  65. /*                                                                           */
  66. /* Parameters:                                                               */
  67. /*                                                                           */
  68. /*   pQueName  ->name of the queue that we're sending the message to.        */
  69. /*   Msg         message going to the queue.                                 */
  70. /*   pBuf      ->queue element buffer.                                       */
  71. /*   Len         queue element buffer length.                                */
  72. /*                                                                           */
  73. /* Return:                                                                   */
  74. /*                                                                           */
  75. /*   rc      0==>success                                                     */
  76. /*           1==>failure                                                     */
  77. /*                                                                           */
  78. /* Assumption:                                                               */
  79. /*                                                                           */
  80. /*****************************************************************************/
  81. void SendMsgToQue( char *pQueName, ULONG Msg, void *pBuf, ULONG Len)
  82. {
  83.  ULONG     Pid;
  84.  HQUEUE    QueHandle;
  85.  void     *pQueElement = NULL;
  86.  ULONG     QueElementLen = Len;
  87.  ULONG     flags;
  88.  TIB      *pTib;
  89.  PIB      *pPib;
  90.  
  91.  /****************************************************************************/
  92.  /* - Open the a queue. The returned pid is the process id of the queue      */
  93.  /*   owner. This call also returns a handle to the queue.                   */
  94.  /****************************************************************************/
  95.  DosOpenQueue(&Pid,&QueHandle,pQueName);
  96.  
  97.  /****************************************************************************/
  98.  /* - Allocate a shared memory for the queue element if there is one.        */
  99.  /* - Copy the message into the shared segment.                              */
  100.  /* - Give the owner of the queue access to the queue element.               */
  101.  /****************************************************************************/
  102.  if( pBuf != NULL )
  103.  {
  104.   flags = PAG_COMMIT | OBJ_GIVEABLE | PAG_READ | PAG_WRITE;
  105.   DosAllocSharedMem( &pQueElement, NULL, QueElementLen, flags );
  106.  
  107.   memcpy(pQueElement,pBuf,QueElementLen);
  108.  
  109.   flags = PAG_READ;
  110.   DosGiveSharedMem( pQueElement, Pid, flags );
  111.  }
  112.  /****************************************************************************/
  113.  /* - Send the message to the queue.                                         */
  114.  /****************************************************************************/
  115.  DosWriteQueue( QueHandle, Msg, QueElementLen, pQueElement,0);
  116.  
  117.  /****************************************************************************/
  118.  /* - Get the pid of "this" process sending the message.                     */
  119.  /* - If the owner of the pid and pid sending the message are the same,      */
  120.  /*   then don't DosFreeMem() the queue element. It will be freed by the     */
  121.  /*   owner.                                                                 */
  122.  /****************************************************************************/
  123.  if(pQueElement != NULL )
  124.  {
  125.   DosGetInfoBlocks(&pTib,&pPib);
  126.   if( pPib->pib_ulpid != Pid )
  127.   {
  128.    DosFreeMem(pQueElement);
  129.   }
  130.  }
  131. }
  132.  
  133.  
  134. #if 0
  135. /*****************************************************************************/
  136. /* - these functions are not currently used but the code is retained just    */
  137. /*   in case.                                                                */
  138. /*****************************************************************************/
  139. void *PeekQueMsg( LHANDLE QueHandle, ULONG QueMsg )
  140. {
  141.  REQUESTDATA  Request;
  142.  ULONG        DataLen;
  143.  BYTE         ElePri;
  144.  ULONG        EleCode;
  145.  ULONG        count;
  146.  
  147.  DBG_QUE_ELEMENT *pDataAddr=NULL;
  148.  
  149.  count = 0;
  150.  DosQueryQueue( QueHandle, &count);
  151.  for( EleCode=0 ;count>0;count-- )
  152.  {
  153.   printf("\nQue count=%d",count);fflush(0);
  154.   DosPeekQueue(QueHandle,
  155.                &Request,
  156.                &DataLen,
  157.                (PPVOID)&pDataAddr,
  158.                &EleCode,
  159.                DCWW_WAIT,
  160.                &ElePri,
  161.                0L);
  162.  
  163.   if( Request.ulData == QueMsg )
  164.   {
  165.    return( ( void*)pDataAddr );
  166.   }
  167.  }
  168.  return(NULL);
  169. }
  170.  
  171. void FlushMsgFromQue( LHANDLE QueHandle, ULONG QueMsg )
  172. {
  173.  REQUESTDATA  Request;
  174.  ULONG        DataLen;
  175.  BYTE         ElePri;
  176.  ULONG        EleCode;
  177.  ULONG        count;
  178.  
  179.  DBG_QUE_ELEMENT *pDataAddr=NULL;
  180.  APIRET rc;
  181.  
  182.  count = 0;
  183.  DosQueryQueue( QueHandle, &count);
  184.  
  185.  for( EleCode=0 ;count>0;count-- )
  186.  {
  187.   rc = DosPeekQueue(QueHandle,
  188.                     &Request,
  189.                     &DataLen,
  190.                     (PPVOID)&pDataAddr,
  191.                     &EleCode,
  192.                     DCWW_WAIT,
  193.                     &ElePri,
  194.                     0L);
  195.  
  196.   if( Request.ulData == QueMsg )
  197.   {
  198.    rc = DosReadQueue(QueHandle,
  199.                      &Request,
  200.                      &DataLen,
  201.                      (PPVOID)&pDataAddr,
  202.                      EleCode,
  203.                      DCWW_WAIT,
  204.                      &ElePri,
  205.                      0L);
  206.    EleCode = 0;
  207.   }
  208.  }
  209. }
  210. #endif
  211.