home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v6.zip / MMPM2TK / TK / FSSHT / FSSHCREA.C < prev    next >
C/C++ Source or Header  |  1993-02-25  |  11KB  |  257 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                    Copyright (c) IBM Corporation 1992, 1993              */
  4. /*                           All Rights Reserved                            */
  5. /*                                                                          */
  6. /* SOURCE FILE NAME: FSSHCREA.C                                             */
  7. /*                                                                          */
  8. /* DESCRIPTIVE NAME:  Stream Handler Create stream routine for file system  */
  9. /*                    stream handler                                        */
  10. /*                                                                          */
  11. /* FUNCTION: This function is creates a stream instance.                    */
  12. /*                                                                          */
  13. /* ENTRY POINTS: ShcCreate                                                  */
  14. /*                                                                          */
  15. /*************************** END OF SPECIFICATIONS **************************/
  16. #define  INCL_NOPMAPI                  /* no PM include files required */
  17. #define  INCL_DOSSEMAPHORES
  18. #define  INCL_DOSPROCESS
  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: ShcCreate                                               */
  27. /*                                                                          */
  28. /* DESCRIPTIVE NAME: Stream Handler Command Create stream routine           */
  29. /*                                                                          */
  30. /* FUNCTION: This routine creates a stream instance of the datatype and     */
  31. /*   subtype passed.                                                        */
  32. /*                                                                          */
  33. /* NOTES:                                                                   */
  34. /*                                                                          */
  35. /* ENTRY POINT: ShcCreate                                                   */
  36. /*   LINKAGE:   CALL NEAR (0:32)                                            */
  37. /*                                                                          */
  38. /* INPUT: Pointer to shc create parameter block (PARM_CREATE) containing:   */
  39. /*   ULONG   ulFunction  Handler command function SHC_CREATE                */
  40. /*   HID     hid         handler ID                                         */
  41. /*   HSTREAM hstream     handle of stream instance                          */
  42. /*   ULONG   ulFlag      Seek flag with following possible Flags:           */
  43. /*                         SPI_SEEK_ABSOLUTE                                */
  44. /*                                                                          */
  45. /* EXIT-NORMAL: NO_ERROR (0)                                                */
  46. /*                                                                          */
  47. /* EXIT-ERROR:                                                              */
  48. /*   ERROR_INVALID_SPCBKEY                                                  */
  49. /*   ERROR_ALLOC_RESOURCES                                                  */
  50. /*   Errors from the external routines:                                     */
  51. /*     DosRequestMutexSem                                                   */
  52. /*     DosCreateThread                                                      */
  53. /*                                                                          */
  54. /* SIDE EFFECTS:                                                            */
  55. /*   Stream Instance Block allocated and added to active list.              */
  56. /*   Thread created suspended to perform I/O (read/write).                  */
  57. /*                                                                          */
  58. /* EXTERNAL REFERENCES:                                                     */
  59. /*   ROUTINES:                                                              */
  60. /*     ShFindEspcb                                                          */
  61. /*     HhpAllocMem                                                          */
  62. /*     DosRequestMutexSem                                                   */
  63. /*     DosCreateThread                                                      */
  64. /*     DosReleaseMutexSem                                                   */
  65. /*     DosSetPriority                                                       */
  66. /*     DosResumeThread                                                      */
  67. /*     DosWaitThread                                                        */
  68. /*     HhpFreeMem                                                           */
  69. /*     HhpGetPID                                                            */
  70. /*                                                                          */
  71. /*   DATA STRUCTURES:                                                       */
  72. /*     hmtxGlobalData                                                       */
  73. /*     pESPCB_ListHead                                                      */
  74. /*     hHeap                                                                */
  75. /*     TgtHandlerID                                                         */
  76. /*************************** END OF SPECIFICATIONS **************************/
  77.  
  78. RC ShcCreate(pcrparm)
  79. PPARM_CREATE pcrparm;
  80.  
  81. { /* Start of ShcCreate */
  82.  
  83. RC      rc = NO_ERROR;                   /* local return code */
  84. PSIB    psib = NULL;                     /* Stream instance block */
  85. PESPCB  pEspcb;                          /* -> to espcb for spcb key passed */
  86. int     notfound = TRUE;                 /* loop control */
  87. BOOL    bIOThreadCreated = FALSE;        /* local var for error processing */
  88. BOOL    bHevStopCreated = FALSE;
  89. BOOL    bHevCmdSyncCreated = FALSE;
  90.  
  91.   /*
  92.    * locate the correct spcb
  93.    * allocate sib
  94.    * create a thread to read or write
  95.    * set thread priority
  96.    * call smhinit
  97.    * add sib to list
  98.    */
  99.   /*
  100.    * Locate the spcb; if not found use the generic spcb
  101.    * Note: there is not checking for if this espcb can support
  102.    * being the requested source or target.
  103.    */
  104.  
  105.   if (!(rc = DosRequestMutexSem(hmtxGlobalData, SEM_INDEFINITE_WAIT)))
  106.     { /* we have the mutex sem */
  107.  
  108.       pEspcb = ShFindEspcb(pcrparm->spcbkey);
  109.  
  110.       /* If espcb not found then get the last generic spcb installed and use it. */
  111.  
  112.       if (!pEspcb)
  113.         { /* Get Generic spcb */
  114.  
  115.           rc = ERROR_INVALID_SPCBKEY;
  116.           pEspcb = pESPCB_ListHead;
  117.           while (pEspcb && notfound)
  118.             { /* Loop thru espcbs */
  119.               if (pEspcb->spcb.spcbkey.ulDataType == DATATYPE_GENERIC)
  120.                 {
  121.                   notfound = FALSE;
  122.                   rc = NO_ERROR;
  123.                 }
  124.               else
  125.                 {
  126.                   pEspcb = pEspcb->pnxtESPCB;
  127.                 }
  128.             } /* Loop thru espcbs */
  129.         } /* Get Generic spcb */
  130.  
  131.       /* If we have an espcb alloc a stream instance block and copy the espcb */
  132.  
  133.       if (!rc)
  134.         {
  135.           /*
  136.            * Allocate a SIB and copy ESPCB
  137.            */
  138.           psib = (PSIB)HhpAllocMem(hHeap, sizeof(SIB));
  139.           if (psib)
  140.             { /* SIB allocated */
  141.  
  142.               psib->espcb = *pEspcb;              /* copy entire structure */
  143.               /* copy over data subtype.  SSM needs this particular field even if */
  144.               /*  it is generic.  In most cases it is already the same. */
  145.               psib->espcb.spcb.spcbkey.ulDataSubType = pcrparm->spcbkey.ulDataSubType;
  146.  
  147.             } /* SIB allocated */
  148.           else
  149.             { /* Error allocating sib */
  150.  
  151.               rc = ERROR_ALLOC_RESOURCES;
  152.  
  153.             } /* Error allocating sib */
  154.         }
  155.       DosReleaseMutexSem(hmtxGlobalData);
  156.     } /* we have the mutex sem */
  157.  
  158.   if (!rc)
  159.     { /* we have sib and espcb */
  160.  
  161.       /* Now create the semaphores for starting and stopping the IO thread */
  162.       /* and for command synchronization. */
  163.  
  164.       rc = DosCreateEventSem(NULL, &psib->hevStop, 0L, FALSE);
  165.       if (!rc)
  166.         {
  167.           bHevStopCreated = TRUE;
  168.           rc = DosCreateEventSem(NULL, &psib->hevCmdSync, 0L, FALSE);
  169.           if (!rc)
  170.             bHevCmdSyncCreated = TRUE;
  171.         }
  172.  
  173.       if (!rc)
  174.         {
  175.           psib->HandlerID = pcrparm->hid;
  176.           psib->hStream = pcrparm->hstream;
  177.           psib->ulActionFlags = 0;
  178.           psib->ProcessID = HhpGetPID();
  179.           pcrparm->pspcb = &psib->espcb.spcb;           /* return spcb address */
  180.           psib->ulStatus = SIB_INITIALIZED;
  181.           /*
  182.            * Create the read/write thread and set it to Time Critical priority
  183.            */
  184.           if (pcrparm->hid == TgtHandlerID)
  185.             {
  186.               rc = DosCreateThread(&psib->ThreadID,
  187.                                    ShWriteRtn,
  188.                                    (ULONG)psib,
  189.                                    0L,
  190.                                    STACKSIZE);
  191.             }
  192.           else
  193.             {
  194.               rc = DosCreateThread(&psib->ThreadID,
  195.                                    ShReadRtn,
  196.                                    (ULONG)psib,
  197.                                    0L,
  198.                                    STACKSIZE);
  199.             }
  200.         }
  201.       if (!rc)
  202.         { /* IO Thread created */
  203.  
  204.           bIOThreadCreated = TRUE;
  205.           DosSetPriority(PRTYS_THREAD,
  206.                          PRTYC_TIMECRITICAL,
  207.                          01L,
  208.                          psib->ThreadID);
  209.           /*
  210.            * Now connect SIB to list, (the list is under semaphore
  211.            * control).
  212.            */
  213.           rc = DosRequestMutexSem(hmtxGlobalData, SEM_INDEFINITE_WAIT);
  214.           if (!rc)
  215.             { /* Update SIB list */
  216.  
  217.               psib->pnxtSIB = pSIB_list_head;
  218.               pSIB_list_head = psib;
  219.               DosReleaseMutexSem(hmtxGlobalData);
  220.  
  221.             } /* Update SIB list */
  222.         } /* IO Thread created */
  223.  
  224.       if (rc)
  225.         { /* Error - Clean up */
  226.  
  227.           if (bIOThreadCreated)
  228.             { /* kill the read write thread */
  229.  
  230.               psib->ulActionFlags |= SIBACTFLG_KILL;
  231.               DosPostEventSem(psib->hevStop);
  232.               DosWaitThread(&psib->ThreadID, DCWW_WAIT);
  233.  
  234.             } /* kill the read write thread */
  235.  
  236.           if (bHevStopCreated)
  237.             {
  238.               DosCloseEventSem(psib->hevStop);
  239.             }
  240.  
  241.           if (bHevCmdSyncCreated)
  242.             {
  243.               DosCloseEventSem(psib->hevCmdSync);
  244.             }
  245.  
  246.           DosRequestMutexSem(hmtxGlobalData, SEM_INDEFINITE_WAIT);
  247.           HhpFreeMem(hHeap, psib);
  248.           DosReleaseMutexSem(hmtxGlobalData);
  249.  
  250.         } /* Error - Clean up */
  251.  
  252.     } /* we have sib and espcb */
  253.  
  254.   return(rc);
  255.  
  256. } /* End of ShcCreate */
  257.