[PATCH] Define internal function mythreadname() This new function returns the name of the calling thread; works for both cygthreads and pthreads. All calls to cygthread::name(/*void*/) replaced by calls to mythreadname(/*void*/).

Mark Geisert mark@maxrnd.com
Tue Jan 23 05:20:00 GMT 2018


---
 winsup/cygwin/exceptions.cc   |  4 ++--
 winsup/cygwin/fhandler_tty.cc |  2 +-
 winsup/cygwin/strace.cc       |  2 +-
 winsup/cygwin/thread.cc       | 17 +++++++++++++++++
 winsup/cygwin/thread.h        |  3 +++
 5 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/exceptions.cc b/winsup/cygwin/exceptions.cc
index 47782e45b..f659540cb 100644
--- a/winsup/cygwin/exceptions.cc
+++ b/winsup/cygwin/exceptions.cc
@@ -200,7 +200,7 @@ cygwin_exception::dump_exception ()
   small_printf ("r14=%016X r15=%016X\r\n", ctx->R14, ctx->R15);
   small_printf ("rbp=%016X rsp=%016X\r\n", ctx->Rbp, ctx->Rsp);
   small_printf ("program=%W, pid %u, thread %s\r\n",
-		myself->progname, myself->pid, cygthread::name ());
+		myself->progname, myself->pid, mythreadname ());
 #else
   if (exception_name)
     small_printf ("Exception: %s at eip=%08x\r\n", exception_name, ctx->Eip);
@@ -210,7 +210,7 @@ cygwin_exception::dump_exception ()
 		ctx->Eax, ctx->Ebx, ctx->Ecx, ctx->Edx, ctx->Esi, ctx->Edi);
   small_printf ("ebp=%08x esp=%08x program=%W, pid %u, thread %s\r\n",
 		ctx->Ebp, ctx->Esp, myself->progname, myself->pid,
-		cygthread::name ());
+		mythreadname ());
 #endif
   small_printf ("cs=%04x ds=%04x es=%04x fs=%04x gs=%04x ss=%04x\r\n",
 		ctx->SegCs, ctx->SegDs, ctx->SegEs, ctx->SegFs,
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 1505b8c2b..0b8185d90 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -106,7 +106,7 @@ fhandler_pty_common::__acquire_output_mutex (const char *fn, int ln,
 #else
       ostack[osi].fn = fn;
       ostack[osi].ln = ln;
-      ostack[osi].tname = cygthread::name ();
+      ostack[osi].tname = mythreadname ();
       termios_printf ("acquired for %s:%d, osi %d", fn, ln, osi);
       osi++;
 #endif
diff --git a/winsup/cygwin/strace.cc b/winsup/cygwin/strace.cc
index 1e7ab047d..b95b436aa 100644
--- a/winsup/cygwin/strace.cc
+++ b/winsup/cygwin/strace.cc
@@ -138,7 +138,7 @@ strace::vsprntf (char *buf, const char *func, const char *infmt, va_list ap)
   char fmt[80];
   static NO_COPY bool nonewline = false;
   DWORD err = GetLastError ();
-  const char *tn = cygthread::name ();
+  const char *tn = mythreadname ();
 
   int microsec = microseconds ();
   lmicrosec = microsec;
diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index f3c709a15..71e17a77f 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -2682,6 +2682,23 @@ pthread_setname_np (pthread_t thread, const char *name)
   return 0;
 }
 
+/* Returns running thread's name; works for both cygthreads and pthreads */
+extern "C" const char *
+mythreadname (void)
+{
+  const char *result = cygthread::name ();
+
+  if (strstr (result, "unknown "))
+    {
+      static char tname[THRNAMELEN];
+
+      tname[0] = '\0';
+      if (0 == pthread_getname_np (pthread_self (), tname, sizeof (tname)))
+        result = tname;
+    }
+
+  return result;
+}
 #undef THRNAMELEN
 
 /* provided for source level compatability.
diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h
index 12a9ef26d..60277c601 100644
--- a/winsup/cygwin/thread.h
+++ b/winsup/cygwin/thread.h
@@ -17,6 +17,9 @@ details. */
 /* resource.cc */
 extern size_t get_rlimit_stack (void);
 
+/* thread.cc */
+extern "C" const char *mythreadname (void);
+
 #include <pthread.h>
 #include <limits.h>
 #include "security.h"
-- 
2.15.1



More information about the Cygwin-patches mailing list