home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / TOOLKIT / OS2 / QUEUES / CLIQTHRD.C < prev    next >
C/C++ Source or Header  |  1994-11-17  |  6KB  |  203 lines

  1. /*==============================================================*\
  2.  *
  3.  *  cliqthrd.c - thread source
  4.  *      Copyright 1992, IBM Corp.
  5.  *
  6.  *--------------------------------------------------------------
  7.  *
  8.  *  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  9.  *  sample code created by IBM Corporation. This sample code is not
  10.  *  part of any standard or IBM product and is provided to you solely
  11.  *  for  the purpose of assisting you in the development of your
  12.  *  applications.  The code is provided "AS IS", without
  13.  *  warranty of any kind.  IBM shall not be liable for any damages
  14.  *  arising out of your use of the sample code, even if they have been
  15.  *  advised of the possibility of   such damages.
  16.  *
  17.  *--------------------------------------------------------------
  18.  *
  19.  *
  20.  *  This file contains the source for the thread initialization
  21.  *  and the source for the thread that services the queue.
  22.  *
  23. \*==============================================================*/
  24.  
  25.  
  26. #define INCL_DOSPROCESS
  27. #define INCL_DOSQUEUES
  28. #define INCL_DOSMEMMGR
  29. #define INCL_WINMESSAGEMGR
  30. #define INCL_DOSERRORS
  31. #define INCL_DOSSEMAPHORES
  32.  
  33. #include <os2.h>
  34.  
  35.  
  36. #include <stdlib.h>
  37. #include <stddef.h>
  38. #include <string.h>
  39.  
  40. #include "cliqmain.h"
  41. #include "queue.h"
  42. #include "cliqglbl.h"
  43. #include "cliqxtrn.h"
  44.  
  45.  
  46. #define  STACKSIZE      8192
  47. #define  Q_BUFFER_SIZE  4000
  48.  
  49.  
  50. PID          pidServer;
  51. HQUEUE       hqQueue;
  52. PVOID        *pvdQMemory;
  53.  
  54.  
  55. BOOL InitQThrd(HEV hevSend, PVOID *ppvdMessage, PUSHORT pusPriority, PULONG pulMsgSize, PULONG pulMsgType)
  56. {
  57.    APIRET              apiRC;
  58.    BOOL                fSuccess = TRUE;
  59.    static THREADPARAM  tpThrdParm;
  60.  
  61.    if (ERROR_QUE_NAME_NOT_EXIST == (apiRC=DosOpenQueue(&pidServer,
  62.                                                        &hqQueue,
  63.                                                        "\\queues\\" Q_NAME)))
  64.    {
  65.       MessageBox(HWND_DESKTOP,
  66.                  IDMSG_SVR_DOWN,
  67.                  apiRC,
  68.                  MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL,
  69.                  TRUE);
  70.       fSuccess = FALSE;
  71.    }
  72.  
  73.    else if (0 != apiRC)
  74.    {
  75.       MessageBox(hwndMain,
  76.                  IDMSG_Q_ERR,
  77.                  apiRC,
  78.                  MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL,
  79.                  TRUE);
  80.       fSuccess = FALSE;
  81.    }
  82.  
  83.    else
  84.    {
  85.       if (apiRC=DosAllocSharedMem((VOID **)&pvdQMemory,
  86.                             NULL,
  87.                             (ULONG)Q_BUFFER_SIZE,
  88.                             fALLOCSHR))
  89.    /* fALLOC is equivalent to PAG_READ | PAG_WRITE | PAG_COMMIT | PAG_EXECUTE | OBJ_TILE
  90.     * OBJ_TILE is necessary when allocating named shared memory
  91.     */
  92.       {
  93.         MessageBox(hwndMain,
  94.                    IDMSG_MEM_ERR,
  95.                    apiRC,
  96.                    MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL,
  97.                    TRUE);
  98.         fSuccess = FALSE;
  99.       }
  100.  
  101.       else if (apiRC=DosSubSetMem(pvdQMemory, DOSSUB_INIT, (ULONG)Q_BUFFER_SIZE))
  102.       {
  103.         MessageBox(hwndMain,
  104.                    IDMSG_SUBALLOC_MEM_ERR,
  105.                    apiRC,
  106.                    MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL,
  107.                    TRUE);
  108.         fSuccess = FALSE;
  109.       }
  110.  
  111.       /* give memory to server */
  112.       else if (apiRC=DosGiveSharedMem(pvdQMemory, pidServer, PAG_READ | PAG_WRITE))
  113.       {
  114.         MessageBox(hwndMain,
  115.                    IDMSG_GIVE_MEM_ERR,
  116.                    apiRC,
  117.                    MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL,
  118.                    TRUE);
  119.         fSuccess = FALSE;
  120.       }
  121.  
  122.       else
  123.       {
  124.          tpThrdParm.hevSend = hevSend;          /* posted when user elects to send message */
  125.          tpThrdParm.ppvdMessage = ppvdMessage;  /* message to be sent */
  126.          tpThrdParm.pusPriority = pusPriority;  /* priority of said message */
  127.          tpThrdParm.pulMsgSize = pulMsgSize;    /* size of message */
  128.          tpThrdParm.pulMsgType = pulMsgType;    /* size of message */
  129.  
  130.  
  131.          if (0 > _beginthread(PostQMessage,
  132.                               NULL,
  133.                               STACKSIZE,
  134.                               (VOID *)&tpThrdParm))
  135.          {
  136.             fSuccess = FALSE;
  137.          }
  138.       }
  139.  
  140.       if (!fSuccess)
  141.       {
  142.          DosCloseQueue(hqQueue);
  143.       }
  144.    }
  145.  
  146.    return (fSuccess);
  147. }
  148.  
  149.  
  150. VOID PostQMessage(VOID *tpThrdParm)
  151. {
  152.    VOID    *pqmsg;
  153.    APIRET  apiRC;
  154.  
  155.    while (!DosWaitEventSem(((THREADPARAM *)tpThrdParm)->hevSend, SEM_INDEFINITE_WAIT))
  156.    {
  157.       if (15 < *(((THREADPARAM *)tpThrdParm)->pusPriority))  /* exit notification */
  158.       {
  159.          break;
  160.       }
  161.  
  162.       if (TRUE == fSetUpMessage(*(THREADPARAM *)tpThrdParm, &pqmsg))
  163.       {
  164.          if (0 != (apiRC=DosWriteQueue(hqQueue,
  165.                                 *(((THREADPARAM *)tpThrdParm)->pulMsgType),
  166.                                 *(((THREADPARAM *)tpThrdParm)->pulMsgSize),
  167.                                 (VOID *)pqmsg,
  168.                                 *(((THREADPARAM *)tpThrdParm)->pusPriority))))
  169.          {
  170.             WinPostMsg(hwndMain, WM_MSG, MPFROMLONG(IDMSG_SVR_DOWN), MPFROMLONG(apiRC));
  171.          }
  172.       }
  173.    }
  174.  
  175.    DosCloseQueue(hqQueue);
  176.    DosFreeMem(pvdQMemory);
  177.    fThrdsDead = TRUE;
  178. }
  179.  
  180.  
  181. BOOL fSetUpMessage(THREADPARAM tpThrdParm, PVOID *pqmsg)
  182. {
  183.    BOOL    fSuccess = FALSE;
  184.    APIRET  apiRC;
  185.    ULONG   ulPosts;
  186.  
  187.    if (apiRC=DosSubAllocMem(pvdQMemory, pqmsg, *tpThrdParm.pulMsgSize))
  188.    {
  189.       WinPostMsg(hwndMain, WM_MSG, MPFROMLONG(IDMSG_MEM_FULL), MPFROMLONG(apiRC));
  190.    }
  191.  
  192.    else
  193.    {
  194.       if (memcpy(*pqmsg, *tpThrdParm.ppvdMessage, *tpThrdParm.pulMsgSize))
  195.       {
  196.          fSuccess = TRUE;
  197.       }
  198.    }
  199.  
  200.    DosResetEventSem(tpThrdParm.hevSend, &ulPosts);
  201.    return (fSuccess);
  202. }
  203.