[Git][ghc/ghc][master] rts: Use pthread_setname_np correctly on Darwin

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Fri Oct 14 22:29:35 UTC 2022



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
ee0deb80 by Ben Gamari at 2022-10-14T18:29:20-04:00
rts: Use pthread_setname_np correctly on Darwin

As noted in #22206, pthread_setname_np on Darwin only supports
setting the name of the calling thread. Consequently we must introduce
a trampoline which first sets the thread name before entering the thread
entrypoint.
- - - - -


1 changed file:

- rts/posix/OSThreads.c


Changes:

=====================================
rts/posix/OSThreads.c
=====================================
@@ -186,22 +186,48 @@ shutdownThread(void)
   pthread_exit(NULL);
 }
 
-int
-createOSThread (OSThreadId* pId, char *name STG_UNUSED,
-                OSThreadProc *startProc, void *param)
+struct ThreadDesc {
+    OSThreadProc *startProc;
+    void *param;
+    char *name;
+};
+
+// N.B. Darwin's pthread_setname_np only allows the name of the
+// calling thread to be set. Consequently we must use this
+// trampoline.
+static void *
+start_thread (void *param)
 {
-  int result = pthread_create(pId, NULL, startProc, param);
-  if (!result) {
-    pthread_detach(*pId);
+    struct ThreadDesc desc = *(struct ThreadDesc *) param;
+    stgFree(param);
+
 #if defined(HAVE_PTHREAD_SET_NAME_NP)
-    pthread_set_name_np(*pId, name);
+    pthread_set_name_np(pthread_self(), desc.name);
 #elif defined(HAVE_PTHREAD_SETNAME_NP)
-    pthread_setname_np(*pId, name);
+    pthread_setname_np(pthread_self(), desc.name);
 #elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
-    pthread_setname_np(name);
+    pthread_setname_np(desc.name);
 #elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
-    pthread_setname_np(*pId, "%s", name);
+    pthread_setname_np(pthread_self(), "%s", desc.name);
 #endif
+
+    return desc.startProc(desc.param);
+}
+
+int
+createOSThread (OSThreadId* pId, char *name STG_UNUSED,
+                OSThreadProc *startProc, void *param)
+{
+  struct ThreadDesc *desc = stgMallocBytes(sizeof(struct ThreadDesc), "createOSThread");
+  desc->startProc = startProc;
+  desc->param = param;
+  desc->name = name;
+
+  int result = pthread_create(pId, NULL, start_thread, desc);
+  if (!result) {
+      pthread_detach(*pId);
+  } else {
+      stgFree(desc);
   }
   return result;
 }



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ee0deb8054da2a597fc5624469b4c44fd769ada2

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ee0deb8054da2a597fc5624469b4c44fd769ada2
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20221014/97e589c9/attachment-0001.html>


More information about the ghc-commits mailing list