summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Trangez <ikke@nicolast.be>2022-10-26 18:24:04 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-11-01 12:47:58 -0400
commitedd175c9f9f2988e2836fc3bdc70d627f0f0c5cf (patch)
treed3585a256a82773832df2970f7e5a53dd43ed092
parent13b5f102b3049e177220bd6d867a33d5c8864b12 (diff)
downloadhaskell-edd175c9f9f2988e2836fc3bdc70d627f0f0c5cf.tar.gz
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
-rw-r--r--rts/include/rts/OSThreads.h4
-rw-r--r--rts/posix/OSThreads.c19
-rw-r--r--rts/posix/ticker/Pthread.c23
3 files changed, 20 insertions, 26 deletions
diff --git a/rts/include/rts/OSThreads.h b/rts/include/rts/OSThreads.h
index 563e886469..ebe964be9a 100644
--- a/rts/include/rts/OSThreads.h
+++ b/rts/include/rts/OSThreads.h
@@ -175,6 +175,10 @@ typedef void* OSThreadProcAttr OSThreadProc(void *);
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 );
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c
index 2987e8162b..2784afe863 100644
--- a/rts/posix/OSThreads.c
+++ b/rts/posix/OSThreads.c
@@ -222,16 +222,25 @@ int
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 = stgMallocBytes(strlen(name) + 1, "createOSThread");
+ 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);
}
diff --git a/rts/posix/ticker/Pthread.c b/rts/posix/ticker/Pthread.c
index 01b3a5f901..a4c7af588f 100644
--- a/rts/posix/ticker/Pthread.c
+++ b/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));
}
}