home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / mm / cdmcidrv / cdmcinit.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  8KB  |  210 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. int _CRT_init( VOID );
  41.  
  42. /*
  43.  * NOTE:   This routine utilizes a feature of the IBM C Set/2 Compiler
  44.  *         that frees programmers from having to write assembler stubs to
  45.  *         initialize their re-entrant DLLs.
  46.  *         C Set/2 provides an assembler initialization stub with the
  47.  *         compiler, and that stub sets the entry point of the DLL to
  48.  *         _DLL_InitTerm.  As long as the DLL entry point has this name,
  49.  *         the DLL can take advantage of the C SET/2 assembler stub instead
  50.  *         of writing one of it's own.
  51.  */
  52.  
  53. #pragma linkage (_DLL_InitTerm, system)
  54.  
  55. unsigned long _DLL_InitTerm (unsigned long hModhandle, unsigned long ulTerm)
  56.    {
  57.  
  58.    ULONG SemWait = 7000;
  59.    USHORT InitCompleted;
  60.    INT rc;
  61.  
  62.    /*
  63.     * Checking this parameter will insure that this routine will only
  64.     * be run on an actual initialization.  Return success from the
  65.     * termination.
  66.     */
  67.  
  68.    if (ulTerm)
  69.       return (1L);
  70.  
  71.  
  72.    rc = _CRT_init();
  73.    if (rc)
  74.    {
  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 HHP heap                                      */
  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 HHP heap.                                                     */
  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. Heap was initialized before this  */
  163.       /* 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.