2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
6 * Written by Kai Tietz <kai.tietz@onevision.com>
13 #include <sect_attribs.h>
21 #ifndef __INTERNAL_FUNC_DEFINED
22 #define __INTERNAL_FUNC_DEFINED
23 typedef void (__cdecl
*_PVFV
)(void);
24 typedef int (__cdecl
*_PIFV
)(void);
25 typedef void (__cdecl
*_PVFI
)(int);
28 extern WINBOOL
__mingw_TLScallback (HANDLE hDllHandle
, DWORD reason
, LPVOID reserved
);
30 #define FUNCS_PER_NODE 30
32 typedef struct TlsDtorNode
{
34 struct TlsDtorNode
*next
;
35 _PVFV funcs
[FUNCS_PER_NODE
];
40 /* TLS raw template data start and end. */
41 _CRTALLOC(".tls$AAA") char _tls_start
= 0;
42 _CRTALLOC(".tls$ZZZ") char _tls_end
= 0;
44 _CRTALLOC(".CRT$XLA") PIMAGE_TLS_CALLBACK __xl_a
= 0;
45 _CRTALLOC(".CRT$XLZ") PIMAGE_TLS_CALLBACK __xl_z
= 0;
48 _CRTALLOC(".tls") const IMAGE_TLS_DIRECTORY64 _tls_used
= {
49 (ULONGLONG
) &_tls_start
+1, (ULONGLONG
) &_tls_end
, (ULONGLONG
) &_tls_index
,
50 (ULONGLONG
) (&__xl_a
+1), (ULONG
) 0, (ULONG
) 0
53 _CRTALLOC(".tls") const IMAGE_TLS_DIRECTORY _tls_used
= {
54 (ULONG
)(ULONG_PTR
) &_tls_start
+1, (ULONG
)(ULONG_PTR
) &_tls_end
,
55 (ULONG
)(ULONG_PTR
) &_tls_index
, (ULONG
)(ULONG_PTR
) (&__xl_a
+1),
61 #ifdef HAVE_ATTRIBUTE_THREAD
62 #define __CRT_THREAD __declspec(thread)
64 #define __CRT_THREAD __thread
68 #define DISABLE_MS_TLS 1
70 static _CRTALLOC(".CRT$XDA") _PVFV __xd_a
= 0;
71 static _CRTALLOC(".CRT$XDZ") _PVFV __xd_z
= 0;
73 #if !defined (DISABLE_MS_TLS)
74 static __CRT_THREAD TlsDtorNode
*dtor_list
;
75 static __CRT_THREAD TlsDtorNode dtor_list_head
;
81 #define MINGWM10_DLL "mingwm10.dll"
82 typedef int (*fMTRemoveKeyDtor
)(DWORD key
);
83 typedef int (*fMTKeyDtor
)(DWORD key
, void (*dtor
)(void *));
84 fMTRemoveKeyDtor __mingw_gMTRemoveKeyDtor
;
85 fMTKeyDtor __mingw_gMTKeyDtor
;
86 int __mingw_usemthread_dll
;
87 static HANDLE __mingw_mthread_hdll
;
90 BOOL WINAPI
__dyn_tls_init (HANDLE
, DWORD
, LPVOID
);
93 __dyn_tls_init (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
100 __mingw_usemthread_dll
= 1;
101 __mingw_mthread_hdll
= LoadLibrary (MINGWM10_DLL
);
102 if (__mingw_mthread_hdll
!= NULL
)
104 __mingw_gMTRemoveKeyDtor
= (fMTRemoveKeyDtor
) GetProcAddress (__mingw_mthread_hdll
, "__mingwthr_remove_key_dtor");
105 __mingw_gMTKeyDtor
= (fMTKeyDtor
) GetProcAddress (__mingw_mthread_hdll
, "__mingwthr_key_dtor");
107 if (__mingw_mthread_hdll
== NULL
|| !__mingw_gMTRemoveKeyDtor
|| !__mingw_gMTKeyDtor
)
109 __mingw_gMTKeyDtor
= NULL
;
110 __mingw_gMTRemoveKeyDtor
= NULL
;
111 if (__mingw_mthread_hdll
)
112 FreeLibrary (__mingw_mthread_hdll
);
113 __mingw_mthread_hdll
= NULL
;
121 /* We don't let us trick here. */
125 if (dwReason
!= DLL_THREAD_ATTACH
)
127 if (dwReason
== DLL_PROCESS_ATTACH
)
128 __mingw_TLScallback (hDllHandle
, dwReason
, lpreserved
);
132 for (pfunc
= &__xd_a
+ 1; pfunc
!= &__xd_z
; ++pfunc
)
140 const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
= (const PIMAGE_TLS_CALLBACK
) __dyn_tls_init
;
141 _CRTALLOC(".CRT$XLC") PIMAGE_TLS_CALLBACK __xl_c
= (PIMAGE_TLS_CALLBACK
) __dyn_tls_init
;
143 int __cdecl
__tlregdtor (_PVFV
);
146 __tlregdtor (_PVFV func
)
150 #if !defined (DISABLE_MS_TLS)
151 if (dtor_list
== NULL
)
153 dtor_list
= &dtor_list_head
;
154 dtor_list_head
.count
= 0;
156 else if (dtor_list
->count
== FUNCS_PER_NODE
)
158 TlsDtorNode
*pnode
= (TlsDtorNode
*) malloc (sizeof (TlsDtorNode
));
162 pnode
->next
= dtor_list
;
165 dtor_list
->count
= 0;
167 dtor_list
->funcs
[dtor_list
->count
++] = func
;
173 __dyn_tls_dtor (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
175 #if !defined (DISABLE_MS_TLS)
176 TlsDtorNode
*pnode
, *pnext
;
180 if (dwReason
!= DLL_THREAD_DETACH
&& dwReason
!= DLL_PROCESS_DETACH
)
182 /* As TLS variables are detroyed already by DLL_THREAD_DETACH
183 call, we have to avoid access on the possible DLL_PROCESS_DETACH
184 call the already destroyed TLS vars.
185 TODO: The used local thread based variables have to be handled
186 manually, so that we can control their lifetime here. */
187 #if !defined (DISABLE_MS_TLS)
188 if (dwReason
!= DLL_PROCESS_DETACH
)
190 for (pnode
= dtor_list
; pnode
!= NULL
; pnode
= pnext
)
192 for (i
= pnode
->count
- 1; i
>= 0; --i
)
194 if (pnode
->funcs
[i
] != NULL
)
195 (*pnode
->funcs
[i
])();
199 free ((void *) pnode
);
203 __mingw_TLScallback (hDllHandle
, dwReason
, lpreserved
);
207 _CRTALLOC(".CRT$XLD") PIMAGE_TLS_CALLBACK __xl_d
= (PIMAGE_TLS_CALLBACK
) __dyn_tls_dtor
;
210 int mingw_initltsdrot_force
= 0;
211 int mingw_initltsdyn_force
=0;
212 int mingw_initltssuo_force
= 0;