winpthreads: For DLL version use DllMain for calling TLS hook

Callbacks defined via PIMAGE_TLS_CALLBACK are not called on Win9x systems,
and also they are not called on all pre-Vista systems when DLL library is
loaded at runtime by LoadLibrary() call.

Co-authored-by: LIU Hao <lh_mouse@126.com>
Signed-off-by: LIU Hao <lh_mouse@126.com>
diff --git a/mingw-w64-libraries/winpthreads/src/thread.c b/mingw-w64-libraries/winpthreads/src/thread.c
index ddf35c3..09d6a73 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -523,6 +523,25 @@
     }
 }
 
+#ifdef DLL_EXPORT
+
+/* For DLL builds register TLS hook via DllMain(). Entrypoint DllMain()
+ * works on all Windows versions (Win32s, Win9x, WinNT) for all cases.
+ *
+ * Callbacks defined via PIMAGE_TLS_CALLBACK are not called on Win9x
+ * systems, and also they are not called on pre-Vista systems when
+ * DLL library is loaded at runtime by LoadLibrary() call.
+ */
+
+BOOL WINAPI
+DllMain (HANDLE hDllHandle, DWORD dwReason, LPVOID lpReserved)
+{
+  __dyn_tls_pthread (hDllHandle, dwReason, lpReserved);
+  return TRUE;
+}
+
+#else
+
 /* TLS-runtime section variable.  */
 
 /* Force a reference to _tls_used to make the linker create the TLS
@@ -562,6 +581,8 @@
 WINPTHREADS_ATTRIBUTE((WINPTHREADS_SECTION(".CRT$XLF")))
 const PIMAGE_TLS_CALLBACK __xl_f = __dyn_tls_pthread;
 
+#endif
+
 /* Internal collect-once structure.  */
 typedef struct collect_once_t {
   pthread_once_t *o;
@@ -1039,7 +1060,7 @@
   t = (struct _pthread_v *) pop_pthread_mem ();
 
   /* If cannot initialize main thread, then the only thing we can do is return null pthread_t */
-  if (!__xl_f || !t)
+  if (!t)
     return 0;
 
   t->p_state = PTHREAD_DEFAULT_ATTR /*| PTHREAD_CREATE_DETACHED*/;