home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / os2thred.zip / MSGQ.CPP next >
C/C++ Source or Header  |  1994-06-05  |  2KB  |  102 lines

  1. // Listing 8 -- MsgQ.cpp
  2.  
  3. //------------------------- Includes ------------------------------
  4.  
  5. #define INCL_WIN
  6. #define INCL_DOS
  7.  
  8. #include <os2.h>
  9. #include "MsgQ.h"
  10.  
  11. //-------------------------- defines ------------------------------
  12.  
  13. const USHORT MQ_INCREMENT   = 5;
  14.  
  15. //--------------------------- code --------------------------------
  16.  
  17. MsgQueue::MsgQueue (USHORT usQSz) : usQSize(usQSz),
  18.                                     Front(0), Rear(0)
  19. {
  20.     msgs = new QMSG[usQSize];
  21.     DosCreateMutexSem (NULL, &hmtx, DC_SEM_SHARED, FALSE);
  22.     DosCreateEventSem (NULL, &hevItmRdy, DC_SEM_SHARED, FALSE);
  23. }
  24.  
  25. MsgQueue::~MsgQueue()
  26. {
  27.     DosCloseEventSem (hevItmRdy);
  28.     DosCloseMutexSem (hmtx);
  29.     delete msgs;
  30. }
  31.  
  32. VOID MsgQueue::PostMsg (ULONG hobj, ULONG msg, MPARAM mp1, MPARAM mp2)
  33. {
  34.     DosRequestMutexSem (hmtx, SEM_INDEFINITE_WAIT);
  35.     QPut(hobj, msg, mp1, mp2);
  36.     DosReleaseMutexSem (hmtx);
  37.     DosPostEventSem (hevItmRdy);    // wake up whoever is waiting for msgs
  38. }
  39.  
  40.  
  41. BOOL MsgQueue::WaitMsg(QMSG & qmsg)
  42. {
  43.     ULONG ulNPosts;
  44.     
  45.     DosWaitEventSem (hevItmRdy, SEM_INDEFINITE_WAIT);
  46.     DosRequestMutexSem (hmtx, SEM_INDEFINITE_WAIT);
  47.     QGet (qmsg);
  48.     if (QEmpty())
  49.         DosResetEventSem (hevItmRdy, &ulNPosts);
  50.     DosReleaseMutexSem (hmtx);
  51.     return (qmsg.msg);
  52. }
  53.  
  54. BOOL MsgQueue::QEmpty()
  55. {
  56.     return (Front == Rear);
  57. }
  58.  
  59. VOID MsgQueue::QPut(ULONG hobj, ULONG msg, MPARAM mp1,MPARAM mp2)
  60. {
  61.     USHORT    usNxtR, usNQSize, idxF, i;
  62.     QMSG    *p;
  63.     
  64.     msgs[Rear].hwnd = (HWND)hobj;
  65.     msgs[Rear].msg = msg;
  66.     msgs[Rear].mp1 = mp1;
  67.     msgs[Rear].mp2 = mp2;
  68.  
  69.     // If queue has filled up, then reallocate a larger queue
  70.     // and transfer the contents to the new queue
  71.     usNxtR = (Rear+1) % usQSize;
  72.     if (usNxtR == Front) {
  73.         usNQSize = usQSize + MQ_INCREMENT;
  74.         p = new QMSG[usNQSize];
  75.         idxF = Front;
  76.         for (i=0; i < usQSize; i++) {
  77.             p[i] = msgs[idxF++];
  78.             if (idxF == usQSize)
  79.                 idxF = 0;
  80.         }
  81.         Front = 0;
  82.         Rear = usQSize;
  83.         delete msgs;
  84.         usQSize = usNQSize;
  85.         msgs = p;
  86.     } else 
  87.         Rear = usNxtR;
  88. }
  89.  
  90.  
  91. VOID MsgQueue::QGet (QMSG & qmsg)
  92. {
  93.     qmsg.hwnd = msgs[Front].hwnd;
  94.     qmsg.msg = msgs[Front].msg;
  95.     qmsg.mp1 = msgs[Front].mp1;
  96.     qmsg.mp2 = msgs[Front].mp2;
  97.     Front = (++Front % usQSize);
  98. }
  99.  
  100. //------------------------ end of file ----------------------------
  101.  
  102.