...rather than lazily on first use with pthread_once_t. That
enables us to simplify a few things (which will be done in a
follow-up commit). We also free the TLS slot on library
unload.

Note that pthread_tls_init is an exported function. For now
I've kept it, however calling it is very dangerous because
you loose state.
---
 mingw-w64-libraries/winpthreads/src/thread.c | 44 +++++++++++---------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/thread.c 
b/mingw-w64-libraries/winpthreads/src/thread.c
index ddf35c3a..48a6041b 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -58,9 +58,7 @@ void (**_pthread_key_dest)(void *) = NULL;
 static volatile long _pthread_cancelling;
 static int _pthread_concur;
 
-/* FIXME Will default to zero as needed */
-static pthread_once_t _pthread_tls_once;
-static DWORD _pthread_tls = 0xffffffff;
+static DWORD _pthread_tls = TLS_OUT_OF_INDEXES;
 
 static pthread_rwlock_t _pthread_key_lock = PTHREAD_RWLOCK_INITIALIZER;
 static unsigned long _pthread_key_max=0L;
@@ -164,6 +162,24 @@ SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
 #endif
 }
 
+void
+pthread_tls_init (void)
+{
+  if (_pthread_tls == TLS_OUT_OF_INDEXES) {
+    _pthread_tls = TlsAlloc();
+
+    /* Cannot continue if out of indexes */
+    if (_pthread_tls == TLS_OUT_OF_INDEXES)
+      abort();
+  }
+}
+
+static void
+pthread_tls_fini (void)
+{
+  TlsFree (_pthread_tls);
+}
+
 /* Search the list idList for an element with identifier ID.  If
    found, its associated _pthread_v pointer is returned, otherwise
    NULL.
@@ -463,15 +479,18 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
 
   if (dwReason == DLL_PROCESS_DETACH)
     {
-      free_pthread_mem ();
+      if (!lpreserved) {
+        free_pthread_mem ();
+        pthread_tls_fini ();
+      }
     }
   else if (dwReason == DLL_PROCESS_ATTACH)
     {
+      pthread_tls_init ();
     }
   else if (dwReason == DLL_THREAD_DETACH)
     {
-      if (_pthread_tls != 0xffffffff)
-       t = (_pthread_v *)TlsGetValue(_pthread_tls);
+      t = (_pthread_v *) TlsGetValue (_pthread_tls);
       if (t && t->thread_noposix != 0)
        {
          _pthread_cleanup_dest (t->x);
@@ -964,16 +983,6 @@ pthread_equal (pthread_t t1, pthread_t t2)
   return (t1 == t2);
 }
 
-void
-pthread_tls_init (void)
-{
-  _pthread_tls = TlsAlloc();
-
-  /* Cannot continue if out of indexes */
-  if (_pthread_tls == TLS_OUT_OF_INDEXES)
-    abort();
-}
-
 void
 _pthread_cleanup_dest (pthread_t t)
 {
@@ -1030,8 +1039,6 @@ __pthread_self_lite (void)
   _pthread_v *t;
   pthread_spinlock_t new_spin_keys = PTHREAD_SPINLOCK_INITIALIZER;
 
-  _pthread_once_raw (&_pthread_tls_once, pthread_tls_init);
-
   t = (_pthread_v *) TlsGetValue (_pthread_tls);
   if (t)
     return t;
@@ -1540,7 +1547,6 @@ pthread_create_wrapper (void *args)
 
   pthread_mutex_lock (&mtx_pthr_locked);
   pthread_mutex_lock (&tv->p_clock);
-  _pthread_once_raw(&_pthread_tls_once, pthread_tls_init);
   TlsSetValue(_pthread_tls, tv);
   tv->tid = GetCurrentThreadId();
   pthread_mutex_unlock (&tv->p_clock);
-- 
2.55.0.windows.5



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to