OSDN Git Service

Include, and make <sys/bsdtypes.h> test suite safe.
[mingw/mingw-org-wsl.git] / mingwrt / mthr_init.c
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( void );
56
57 #ifdef DEBUG
58   printf ("%s: reason %d\n", __FUNCTION__, reason );
59 #endif
60
61   switch (reason)
62     {
63     case DLL_PROCESS_ATTACH:
64        InitializeCriticalSection (&__mingwthr_cs);
65        break;
66
67     case DLL_PROCESS_DETACH:
68       __mingwthr_run_key_dtors();
69        DeleteCriticalSection (&__mingwthr_cs);
70       break;
71
72     case DLL_THREAD_ATTACH:
73       break;
74
75     case DLL_THREAD_DETACH:
76       __mingwthr_run_key_dtors();
77       break;
78     }
79   return TRUE;
80 }