Bug 1476405: Part 2a - Create nsThread wrappers/set names for chromium threads. r?erahm,jld draft
authorKris Maglione <maglione.k@gmail.com>
Wed, 18 Jul 2018 22:31:30 -0700
changeset 821040 b1f0333414b0d716e874efb5e7916227d42b62b4
parent 821039 f975d460c5eff4f6abd9c58c06c535da8fcebde0
child 821041 68935ef593163b30172950789be82ec5730bc6c9
push id116998
push usermaglione.k@gmail.com
push dateFri, 20 Jul 2018 20:53:25 +0000
reviewerserahm, jld
bugs1476405
milestone63.0a1
Bug 1476405: Part 2a - Create nsThread wrappers/set names for chromium threads. r?erahm,jld MozReview-Commit-ID: FvGhq6nhIde
ipc/chromium/src/base/platform_thread_posix.cc
ipc/chromium/src/base/platform_thread_win.cc
--- a/ipc/chromium/src/base/platform_thread_posix.cc
+++ b/ipc/chromium/src/base/platform_thread_posix.cc
@@ -21,23 +21,29 @@
 #if !defined(OS_MACOSX)
 #include <unistd.h>
 #endif
 
 #if defined(OS_BSD) && !defined(OS_NETBSD) && !defined(__GLIBC__)
 #include <pthread_np.h>
 #endif
 
+#include "nsThreadUtils.h"
+
 #if defined(OS_MACOSX)
 namespace base {
 void InitThreading();
 }  // namespace
 #endif
 
 static void* ThreadFunc(void* closure) {
+  // Create a nsThread wrapper for the current platform thread, and register it
+  // with the thread manager.
+  (void) NS_GetCurrentThread();
+
   PlatformThread::Delegate* delegate =
       static_cast<PlatformThread::Delegate*>(closure);
   delegate->ThreadMain();
   return NULL;
 }
 
 // static
 PlatformThreadId PlatformThread::CurrentId() {
@@ -87,31 +93,20 @@ void PlatformThread::Sleep(int duration_
 void PlatformThread::SetName(const char* name) {
   // On linux we can get the thread names to show up in the debugger by setting
   // the process name for the LWP.  We don't want to do this for the main
   // thread because that would rename the process, causing tools like killall
   // to stop working.
   if (PlatformThread::CurrentId() == getpid())
     return;
 
-  // http://0pointer.de/blog/projects/name-your-threads.html
-  // Set the name for the LWP (which gets truncated to 15 characters).
-  // Note that glibc also has a 'pthread_setname_np' api, but it may not be
-  // available everywhere and it's only benefit over using prctl directly is
-  // that it can set the name of threads other than the current thread.
-#if defined(OS_LINUX)
-  prctl(PR_SET_NAME, reinterpret_cast<uintptr_t>(name), 0, 0, 0); 
-#elif defined(OS_NETBSD)
-  pthread_setname_np(pthread_self(), "%s", (void *)name);
-#elif defined(OS_BSD) && !defined(__GLIBC__)
-  pthread_set_name_np(pthread_self(), name);
-#elif defined(OS_SOLARIS)
-  pthread_setname_np(pthread_self(), name);
-#else
-#endif
+  // Using NS_SetCurrentThreadName, as opposed to using platform APIs directly,
+  // also sets the thread name on the PRThread wrapper, and allows us to
+  // retrieve it using PR_GetThreadName.
+  NS_SetCurrentThreadName(name);
 }
 #endif // !OS_MACOSX
 
 namespace {
 
 bool CreateThread(size_t stack_size, bool joinable,
                   PlatformThread::Delegate* delegate,
                   PlatformThreadHandle* thread_handle) {
--- a/ipc/chromium/src/base/platform_thread_win.cc
+++ b/ipc/chromium/src/base/platform_thread_win.cc
@@ -18,16 +18,20 @@ const DWORD kVCThreadNameException = 0x4
 typedef struct tagTHREADNAME_INFO {
   DWORD dwType;  // Must be 0x1000.
   LPCSTR szName;  // Pointer to name (in user addr space).
   DWORD dwThreadID;  // Thread ID (-1=caller thread).
   DWORD dwFlags;  // Reserved for future use, must be zero.
 } THREADNAME_INFO;
 
 DWORD __stdcall ThreadFunc(void* closure) {
+  // Create a nsThread wrapper for the current platform thread, and register it
+  // with the thread manager.
+  (void) NS_GetCurrentThread();
+
   PlatformThread::Delegate* delegate =
       static_cast<PlatformThread::Delegate*>(closure);
   delegate->ThreadMain();
   return 0;
 }
 
 }  // namespace
 
@@ -43,34 +47,20 @@ void PlatformThread::YieldCurrentThread(
 
 // static
 void PlatformThread::Sleep(int duration_ms) {
   ::Sleep(duration_ms);
 }
 
 // static
 void PlatformThread::SetName(const char* name) {
-#ifdef HAVE_SEH_EXCEPTIONS
-  // The debugger needs to be around to catch the name in the exception.  If
-  // there isn't a debugger, we are just needlessly throwing an exception.
-  if (!::IsDebuggerPresent())
-    return;
-
-  THREADNAME_INFO info;
-  info.dwType = 0x1000;
-  info.szName = name;
-  info.dwThreadID = CurrentId();
-  info.dwFlags = 0;
-
-  MOZ_SEH_TRY {
-    RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
-                   reinterpret_cast<DWORD_PTR*>(&info));
-  } MOZ_SEH_EXCEPT(EXCEPTION_CONTINUE_EXECUTION) {
-  }
-#endif
+  // Using NS_SetCurrentThreadName, as opposed to using platform APIs directly,
+  // also sets the thread name on the PRThread wrapper, and allows us to
+  // retrieve it using PR_GetThreadName.
+  NS_SetCurrentThreadName(name);
 }
 
 // static
 bool PlatformThread::Create(size_t stack_size, Delegate* delegate,
                             PlatformThreadHandle* thread_handle) {
   unsigned int flags = 0;
   if (stack_size > 0) {
     flags = STACK_SIZE_PARAM_IS_A_RESERVATION;