[Git][ghc/ghc][master] 3 commits: rts: `name` argument of `createOSThread` can be `const`

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Tue Nov 1 16:48:14 UTC 2022



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


Commits:
8ee8b418 by Nicolas Trangez at 2022-11-01T12:47:58-04:00
rts: `name` argument of `createOSThread` can be `const`

Since we don't intend to ever change the incoming string, declare this
to be true.

Also, in the POSIX implementation, the argument is no longer `STG_UNUSED`
(since ee0deb8054da2a597fc5624469b4c44fd769ada2) in any code path.

See: https://gitlab.haskell.org/ghc/ghc/-/commit/ee0deb8054da2a597fc5624469b4c44fd769ada2#note_460080

- - - - -
13b5f102 by Nicolas Trangez at 2022-11-01T12:47:58-04:00
rts: fix lifetime of `start_thread`s `name` value

Since, unlike the code in ee0deb8054da2^, usage of the `name` value
passed to `createOSThread` now outlives said function's lifetime, and
could hence be released by the caller by the time the new thread runs
`start_thread`, it needs to be copied.

See: https://gitlab.haskell.org/ghc/ghc/-/commit/ee0deb8054da2a597fc5624469b4c44fd769ada2#note_460080
See: https://gitlab.haskell.org/ghc/ghc/-/merge_requests/9066

- - - - -
edd175c9 by Nicolas Trangez at 2022-11-01T12:47:58-04:00
rts: fix OS thread naming in ticker

Since ee0deb805, the use of `pthread_setname_np` on Darwin was fixed
when invoking `createOSThread`. However, the 'ticker' has some
thread-creation code which doesn't rely on `createOSThread`, yet also
uses `pthread_setname_np`.

This patch enforces all thread creation to go through a single
function, which uses the (correct) thread-naming code introduced in
ee0deb805.

See: https://gitlab.haskell.org/ghc/ghc/-/commit/ee0deb8054da2a597fc5624469b4c44fd769ada2
See: https://gitlab.haskell.org/ghc/ghc/-/issues/22206
See: https://gitlab.haskell.org/ghc/ghc/-/merge_requests/9066

- - - - -


4 changed files:

- rts/include/rts/OSThreads.h
- rts/posix/OSThreads.c
- rts/posix/ticker/Pthread.c
- rts/win32/OSThreads.c


Changes:

=====================================
rts/include/rts/OSThreads.h
=====================================
@@ -173,8 +173,12 @@ extern void yieldThread           ( void );
 
 typedef void* OSThreadProcAttr OSThreadProc(void *);
 
-extern int  createOSThread        ( OSThreadId* tid, char *name,
+extern int  createOSThread        ( OSThreadId* tid, const char *name,
                                     OSThreadProc *startProc, void *param);
+#if !defined(mingw32_HOST_OS)
+extern int  createAttachedOSThread( OSThreadId *tid, const char *name,
+                                    OSThreadProc *startProc, void *param);
+#endif
 extern bool osThreadIsAlive       ( OSThreadId id );
 extern void interruptOSThread     ( OSThreadId id );
 extern void joinOSThread          ( OSThreadId id );


=====================================
rts/posix/OSThreads.c
=====================================
@@ -198,35 +198,50 @@ struct ThreadDesc {
 static void *
 start_thread (void *param)
 {
-    struct ThreadDesc desc = *(struct ThreadDesc *) param;
-    stgFree(param);
+    struct ThreadDesc *desc = (struct ThreadDesc *) param;
+    OSThreadProc *startProc = desc->startProc;
+    void *startParam = desc->param;
 
 #if defined(HAVE_PTHREAD_SET_NAME_NP)
-    pthread_set_name_np(pthread_self(), desc.name);
+    pthread_set_name_np(pthread_self(), desc->name);
 #elif defined(HAVE_PTHREAD_SETNAME_NP)
-    pthread_setname_np(pthread_self(), desc.name);
+    pthread_setname_np(pthread_self(), desc->name);
 #elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
-    pthread_setname_np(desc.name);
+    pthread_setname_np(desc->name);
 #elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
-    pthread_setname_np(pthread_self(), "%s", desc.name);
+    pthread_setname_np(pthread_self(), "%s", desc->name);
 #endif
 
-    return desc.startProc(desc.param);
+    stgFree(desc->name);
+    stgFree(desc);
+
+    return startProc(startParam);
 }
 
 int
-createOSThread (OSThreadId* pId, char *name STG_UNUSED,
+createOSThread (OSThreadId* pId, const char *name,
                 OSThreadProc *startProc, void *param)
 {
-  struct ThreadDesc *desc = stgMallocBytes(sizeof(struct ThreadDesc), "createOSThread");
+  int result = createAttachedOSThread(pId, name, startProc, param);
+  if (!result) {
+    pthread_detach(*pId);
+  }
+  return result;
+}
+
+int
+createAttachedOSThread (OSThreadId *pId, const char *name,
+                        OSThreadProc *startProc, void *param)
+{
+  struct ThreadDesc *desc = stgMallocBytes(sizeof(struct ThreadDesc), "createAttachedOSThread");
   desc->startProc = startProc;
   desc->param = param;
-  desc->name = name;
+  desc->name = stgMallocBytes(strlen(name) + 1, "createAttachedOSThread");
+  strcpy(desc->name, name);
 
   int result = pthread_create(pId, NULL, start_thread, desc);
-  if (!result) {
-      pthread_detach(*pId);
-  } else {
+  if (result) {
+      stgFree(desc->name);
       stgFree(desc);
   }
   return result;


=====================================
rts/posix/ticker/Pthread.c
=====================================
@@ -188,15 +188,6 @@ initTicker (Time interval, TickProc handle_tick)
 #endif
 
     /*
-     * We can't use the RTS's createOSThread here as we need to remain attached
-     * to the thread we create so we can later join to it if requested
-     *
-     * On FreeBSD 12.2 pthread_set_name_np() is unconditionally declared in
-     * <pthread_np.h>, while pthread_setname_np() is conditionally declared in
-     * <pthread.h> when _POSIX_SOURCE is not defined, but we're including
-     * <rts/PosixSource.h>, so must use pthread_set_name_np() instead.  See
-     * similar code in "rts/posix/OSThreads.c".
-     *
      * Create the thread with all blockable signals blocked, leaving signal
      * handling to the main and/or other threads.  This is especially useful in
      * the non-threaded runtime, where applications might expect sigprocmask(2)
@@ -206,23 +197,13 @@ initTicker (Time interval, TickProc handle_tick)
     sigfillset(&mask);
     sigret = pthread_sigmask(SIG_SETMASK, &mask, &omask);
 #endif
-    ret = pthread_create(&thread, NULL, itimer_thread_func, (void*)handle_tick);
+    ret = createAttachedOSThread(&thread, "ghc_ticker", itimer_thread_func, (void*)handle_tick);
 #if defined(HAVE_SIGNAL_H)
     if (sigret == 0)
         pthread_sigmask(SIG_SETMASK, &omask, NULL);
 #endif
 
-    if (ret == 0) {
-#if defined(HAVE_PTHREAD_SET_NAME_NP)
-        pthread_set_name_np(thread, "ghc_ticker");
-#elif defined(HAVE_PTHREAD_SETNAME_NP)
-        pthread_setname_np(thread, "ghc_ticker");
-#elif defined(HAVE_PTHREAD_SETNAME_NP_DARWIN)
-        pthread_setname_np("ghc_ticker");
-#elif defined(HAVE_PTHREAD_SETNAME_NP_NETBSD)
-        pthread_setname_np(thread, "%s", "ghc_ticker");
-#endif
-    } else {
+    if (ret != 0) {
         barf("Ticker: Failed to spawn thread: %s", strerror(errno));
     }
 }


=====================================
rts/win32/OSThreads.c
=====================================
@@ -42,7 +42,7 @@ shutdownThread()
 }
 
 int
-createOSThread (OSThreadId* pId, char *name STG_UNUSED,
+createOSThread (OSThreadId* pId, const char *name STG_UNUSED,
                 OSThreadProc *startProc, void *param)
 {
     HANDLE h;



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d45d8cb3d9bce11729b840bc96ec4616f559809e...edd175c9f9f2988e2836fc3bdc70d627f0f0c5cf

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d45d8cb3d9bce11729b840bc96ec4616f559809e...edd175c9f9f2988e2836fc3bdc70d627f0f0c5cf
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/20221101/1024019e/attachment-0001.html>


More information about the ghc-commits mailing list