home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / toolkit / mm / fssht / shdestry.c < prev    next >
C/C++ Source or Header  |  1996-11-19  |  7KB  |  140 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                    Copyright (c) IBM Corporation 1992, 1993              */
  4. /*                           All Rights Reserved                            */
  5. /*                                                                          */
  6. /* SOURCE FILE NAME: SHDESTRY.C                                             */
  7. /*                                                                          */
  8. /* DESCRIPTIVE NAME:  Stream Handler Destroy stream routine                 */
  9. /*                                                                          */
  10. /* FUNCTION: This function destroys a stream instance.                      */
  11. /*                                                                          */
  12. /* ENTRY POINTS: ShcDestroy                                                 */
  13. /*                                                                          */
  14. /*************************** END OF SPECIFICATIONS **************************/
  15. #define  INCL_NOPMAPI                  /* no PM include files required */
  16. #define  INCL_DOSSEMAPHORES
  17. #define  INCL_DOSPROCESS
  18. #define  INCL_OS2MM
  19. #include <os2.h>
  20. #include <os2me.h>
  21. #include <hhpheap.h>
  22. #include <shi.h>
  23.  
  24. /************************** START OF SPECIFICATIONS *************************/
  25. /*                                                                          */
  26. /* SUBROUTINE NAME: ShcDestroy                                              */
  27. /*                                                                          */
  28. /* DESCRIPTIVE NAME: Stream Handler Command Destroy stream routine          */
  29. /*                                                                          */
  30. /* FUNCTION: Destroys the specified stream.  Which means it takes the       */
  31. /*           stream instance block off the active chain; it signals the     */
  32. /*           worker thread to end (SIBACTFLG_KILL) and waits for it; then it*/
  33. /*           deallocates the sib.  At this point the stream is gone from    */
  34. /*           this stream handler.                                           */
  35. /*                                                                          */
  36. /* ENTRY POINT: ShcDestroy                                                  */
  37. /*   LINKAGE:   CALL NEAR (0:32)                                            */
  38. /*                                                                          */
  39. /* INPUT: Pointer to Destroy SHC paramater block (PARM_DESTROY).  It        */
  40. /*   contains:                                                              */
  41. /*     ULONG    ulFunction;           Handler command function (SHC_DESTROY)*/
  42. /*     HID      hid;                  handler ID                            */
  43. /*     HSTREAM  hstream;              handle of stream instance             */
  44. /*                                                                          */
  45. /* EXIT-NORMAL: 0 NO_ERROR                                                  */
  46. /*                                                                          */
  47. /* EXIT-ERROR:                                                              */
  48. /*   ERROR_INVALID_STREAM                                                   */
  49. /*   Errors from:                                                           */
  50. /*     DosRequestMutexSem                                                   */
  51. /*                                                                          */
  52. /* SIDE EFFECTS:                                                            */
  53. /*   All information known to this stream handler about the stream is gone. */
  54. /*   This includes the worker read/write thread and the stream instance     */
  55. /*   block.  The stream is destroyed.                                       */
  56. /*                                                                          */
  57. /* INTERNAL REFERENCES:                                                     */
  58. /*        ROUTINES: None                                                    */
  59. /*                                                                          */
  60. /* EXTERNAL REFERENCES:                                                     */
  61. /*   ROUTINES:                                                              */
  62. /*    HhpFreeMem - Free memory (sib)                                        */
  63. /*                                                                          */
  64. /*   DATA STRUCTURES:                                                       */
  65. /*    pHeap - Pointer to Stream handler heap.                               */
  66. /*    hmtxGlobalData - handle of mutex semaphore to control global data     */
  67. /*      access.  In this case the control of the sib chain, and control of  */
  68. /*      the heap control blocks.                                            */
  69. /*                                                                          */
  70. /* NOTES: Filter stream handlers only have 1 worker thread for 2 streams.   */
  71. /*   This is indicated by a NULL (0) sib.ThreadID for the second stream.    */
  72. /*   So when destroy is called for this second stream (sib.threadid = 0)    */
  73. /*   we skip the code to end the thread since there one does not exist.     */
  74. /*                                                                          */
  75. /*************************** END OF SPECIFICATIONS **************************/
  76.  
  77. RC ShcDestroy(pdyparm)
  78. PPARM_DESTROY pdyparm;
  79.  
  80. { /* Start of ShcDestroy */
  81.  
  82. RC rc = NO_ERROR;                       /* local return code */
  83. PSIB psib;                              /* Stream instance block */
  84. PSIB psibPrev = NULL;                   /* previous Stream instance block in */
  85.                                         /* chain */
  86.  
  87.   /*
  88.    * Find our Stream Instance Block and take it off the list
  89.    */
  90.   ENTERCRITX(rc);
  91.  
  92.   psib = pSIB_list_head;
  93.   while ((psib) &&
  94.          !((psib->hStream == pdyparm->hstream) && (psib->HandlerID == pdyparm->hid)))
  95.     {
  96.       psibPrev = psib;
  97.       psib = psib->pnxtSIB;
  98.     }
  99.  
  100.   /* If the sib was found take it off the chain.  If it was not found the */
  101.   /* stream handle and id is invalid. */
  102.  
  103.   if (psib)
  104.     { /* Take the sib off the chain */
  105.  
  106.       /* If previous sib in chain then take this sib out.  If no previous */
  107.       /*  sib then this is the first sib in the chain so update the list */
  108.       /*  head. */
  109.  
  110.       if (psibPrev)
  111.         {
  112.           psibPrev->pnxtSIB = psib->pnxtSIB;
  113.         }
  114.       else
  115.         {
  116.           pSIB_list_head = psib->pnxtSIB;
  117.         }
  118.     } /* Take the sib off the chain */
  119.   else
  120.     {
  121.       rc = ERROR_INVALID_STREAM;
  122.     }
  123.  
  124.   EXITCRIT;
  125.  
  126.   if (!rc)
  127.     { /* We have SIB */
  128.  
  129.       /* First kill the read write thread */
  130.  
  131.       /* Check sib threadid to make sure there is a thread for this stream */
  132.       /* before resume and waiting for it. */
  133.  
  134.       DestroySIB(psib);
  135.     } /* We have SIB */
  136.  
  137.   return(rc);
  138.  
  139. } /* End of ShcDestroy */
  140.