home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warptlk3.zip / TOOLKIT / SAMPLES / MM / CDMCIDRV / CDMCINIT.C < prev    next >
C/C++ Source or Header  |  1995-08-24  |  8KB  |  207 lines

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