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>
19 #define _CRTALLOC(x) __attribute__ ((section (x) ))
22 #ifndef __INTERNAL_FUNC_DEFINED
23 #define __INTERNAL_FUNC_DEFINED
24 typedef void (__cdecl
*_PVFV
)(void);
25 typedef int (__cdecl
*_PIFV
)(void);
26 typedef void (__cdecl
*_PVFI
)(int);
29 extern WINBOOL
__mingw_TLScallback (HANDLE hDllHandle
, DWORD reason
, LPVOID reserved
);
31 #define FUNCS_PER_NODE 30
33 typedef struct TlsDtorNode
{
35 struct TlsDtorNode
*next
;
36 _PVFV funcs
[FUNCS_PER_NODE
];
41 /* TLS raw template data start and end. */
42 _CRTALLOC(".tls$AAA") char _tls_start
= 0;
43 _CRTALLOC(".tls$ZZZ") char _tls_end
= 0;
45 _CRTALLOC(".CRT$XLA") PIMAGE_TLS_CALLBACK __xl_a
= 0;
46 _CRTALLOC(".CRT$XLZ") PIMAGE_TLS_CALLBACK __xl_z
= 0;
49 _CRTALLOC(".tls") const IMAGE_TLS_DIRECTORY64 _tls_used
= {
50 (ULONGLONG
) &_tls_start
+1, (ULONGLONG
) &_tls_end
, (ULONGLONG
) &_tls_index
,
51 (ULONGLONG
) (&__xl_a
+1), (ULONG
) 0, (ULONG
) 0
54 _CRTALLOC(".tls") const IMAGE_TLS_DIRECTORY _tls_used
= {
55 (ULONG
)(ULONG_PTR
) &_tls_start
+1, (ULONG
)(ULONG_PTR
) &_tls_end
,
56 (ULONG
)(ULONG_PTR
) &_tls_index
, (ULONG
)(ULONG_PTR
) (&__xl_a
+1),
62 #ifdef HAVE_ATTRIBUTE_THREAD
63 #define __CRT_THREAD __declspec(thread)
65 #define __CRT_THREAD __thread
69 #define DISABLE_MS_TLS 1
71 static _CRTALLOC(".CRT$XDA") _PVFV __xd_a
= 0;
72 static _CRTALLOC(".CRT$XDZ") _PVFV __xd_z
= 0;
74 #if !defined (DISABLE_MS_TLS)
75 static __CRT_THREAD TlsDtorNode
*dtor_list
;
76 static __CRT_THREAD TlsDtorNode dtor_list_head
;
82 #define MINGWM10_DLL "mingwm10.dll"
83 typedef int (*fMTRemoveKeyDtor
)(DWORD key
);
84 typedef int (*fMTKeyDtor
)(DWORD key
, void (*dtor
)(void *));
85 fMTRemoveKeyDtor __mingw_gMTRemoveKeyDtor
;
86 fMTKeyDtor __mingw_gMTKeyDtor
;
87 int __mingw_usemthread_dll
;
88 static HANDLE __mingw_mthread_hdll
;
91 BOOL WINAPI
__dyn_tls_init (HANDLE
, DWORD
, LPVOID
);
94 __dyn_tls_init (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
101 __mingw_usemthread_dll
= 1;
102 __mingw_mthread_hdll
= LoadLibrary (MINGWM10_DLL
);
103 if (__mingw_mthread_hdll
!= NULL
)
105 __mingw_gMTRemoveKeyDtor
= (fMTRemoveKeyDtor
) GetProcAddress (__mingw_mthread_hdll
, "__mingwthr_remove_key_dtor");
106 __mingw_gMTKeyDtor
= (fMTKeyDtor
) GetProcAddress (__mingw_mthread_hdll
, "__mingwthr_key_dtor");
108 if (__mingw_mthread_hdll
== NULL
|| !__mingw_gMTRemoveKeyDtor
|| !__mingw_gMTKeyDtor
)
110 __mingw_gMTKeyDtor
= NULL
;
111 __mingw_gMTRemoveKeyDtor
= NULL
;
112 if (__mingw_mthread_hdll
)
113 FreeLibrary (__mingw_mthread_hdll
);
114 __mingw_mthread_hdll
= NULL
;
122 /* We don't let us trick here. */
126 if (dwReason
!= DLL_THREAD_ATTACH
)
128 if (dwReason
== DLL_PROCESS_ATTACH
)
129 __mingw_TLScallback (hDllHandle
, dwReason
, lpreserved
);
133 for (pfunc
= &__xd_a
+ 1; pfunc
!= &__xd_z
; ++pfunc
)
141 const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback
= (const PIMAGE_TLS_CALLBACK
) __dyn_tls_init
;
142 _CRTALLOC(".CRT$XLC") PIMAGE_TLS_CALLBACK __xl_c
= (PIMAGE_TLS_CALLBACK
) __dyn_tls_init
;
144 int __cdecl
__tlregdtor (_PVFV
);
147 __tlregdtor (_PVFV func
)
151 #if !defined (DISABLE_MS_TLS)
152 if (dtor_list
== NULL
)
154 dtor_list
= &dtor_list_head
;
155 dtor_list_head
.count
= 0;
157 else if (dtor_list
->count
== FUNCS_PER_NODE
)
159 TlsDtorNode
*pnode
= (TlsDtorNode
*) malloc (sizeof (TlsDtorNode
));
163 pnode
->next
= dtor_list
;
166 dtor_list
->count
= 0;
168 dtor_list
->funcs
[dtor_list
->count
++] = func
;
174 __dyn_tls_dtor (HANDLE hDllHandle
, DWORD dwReason
, LPVOID lpreserved
)
176 #if !defined (DISABLE_MS_TLS)
177 TlsDtorNode
*pnode
, *pnext
;
181 if (dwReason
!= DLL_THREAD_DETACH
&& dwReason
!= DLL_PROCESS_DETACH
)
183 /* As TLS variables are detroyed already by DLL_THREAD_DETACH
184 call, we have to avoid access on the possible DLL_PROCESS_DETACH
185 call the already destroyed TLS vars.
186 TODO: The used local thread based variables have to be handled
187 manually, so that we can control their lifetime here. */
188 #if !defined (DISABLE_MS_TLS)
189 if (dwReason
!= DLL_PROCESS_DETACH
)
191 for (pnode
= dtor_list
; pnode
!= NULL
; pnode
= pnext
)
193 for (i
= pnode
->count
- 1; i
>= 0; --i
)
195 if (pnode
->funcs
[i
] != NULL
)
196 (*pnode
->funcs
[i
])();
200 free ((void *) pnode
);
204 __mingw_TLScallback (hDllHandle
, dwReason
, lpreserved
);
208 _CRTALLOC(".CRT$XLD") PIMAGE_TLS_CALLBACK __xl_d
= (PIMAGE_TLS_CALLBACK
) __dyn_tls_dtor
;
211 int mingw_initltsdrot_force
= 0;
212 int mingw_initltsdyn_force
=0;
213 int mingw_initltssuo_force
= 0;