diff options
author | Ben Gamari <ben@well-typed.com> | 2022-09-26 15:19:19 +0000 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2022-10-12 17:56:50 -0400 |
commit | 8fcca7a555776b4d4d837024d58d300893a3233c (patch) | |
tree | 5dad7ebce190420583ce4fec2d1364fc4ca9803c | |
parent | 44fcdb04467c23b794a82451c64cbfaed6f4ef62 (diff) | |
download | haskell-wip/T22206.tar.gz |
rts: Use pthread_setname_np correctly on Darwinwip/T22206
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.
-rw-r--r-- | rts/posix/OSThreads.c | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index 20064910ad..3d3fe98611 100644 --- a/rts/posix/OSThreads.c +++ b/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; } |