home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / runnable / mmos2 / mmtoolkt / samples / cdmct / cdmcinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-06  |  9.5 KB  |  253 lines

  1. /*static char *SCCSID = "@(#)cdmcinit.c    13.4 92/04/30";*/
  2. /******************************STATE OF SPECIFICATIONS**********************/
  3. /*                                                                         */
  4. /*      SOURCE FILE NAME : CDMCINIT.C                                      */
  5. /*                                                                         */
  6. /*      DESCRIPTIVE NAME : CDMC DATA INITIALIZATION  DLL                   */
  7. /*                                                                         */
  8. /*      COPYRIGHT :                                                        */
  9. /*                    COPYRIGHT (C) IBM CORPORATION 1990 - 1992            */
  10. /*                          ALL RIGHTS RESERVED                            */
  11. /*                                                                         */
  12. /*                                                                         */
  13. /*      FUNCTION :                                                         */
  14. /*                Reads the MMPM2.INI file and loads all information for   */
  15. /*                device management into the CDMC_Main_CB structure.       */
  16. /*                                                                         */
  17. /*                                                                         */
  18. /*      NOTES :                                                            */
  19. /*                                                                         */
  20. /*******************************END OF SPECIFICATIONS***********************/
  21.  
  22.  
  23. #define INCL_BASE
  24. #define INCL_PM
  25. #define INCL_WINWINDOWMGR
  26. #define INCL_MODULEMGR
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <os2.h>
  31. #include <stddef.h>
  32. #include <string.h>
  33. #include "os2me.h"
  34. #include "hhpheap.h"
  35.  
  36. int            CDMCInitialization(void);
  37. USHORT         CDMC_Exit();
  38.  
  39. PVOID          CDMC_hHeap;
  40. DWORD          AccessSem = 0;
  41. USHORT         CDMC_Init = 0;
  42.  
  43. /*
  44.  * NOTE:   This routine utilizes a feature of the IBM C Set/2 Compiler
  45.  *         that frees programmers from having to write assembler stubs to
  46.  *         initialize their re-entrant DLLs.
  47.  *         C Set/2 provides an assembler initialization stub with the
  48.  *         compiler, and that stub sets the entry point of the DLL to
  49.  *         _DLL_InitTerm.  As long as the DLL entry point has this name,
  50.  *         the DLL can take advantage of the C SET/2 assembler stub instead
  51.  *         of writing one of it's own.
  52.  */
  53.  
  54. #pragma linkage (_DLL_InitTerm, system)
  55.  
  56. unsigned long _DLL_InitTerm (unsigned long hModhandle, unsigned long ulTerm)
  57.    {
  58.  
  59.    DWORD SemWait = 7000;
  60.    USHORT InitCompleted;
  61.    INT rc;
  62.  
  63.    /*
  64.     * Checking this parameter will insure that this routine will only
  65.     * be run on an actual initialization.  Return success from the
  66.     * termination.
  67.     */
  68.  
  69.    if (ulTerm)
  70.       return (1L);
  71.  
  72.  
  73.    rc = _CRT_init();
  74.    if (rc) {
  75.       return (0L);
  76.    }
  77.  
  78.    InitCompleted = TRUE;   // Set InitCompleted = True;
  79.  
  80.    /**************************************************************************/
  81.    /* Increment CDMC_Init. If this is the first call to CDMCAttach, then     */
  82.    /* ++CDMC_Init will be 1 , AccessSem will be 0. This causes the semaphore */
  83.    /* to be created, requested and CDMCInitialization called. If ++CDMC_Init */
  84.    /* is >1 or AccessSem != 0, or DosCreateMutexSem fails, then              */
  85.    /* Initialization was previously completed, and HhpAccessHeap will be     */
  86.    /* called                                                                 */
  87.    /**************************************************************************/
  88.    if ( ((++CDMC_Init) == 1) && (!AccessSem) )
  89.       {
  90.  
  91.       /**********************************************************************
  92.       * If DosCreateMutexSem fails, then Init was previously completed.    **
  93.       **********************************************************************/
  94.       if (!(DosCreateMutexSem(NULL,&AccessSem,DC_SEM_SHARED,FALSE)))
  95.          {
  96.  
  97.          /******************************************************************/
  98.          /* Set InitCompleted to false (not Previously initialized)        */
  99.          /******************************************************************/
  100.          InitCompleted = 0;
  101.  
  102.          /******************************************************************/
  103.          /* request the Mutex semaphore                                    */
  104.          /******************************************************************/
  105.          if (DosRequestMutexSem(AccessSem,SemWait))
  106.             {
  107.  
  108.             /***************************************************************/
  109.             /* close the Mutex semaphore                                   */
  110.             /***************************************************************/
  111.             DosCloseMutexSem(AccessSem);
  112.             return(0);
  113.             }
  114.  
  115.          /******************************************************************/
  116.          /*   Initialize the CDMC_Main_CB structure                        */
  117.          /******************************************************************/
  118.          if (CDMCInitialization())
  119.             {
  120.  
  121.             /***************************************************************/
  122.             /* CDMCInitialize failed! Restore The CDMC_Init count          */
  123.             /***************************************************************/
  124.             --CDMC_Init;
  125.  
  126.             /***************************************************************/
  127.             /*  release the Mutex semaphore                                */
  128.             /***************************************************************/
  129.             DosReleaseMutexSem(AccessSem);
  130.  
  131.             /***************************************************************/
  132.             /* close the Mutex semaphore                                   */
  133.             /***************************************************************/
  134.             DosCloseMutexSem(AccessSem);
  135.             return(0);
  136.             } /* end if */
  137.          } /* end if */
  138.       } /* end if */
  139.  
  140.  
  141.    if (InitCompleted)
  142.    /*********************************************************************/
  143.    /* If CDMCInitialization was called by a previous process:           */
  144.    /* Open and request the semaphore and then Provide shared access to  */
  145.    /* the CDMC_Main_CB structure.                                       */
  146.    /*********************************************************************/
  147.       {
  148.       if ((DosOpenMutexSem(NULL,&AccessSem)))
  149.          return(0);
  150.  
  151.       if ((DosRequestMutexSem(AccessSem,SemWait)) )
  152.          {
  153.  
  154.          /****************************************************************/
  155.          /* If request fails, close the Mutex and return an error        */
  156.          /****************************************************************/
  157.          DosCloseMutexSem(AccessSem);
  158.          return(0);
  159.          }
  160.  
  161.       /*****************************************************************/
  162.       /* Provide access to the heap. CDMC_Main_CB has was initialized  */
  163.       /* before this call.                                             */
  164.       /*****************************************************************/
  165.       if (HhpAccessHeap(CDMC_hHeap,HhpGetPID()))
  166.          {
  167.          DosReleaseMutexSem(AccessSem);
  168.          DosCloseMutexSem(AccessSem);
  169.          return(0);
  170.          } /* end if */
  171.  
  172.       } /* end if */
  173.  
  174.     /******************************************************************/
  175.     /* Release the semaphore                                          */
  176.     /******************************************************************/
  177.    if (DosReleaseMutexSem(AccessSem))
  178.       return(0);
  179.  
  180.    /*************************************/
  181.    /* Successful initialization of DLL  */
  182.    /*************************************/
  183.    return(1);
  184.    }
  185.  
  186.  
  187.  
  188. /****************************************************************************
  189. **  CDMCInitialization                                                     **
  190. *****************************************************************************
  191. *
  192. *  Arguments: no arguments
  193. *
  194. *  Return:
  195. ****************************************************************************/
  196.  
  197.  
  198. int CDMCInitialization(void)
  199. {
  200.  
  201.   /***********************************************************************/
  202.   /* Obtain initial heap from memory management routines                 */
  203.   /*   store as pHeap for easy reference                                 */
  204.   /***********************************************************************/
  205.   CDMC_hHeap  = HhpCreateHeap(4096L, HH_SHARED);
  206.  
  207.   return(0);
  208. }
  209.  
  210. /****************************************************************************
  211. **  CDMC_Exit                                                              **
  212. *****************************************************************************
  213. *
  214. *  Arguments: no arguments
  215. *
  216. *  Return:
  217. *          0 Success
  218. *
  219. *  Description: Cleans up instances, CDMC_Init count and allocated memory
  220. *               after a process terminates.
  221. *
  222. *  Global Vars referenced:
  223. *               CDMC_Init
  224. *               CDMC_Main_CB
  225. *
  226. *  Global Vars Modified:
  227. *               CDMC_Init
  228. *               CDMC_Main_CB
  229. *
  230. ****************************************************************************/
  231.  
  232.  
  233. USHORT CDMC_Exit()
  234.  
  235.    {
  236.  
  237.  
  238.    --CDMC_Init;
  239.  
  240.    if (CDMC_Init == 0)
  241.       {
  242.       if (DosCloseMutexSem(AccessSem) )
  243.          return(1);
  244.  
  245.       if(HhpDestroyHeap(CDMC_hHeap) )
  246.          return(1);
  247.  
  248.       }
  249.  
  250.    DosExitList(EXLST_EXIT,(PFNEXITLIST) CDMC_Exit);
  251.    }
  252.  
  253.