On 22.04.2015 14:37, Yaron Keren wrote:
> 2015-04-22 14:15 GMT+03:00 LRN:
>> On 22.04.2015 14:02, Óscar Fuentes wrote:
>>> LRN writes:
>>>
>>>> Here's a patch to fix this.
>>>> It does solve the problem for the simple testcase, but i have no idea
>>>> how it
>>>> would behave in the wild, i didn't test it on anything else. One might
>>>> notice
>>>> that it is set to crash when certain things happen. I'm not sure
>>>> whether these
>>>> things would happen with correct real-life code. If they do, we'll have
>>>> to find
>>>> another way.
>>>
>>>> +  if (EPERM == pthread_spin_destroy (old))
>>>> +    {
>>>> +      fprintf(stderr, "Error cleaning up spin_keys for thread %lu\n",
>> GetCurrentThreadId ());
>>>> +      abort ();
>>>> +    }
>>>
>>> Why is fprintf used for reporting critical errors? On a GUI application
>>> running outside of a debugger that's a guarantee that the message will
>>> be invisible.
>>
>> Probably true, but that's the easiest debug-message-spewing method i could
>> think of at the time. Also, it calls abort() immediately after that, which
>> terminates the application. Once debugger is hooked up, the message should
>> be
>> (hopefully) seen (unless the app eliminates stderr completely).
>>
>> Point is that this isn't supposed to happen at all, and right now eveyone's
>> goal should be to figure out whether it actually happens or not. If it
>> does,
>> then this code won't go live anyway. If it doesn't, this debug message
>> won't be
>> relevant.
>>
> stderr is not likely to show up anywhere with a GUI program.
> The standard practice in Windows is to use OutputDebugString.
>
> https://msdn.microsoft.com/en-us/library/windows/desktop
> /aa363362%28v=vs.85%29.aspx

Here. Happy now?

-- 
O< ascii ribbon - stop html email! - www.asciiribbon.org
From 98864d12cb2d695b31e504f8c748f6fe57aff8f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1?=
 =?UTF-8?q?=D1=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= <lrn1...@gmail.com>
Date: Wed, 22 Apr 2015 06:34:36 +0000
Subject: [PATCH] Try to eliminate spin_keys memleak

---
 mingw-w64-libraries/winpthreads/src/thread.c | 53 ++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 10 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/thread.c 
b/mingw-w64-libraries/winpthreads/src/thread.c
index 5c40f12..1f7968f 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -23,6 +23,7 @@
 #include <windows.h>
 #include <strsafe.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <malloc.h>
 #include <signal.h>
 #include "pthread.h"
@@ -374,6 +375,38 @@ free_pthread_mem (void)
   pthread_mutex_unlock (&mtx_pthr_locked);
 }
 
+static void
+replace_spin_keys (pthread_spinlock_t *old, pthread_spinlock_t new)
+{
+  if (old == NULL)
+    return;
+
+  if (EPERM == pthread_spin_destroy (old))
+    {
+#define THREADERR "Error cleaning up spin_keys for thread "
+#define THREADERR_LEN ((sizeof (THREADERR) / sizeof (*THREADERR)) - 1)
+#define THREADID_LEN THREADERR_LEN + 66 + 1 + 1
+      int i;
+      char thread_id[THREADID_LEN] = THREADERR;
+      _ultoa ((unsigned long) GetCurrentThreadId (), 
&thread_id[THREADERR_LEN], 10);
+      for (i = THREADERR_LEN; thread_id[i] != '\0' && i < THREADID_LEN - 1; 
i++)
+        {
+        }
+      if (i < THREADID_LEN - 1)
+        {
+          thread_id[i] = '\n';
+          thread_id[i + 1] = '\0';
+        }
+#undef THREADERR
+#undef THREADERR_LEN
+#undef THREADID_LEN
+      OutputDebugStringA (thread_id);
+      abort ();
+    }
+
+  *old = new;
+}
+
 /* Hook for TLS-based deregistration/registration of thread.  */
 static BOOL WINAPI
 __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
@@ -415,7 +448,7 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
              t->h = NULL;
            }
          pthread_mutex_destroy (&t->p_clock);
-         t->spin_keys = new_spin_keys;
+         replace_spin_keys (&t->spin_keys, new_spin_keys);
          push_pthread_mem (t);
          t = NULL;
          TlsSetValue (_pthread_tls, t);
@@ -434,14 +467,14 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
                CloseHandle (t->h);
              t->h = NULL;
              pthread_mutex_destroy(&t->p_clock);
-             t->spin_keys = new_spin_keys;
+             replace_spin_keys (&t->spin_keys, new_spin_keys);
              push_pthread_mem (t);
              t = NULL;
              TlsSetValue (_pthread_tls, t);
              return TRUE;
            }
          pthread_mutex_destroy(&t->p_clock);
-         t->spin_keys = new_spin_keys;
+         replace_spin_keys (&t->spin_keys, new_spin_keys);
        }
       else if (t)
        {
@@ -449,7 +482,7 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
            CloseHandle (t->evStart);
          t->evStart = NULL;
          pthread_mutex_destroy (&t->p_clock);
-         t->spin_keys = new_spin_keys;
+         replace_spin_keys (&t->spin_keys, new_spin_keys);
        }
     }
   return TRUE;
@@ -963,7 +996,7 @@ __pthread_self_lite (void)
   t->tid = GetCurrentThreadId();
   t->evStart = CreateEvent (NULL, 1, 0, NULL);
   t->p_clock = PTHREAD_MUTEX_INITIALIZER;
-  t->spin_keys = new_spin_keys;
+  replace_spin_keys (&t->spin_keys, new_spin_keys);
   t->sched_pol = SCHED_OTHER;
   t->h = NULL; //GetCurrentThread();
   if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), 
GetCurrentProcess(), &t->h, 0, FALSE, DUPLICATE_SAME_ACCESS))
@@ -1521,7 +1554,7 @@ pthread_create (pthread_t *th, const pthread_attr_t 
*attr, void *(* func)(void *
   while (++redo <= 4);
 
   tv->p_clock = PTHREAD_MUTEX_INITIALIZER;
-  tv->spin_keys = new_spin_keys;
+  replace_spin_keys (&tv->spin_keys, new_spin_keys);
   tv->valid = LIFE_THREAD;
   tv->sched.sched_priority = THREAD_PRIORITY_NORMAL;
   tv->sched_pol = SCHED_OTHER;
@@ -1559,7 +1592,7 @@ pthread_create (pthread_t *th, const pthread_attr_t 
*attr, void *(* func)(void *
       if (tv->evStart)
        CloseHandle (tv->evStart);
       pthread_mutex_destroy (&tv->p_clock);
-      tv->spin_keys = new_spin_keys;
+      replace_spin_keys (&tv->spin_keys, new_spin_keys);
       tv->evStart = NULL;
       tv->h = 0;
       if (th)
@@ -1621,7 +1654,7 @@ pthread_join (pthread_t t, void **res)
   if (res)
     *res = tv->ret_arg;
   pthread_mutex_destroy (&tv->p_clock);
-  tv->spin_keys = new_spin_keys;
+  replace_spin_keys (&tv->spin_keys, new_spin_keys);
   push_pthread_mem (tv);
 
   return 0;
@@ -1671,7 +1704,7 @@ _pthread_tryjoin (pthread_t t, void **res)
   if (res)
     *res = tv->ret_arg;
   pthread_mutex_destroy (&tv->p_clock);
-  tv->spin_keys = new_spin_keys;
+  replace_spin_keys (&tv->spin_keys, new_spin_keys);
 
   push_pthread_mem (tv);
 
@@ -1714,7 +1747,7 @@ pthread_detach (pthread_t t)
            CloseHandle (tv->evStart);
          tv->evStart = NULL;
          pthread_mutex_destroy (&tv->p_clock);
-         tv->spin_keys = new_spin_keys;
+         replace_spin_keys (&tv->spin_keys, new_spin_keys);
          push_pthread_mem (tv);
        }
     }
-- 
1.8.5.3

Attachment: 0x922360B0.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud 
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to