OSDN Git Service

Insert removed author attribution.
[mingw/mingw-org-wsl.git] / src / libcrt / crt / mthr_init.c
1 /**
2  * @file mthr_init.c
3  * @copy 2012 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 /* FIXME - Is this still used by GCC? */
26
27 /*
28  * Created by Mumit Khan  <khan@nanotech.wisc.edu>
29  *
30  * Do the thread-support DLL initialization.
31  *
32  * This file is used iff the following conditions are met:
33  *  - gcc uses -mthreads option 
34  *  - user code uses C++ exceptions
35  *
36  * The sole job of the Mingw thread support DLL (MingwThr) is to catch 
37  * all the dying threads and clean up the data allocated in the TLSs 
38  * for exception contexts during C++ EH. Posix threads have key dtors, 
39  * but win32 TLS keys do not, hence the magic. Without this, there's at 
40  * least `24 * sizeof (void*)' bytes leaks for each catch/throw in each
41  * thread.
42  * 
43  * See mthr.c for all the magic.
44  */
45
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
48 #undef WIN32_LEAN_AND_MEAN
49 #include <stdio.h>
50
51 /*
52  *----------------------------------------------------------------------
53  *
54  * DllMain --
55  *
56  *      This routine is called by the Mingw32, Cygwin32 or VC++ C run 
57  *      time library init code, or the Borland DllEntryPoint routine. It 
58  *      is responsible for initializing various dynamically loaded 
59  *      libraries.
60  *
61  * Results:
62  *      TRUE on sucess, FALSE on failure.
63  *
64  * Side effects:
65  *
66  *----------------------------------------------------------------------
67  */
68 BOOL APIENTRY
69 DllMain (HINSTANCE hDllHandle /* Library instance handle. */,
70          DWORD reason /* Reason this function is being called. */,
71          LPVOID reserved /* Not used. */)
72 {
73
74   extern CRITICAL_SECTION __mingwthr_cs;
75   extern void __mingwthr_run_key_dtors( void );
76
77 #ifdef DEBUG
78   printf ("%s: reason %d\n", __FUNCTION__, reason );
79 #endif
80
81   switch (reason)
82     {
83     case DLL_PROCESS_ATTACH:
84        InitializeCriticalSection (&__mingwthr_cs);
85        break;
86
87     case DLL_PROCESS_DETACH:
88       __mingwthr_run_key_dtors();
89        DeleteCriticalSection (&__mingwthr_cs);
90       break;
91
92     case DLL_THREAD_ATTACH:
93       break;
94
95     case DLL_THREAD_DETACH:
96       __mingwthr_run_key_dtors();
97       break;
98     }
99   return TRUE;
100 }