[MINGW-W64]
[reactos.git] / reactos / lib / 3rdparty / mingw / tlssup.c
index 0088beb..1c32d0d 100644 (file)
@@ -1,20 +1,37 @@
 /**
  * This file has no copyright assigned and is placed in the Public Domain.
  * This file is part of the w64 mingw-runtime package.
- * No warranty is given; refer to the file DISCLAIMER within this package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ *
+ * Written by Kai Tietz  <kai.tietz@onevision.com>
  */
 
 #ifdef CRTDLL
 #undef CRTDLL
 #endif
 
-#include <internal.h>
 #include <sect_attribs.h>
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
 #include <windows.h>
+
+#include <stdio.h>
+#include <memory.h>
 #include <malloc.h>
-#include <crtdbg.h>
+#ifndef _WIN64
+#include <stdlib.h> /* for _winmajor */
+#endif
+
+#ifndef __INTERNAL_FUNC_DEFINED
+#define __INTERNAL_FUNC_DEFINED
+typedef void (__cdecl *_PVFV)(void);
+typedef int (__cdecl *_PIFV)(void);
+typedef void (__cdecl *_PVFI)(int);
+#endif
 
-extern BOOL __mingw_TLScallback (HANDLE hDllHandle, DWORD reason, LPVOID reserved);
+extern WINBOOL __mingw_TLScallback (HANDLE hDllHandle, DWORD reason, LPVOID reserved);
 
 #define FUNCS_PER_NODE 30
 
@@ -66,6 +83,16 @@ static __CRT_THREAD TlsDtorNode dtor_list_head;
 
 extern int _CRT_MT;
 
+#ifndef _WIN64
+#define MINGWM10_DLL "mingwm10.dll"
+typedef int (*fMTRemoveKeyDtor)(DWORD key);
+typedef int (*fMTKeyDtor)(DWORD key, void (*dtor)(void *));
+fMTRemoveKeyDtor __mingw_gMTRemoveKeyDtor;
+fMTKeyDtor __mingw_gMTKeyDtor;
+int __mingw_usemthread_dll;
+static HANDLE __mingw_mthread_hdll;
+#endif
+
 BOOL WINAPI __dyn_tls_init (HANDLE, DWORD, LPVOID);
 
 BOOL WINAPI
@@ -73,6 +100,30 @@ __dyn_tls_init (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
 {
   _PVFV *pfunc;
 
+#ifndef _WIN64
+  if (_winmajor < 4)
+  {
+    __mingw_usemthread_dll = 1;
+    __mingw_mthread_hdll = LoadLibrary (MINGWM10_DLL);
+    if (__mingw_mthread_hdll != NULL)
+    {
+      __mingw_gMTRemoveKeyDtor = (fMTRemoveKeyDtor) GetProcAddress (__mingw_mthread_hdll, "__mingwthr_remove_key_dtor");
+      __mingw_gMTKeyDtor = (fMTKeyDtor)  GetProcAddress (__mingw_mthread_hdll, "__mingwthr_key_dtor");
+    }
+    if (__mingw_mthread_hdll == NULL || !__mingw_gMTRemoveKeyDtor || !__mingw_gMTKeyDtor)
+      {
+       __mingw_gMTKeyDtor = NULL;
+       __mingw_gMTRemoveKeyDtor = NULL;
+       if (__mingw_mthread_hdll)
+         FreeLibrary (__mingw_mthread_hdll);
+       __mingw_mthread_hdll = NULL;
+       _CRT_MT = 0;
+       return TRUE;
+      }
+    _CRT_MT = 1;
+    return TRUE;
+  }
+#endif
   /* We don't let us trick here.  */
   if (_CRT_MT != 2)
    _CRT_MT = 2;
@@ -163,5 +214,5 @@ _CRTALLOC(".CRT$XLD") PIMAGE_TLS_CALLBACK __xl_d = (PIMAGE_TLS_CALLBACK) __dyn_t
 
 
 int mingw_initltsdrot_force = 0;
-int mingw_initltsdyn_force=0;
+int mingw_initltsdyn_force = 0;
 int mingw_initltssuo_force = 0;