aboutsummaryrefslogtreecommitdiff
path: root/winsup/mingw/mthr_init.c
blob: 5c8c8bbe87dccd921a97020b28031ba2f2fcd9a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * mthr_init.c
 *
 * Do the thread-support DLL initialization.
 *
 * This file is used iff the following conditions are met:
 *  - gcc uses -mthreads option 
 *  - user code uses C++ exceptions
 *
 * The sole job of the Mingw thread support DLL (MingwThr) is to catch 
 * all the dying threads and clean up the data allocated in the TLSs 
 * for exception contexts during C++ EH. Posix threads have key dtors, 
 * but win32 TLS keys do not, hence the magic. Without this, there's at 
 * least `24 * sizeof (void*)' bytes leaks for each catch/throw in each
 * thread.
 * 
 * See mthr.c for all the magic.
 *  
 * Created by Mumit Khan  <khan@nanotech.wisc.edu>
 *
 */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#include <stdio.h>

BOOL APIENTRY DllMain (HANDLE hDllHandle, DWORD reason, 
                       LPVOID reserved /* Not used. */ );

/*
 *----------------------------------------------------------------------
 *
 * DllMain --
 *
 *	This routine is called by the Mingw32, Cygwin32 or VC++ C run 
 *	time library init code, or the Borland DllEntryPoint routine. It 
 *	is responsible for initializing various dynamically loaded 
 *	libraries.
 *
 * Results:
 *      TRUE on sucess, FALSE on failure.
 *
 * Side effects:
 *
 *----------------------------------------------------------------------
 */
BOOL APIENTRY
DllMain (HANDLE hDllHandle /* Library instance handle. */,
	 DWORD reason /* Reason this function is being called. */,
	 LPVOID reserved /* Not used. */)
{

  extern CRITICAL_SECTION __mingwthr_cs;
  extern void __mingwthr_run_key_dtors( void );

#ifdef DEBUG
  printf ("%s: reason %d\n", __FUNCTION__, reason );
#endif

  switch (reason)
    {
    case DLL_PROCESS_ATTACH:
       InitializeCriticalSection (&__mingwthr_cs);
       break;

    case DLL_PROCESS_DETACH:
      __mingwthr_run_key_dtors();
       DeleteCriticalSection (&__mingwthr_cs);
      break;

    case DLL_THREAD_ATTACH:
      break;

    case DLL_THREAD_DETACH:
      __mingwthr_run_key_dtors();
      break;
    }
  return TRUE;
}