home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / src / mingw-runtime-19991107 / mingw / mthr_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-30  |  1.9 KB  |  77 lines

  1. /*
  2.  * mthr_init.c
  3.  *
  4.  * Do the thread-support DLL initialization.
  5.  *
  6.  * This file is used iff the following conditions are met:
  7.  *  - gcc uses -mthreads option 
  8.  *  - user code uses C++ exceptions
  9.  *
  10.  * The sole job of the Mingw thread support DLL (MingwThr) is to catch 
  11.  * all the dying threads and clean up the data allocated in the TLSs 
  12.  * for exception contexts during C++ EH. Posix threads have key dtors, 
  13.  * but win32 TLS keys do not, hence the magic. Without this, there's at 
  14.  * least `24 * sizeof (void*)' bytes leaks for each catch/throw in each
  15.  * thread.
  16.  * 
  17.  * See mthr.c for all the magic.
  18.  *  
  19.  * Created by Mumit Khan  <khan@nanotech.wisc.edu>
  20.  *
  21.  */
  22.  
  23. #define WIN32_LEAN_AND_MEAN
  24. #include <windows.h>
  25. #undef WIN32_LEAN_AND_MEAN
  26. #include <stdio.h>
  27.  
  28. BOOL APIENTRY DllMain (HANDLE hDllHandle, DWORD reason, 
  29.                        LPVOID reserved /* Not used. */ );
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * DllMain --
  35.  *
  36.  *    This routine is called by the Mingw32, Cygwin32 or VC++ C run 
  37.  *    time library init code, or the Borland DllEntryPoint routine. It 
  38.  *    is responsible for initializing various dynamically loaded 
  39.  *    libraries.
  40.  *
  41.  * Results:
  42.  *      TRUE on sucess, FALSE on failure.
  43.  *
  44.  * Side effects:
  45.  *
  46.  *----------------------------------------------------------------------
  47.  */
  48. BOOL APIENTRY
  49. DllMain (HANDLE hDllHandle /* Library instance handle. */,
  50.      DWORD reason /* Reason this function is being called. */,
  51.      LPVOID reserved /* Not used. */)
  52. {
  53.  
  54.   extern CRITICAL_SECTION __mingwthr_cs;
  55.   extern void __mingwthr_run_key_dtors (DWORD);
  56.  
  57.   switch (reason)
  58.     {
  59.     case DLL_PROCESS_ATTACH:
  60.       InitializeCriticalSection (&__mingwthr_cs);
  61.       break;
  62.  
  63.     case DLL_PROCESS_DETACH:
  64.       DeleteCriticalSection (&__mingwthr_cs);
  65.       break;
  66.  
  67.     case DLL_THREAD_ATTACH:
  68.       break;
  69.  
  70.     case DLL_THREAD_DETACH:
  71.       __mingwthr_run_key_dtors (GetCurrentThreadId ());
  72.       break;
  73.     }
  74.   return TRUE;
  75. }
  76.  
  77.