home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / mmpm2tk / mmpmtlk2 / admct / admcinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-06  |  10.6 KB  |  328 lines

  1. /********************* START OF SPECIFICATIONS *********************
  2. *
  3. * SUBROUTINE NAME: admcinit.c
  4. *
  5. * DESCRIPTIVE NAME: Audio MCD DLL Intialization and termination
  6. *                  functions.
  7. *
  8. * FUNCTION: Create Global heap, carry out per process intialization.
  9. *
  10. * NOTES:
  11. *
  12. * EXIT-NORMAL: Return Code 0.
  13. *
  14. * EXIT_ERROR:  Error Code.
  15. *
  16. *
  17. * Global Vars Referenced:
  18. *              hmtxProcSem, heap, UserCount,
  19. *              hEventInitCompleted,
  20. *
  21. * INTERNAL REFERENCES:   ADMCEXIT () -- DLL Termination Routine.
  22. *                        _DLL_InitTerm -- DLL Intialization Routine
  23. *
  24. * EXTERNAL REFERENCES:   DosResetEventSem ()        - OS/2 API
  25. *                        DosPostEventSem  ()        - OS/2 API
  26. *                        DosCreateMutexSem()        - OS/2 API
  27. *                        HhpCreateHeap    ()        - MME  API
  28. *                        HhpDestroyHeap   ()        - MME  API
  29. *                        HhpGetPID()      ()        - MME  API
  30. *
  31. *********************** END OF SPECIFICATIONS **********************/
  32.  
  33. #define INCL_BASE
  34. #define INCL_DOSSEMAPHORES
  35. #define INCL_DOSPROCESS
  36. #define INCL_ERRORS
  37.  
  38. #define MCIERR_SUCCESS          0
  39. #define INDEFNITE_PERIOD       -1
  40. #define HEAP_SIZE               64768
  41. #define INTIALIZE               0
  42. #define TERMINATE               1
  43. #define FAILURE                 0L
  44. #define SUCCESS                 1L
  45.  
  46. #include <os2.h>                        // OS/2 System Include
  47. #include <hhpheap.h>                    // Heap Manager Definitions
  48.  
  49. /********************* START OF SPECIFICATIONS *******************************
  50. *
  51. * SUBROUTINE NAME: ADMEXIT
  52. *
  53. * DESCRIPTIVE NAME: AudioMCD DLL Temination Routine
  54. *
  55. * FUNCTION: Cleanup and deallocate resources used.
  56. *
  57. *
  58. * ENTRY POINTS:
  59. *     LINKAGE:   CALL FAR
  60. *
  61. * INPUT:
  62. *
  63. * EXIT-NORMAL: Return Code 1 to the System Loader.
  64. *
  65. * EXIT_ERROR:  Failure To Load This Module.
  66. *
  67. * EFFECTS:
  68. *
  69. * INTERNAL REFERENCES: heap management Routines.
  70. *
  71. *               Global Variables referenced:
  72. *                      heap.
  73. *                      hmtxProcSem.
  74. *                      UserCount.
  75. *
  76. * EXTERNAL REFERENCES: DosCreateMutexSem  ()    -  OS/2 API
  77. *                      DosOpenMutexSem    ()    -  OS/2 API
  78. *                      DosReleaseMutexSem ()    -  OS/2 API
  79. *                      DosCloseMutexSem   ()    -  OS/2 API
  80. *                      DosExitList        ()    -  OS/2 API -- (DCR ed )
  81. *
  82. *********************** END OF SPECIFICATIONS ********************************/
  83.  
  84. USHORT ADMCEXIT ()                    /* AudioMCD Exit Routine    */
  85. {
  86.  
  87.   extern int          UserCount;   // Number of Dynamic links
  88.   extern HHUGEHEAP    heap;        // Global Heap
  89.   extern HMTX         hmtxProcSem; // Global Mutex semaphore
  90.   ULONG               ulrc;        // Return Code
  91.  
  92.   ulrc = MCIERR_SUCCESS;
  93.   UserCount--;
  94.   /********************************************
  95.   * Open the global mutex semaphore
  96.   *********************************************/
  97.   ulrc = DosOpenMutexSem ((ULONG)NULL, (PHMTX)&hmtxProcSem);
  98.  
  99.   if (ulrc)
  100.       return (FAILURE);              /* Failed Sem Open */
  101.  
  102.   /*****************************************
  103.   * Request the mutex semaphore.
  104.   ******************************************/
  105.   if ((ulrc = DosRequestMutexSem (hmtxProcSem,
  106.                                  (ULONG)INDEFNITE_PERIOD)) != 0) {
  107.       DosCloseMutexSem (hmtxProcSem);
  108.       return (FAILURE);              /* Indicate failure to loader */
  109.   }
  110.   /*********************************************
  111.   * The last process destroys the heap
  112.   **********************************************/
  113.   if (UserCount == 0) {
  114.       HhpDestroyHeap (heap);    /* Destroy the heap */
  115.   }
  116.  
  117.   /*********************************************
  118.   * If not the last release access to heap
  119.   **********************************************/
  120.   if (UserCount > 0) {
  121.       HhpReleaseHeap (heap, HhpGetPID());  /* Release access */
  122.   }
  123.  
  124.   DosReleaseMutexSem (hmtxProcSem);
  125.   DosCloseMutexSem (hmtxProcSem);
  126.  
  127.   /* De register Termination Routine */
  128.  
  129.   //DosExitList (EXLST_EXIT, (PFNEXITLIST)ADMCEXIT);
  130.  
  131.   return (SUCCESS);
  132. }
  133.  
  134.  
  135.  
  136.  
  137. /********************* START OF SPECIFICATIONS *******************************
  138. *
  139. * SUBROUTINE NAME: ADMCINIT.C
  140. *
  141. * DESCRIPTIVE NAME: Audio MCD DLL Initialization.
  142. *
  143. * FUNCTION: Intialize the DLL and provide mem access to multiple processes.
  144. *
  145. * NOTES: Access to The Global heap is provided using the heap manager
  146. *        Routines. An Exit List is Registered at intialization Time.
  147. *        The Routine is deregistered when the last Process that uses
  148. *        this DLL Terminates.
  149. *
  150. * NOTES:  This routine utilizes a feature of the IBM C Set/2 Compiler
  151. *         that frees programmers from having to write assembler stubs to
  152. *         initialize their re-entrant DLLs.
  153. *         C Set/2 provides an assembler initialization stub with the
  154. *         compiler, and that stub sets the entry point of the DLL to
  155. *         _DLL_InitTerm.  As long as the DLL entry point has this name,
  156. *         the DLL can take advantage of the C SET/2 assembler stub instead
  157. *         of writing one of it's own.
  158. *
  159. * ENTRY POINT: _DLL_InitTerm
  160. *
  161. * EXIT-NORMAL: Return Code 1 to the System Loader.
  162. *
  163. * EXIT_ERROR:  Failure To Load This Module.
  164. *
  165. * EFFECTS:
  166. *
  167. * INTERNAL REFERENCES: heap management Routines,
  168. *
  169. *               Global Variables referenced:
  170. *                      heap.
  171. *                      hmtxProcSem.
  172. *                      UserCount.
  173. *
  174. * EXTERNAL REFERENCES: DosCreateMutexSem  ()    -  OS/2 API
  175. *                      DosOpenMutexSem    ()    -  OS/2 API
  176. *                      DosReleaseMutexSem ()    -  OS/2 API
  177. *                      DosCloseMutexSem   ()    -  OS/2 API
  178. *                      DosExitList        ()    -  OS/2 API
  179. *
  180. *********************** END OF SPECIFICATIONS ********************************/
  181.  
  182. #pragma linkage (_DLL_InitTerm, system)
  183.  
  184. unsigned long _DLL_InitTerm (unsigned long hModhandle, unsigned long Flag)
  185. {
  186.  
  187.   extern int          UserCount;
  188.   extern HHUGEHEAP    heap;
  189.   extern HMTX         hmtxProcSem;
  190.   extern HEV          hEventInitCompleted;
  191.   extern ULONG        lPost;
  192.  
  193.   ULONG               ulrc;
  194.   USHORT              HeapAllocated;
  195.   USHORT              RCExit;
  196.   INT                 rc;
  197.   ulrc = hModhandle;      // Subdue the Warning for now
  198.   ulrc = MCIERR_SUCCESS;
  199.   HeapAllocated = FALSE;
  200.  
  201.   switch (Flag)
  202.   {
  203.   case INTIALIZE:
  204.  
  205.        {
  206.        rc = _CRT_init();
  207.        if (rc) {
  208.           return (0L);
  209.        }
  210.        /*****************************************************************
  211.        * Create The InitCompleted Event Sem. This semaphore is used
  212.        * to block all entries in to the DLL prior to the completion of
  213.        * intialization routine. On completion of the intialization
  214.        * InitCompleted event is posted which frees up any blocked
  215.        * thread at the entry point.
  216.        *******************************************************************/
  217.  
  218.        if (hEventInitCompleted == (ULONG)NULL) {
  219.  
  220.            ulrc = DosCreateEventSem ((ULONG)NULL,
  221.                                      (PHEV)&(hEventInitCompleted),
  222.                                      DC_SEM_SHARED,
  223.                                      FALSE);
  224.            if (ulrc)
  225.                return (FAILURE);
  226.        }
  227.        /*****************************************
  228.        * Reset Intialization Complete Semaphore
  229.        ******************************************/
  230.        DosResetEventSem (hEventInitCompleted, &lPost);
  231.  
  232.  
  233.        /**************************************
  234.        * Create the Global Mutex Sem
  235.        ***************************************/
  236.        if ((hmtxProcSem == 0) && (UserCount == 0)) {
  237.             ulrc = DosCreateMutexSem ((ULONG)NULL,
  238.                                 &hmtxProcSem,
  239.                                 DC_SEM_SHARED,
  240.                                 (ULONG)FALSE);
  241.             if (ulrc)
  242.                     return (FAILURE);  /* Failed Sem Create */
  243.        }
  244.        /***************************************
  245.        * Open The Mutex Semaphore
  246.        ****************************************/
  247.        ulrc = DosOpenMutexSem ((ULONG)NULL, (PHMTX)&hmtxProcSem);
  248.  
  249.        if (ulrc)
  250.             return (FAILURE);          /* Failed Sem Open */
  251.  
  252.        /****************************************
  253.        * Acquire The Mutex Semaphore
  254.        *****************************************/
  255.        if ((ulrc = DosRequestMutexSem (hmtxProcSem,
  256.                               (ULONG)INDEFNITE_PERIOD)) != 0) {
  257.  
  258.            DosCloseMutexSem (hmtxProcSem);
  259.            return (FAILURE);
  260.        }
  261.  
  262.        if (UserCount == 0) {
  263.  
  264.            heap = HhpCreateHeap ((ULONG)HEAP_SIZE, HH_SHARED);
  265.  
  266.            if (heap == (ULONG)NULL) {
  267.                DosReleaseMutexSem (hmtxProcSem);
  268.                DosCloseMutexSem (hmtxProcSem);
  269.                return (FAILURE);
  270.            }
  271.            HeapAllocated = TRUE;
  272.        }
  273.        /*******************************************
  274.        * Register ExitList Routine.
  275.        ********************************************/
  276.        // ulrc = DosExitList (0x6101, (PFNEXITLIST)ADMCEXIT);
  277.  
  278.        /*************************************************
  279.        * If the user count is greater than one
  280.        * then we just provide access to the global heap.
  281.        **************************************************/
  282.        if (UserCount != 0) {
  283.           /**********************************
  284.           * Provide Shared Access to heap
  285.           ***********************************/
  286.           ulrc = HhpAccessHeap (heap, HhpGetPID());
  287.           if (ulrc)
  288.                return (FAILURE);
  289.        }
  290.  
  291.        /******************************************
  292.         * Release The Mutex Semaphore
  293.        *******************************************/
  294.        if ((ulrc = DosReleaseMutexSem (hmtxProcSem)) != 0L)
  295.        {
  296.            if (UserCount != 0)
  297.                 DosCloseMutexSem (hmtxProcSem);
  298.            return (FAILURE);
  299.        }
  300.  
  301.        UserCount++;                         /* Increment Usage */
  302.        //DosCloseMutexSem (hmtxProcSem);
  303.  
  304.        /**************************************
  305.        * Post Intialization Complete Semaphore
  306.        ****************************************/
  307.        DosPostEventSem (hEventInitCompleted);
  308.  
  309.        /************************************
  310.        * Init Complete, Indicate success
  311.        * to the loader
  312.        *************************************/
  313.        return (SUCCESS);
  314.        }
  315.        break;
  316.  
  317.   case TERMINATE:
  318.        {
  319.           _CRT_term();
  320.           RCExit = ADMCEXIT ();
  321.           return (RCExit);
  322.        }
  323.        break;
  324.  
  325.   } /* Of switch statement */
  326.  
  327. } /* of Init Term Function */
  328.