From f9480f502a09e8d727ae90b0614a67fd2d4ee2dd Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Tue, 10 Mar 2020 22:55:19 -0400 Subject: rts: Join to concurrent mark thread during shutdown Previously we would take all capabilities but fail to join on the thread itself, potentially resulting in a leaked thread. --- includes/rts/OSThreads.h | 3 ++- rts/posix/OSThreads.c | 8 ++++++++ rts/sm/NonMoving.c | 1 + rts/win32/OSThreads.c | 9 +++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/includes/rts/OSThreads.h b/includes/rts/OSThreads.h index 322624e8ad..c99a269a08 100644 --- a/includes/rts/OSThreads.h +++ b/includes/rts/OSThreads.h @@ -171,7 +171,8 @@ typedef void* OSThreadProcAttr OSThreadProc(void *); extern int createOSThread ( OSThreadId* tid, char *name, OSThreadProc *startProc, void *param); extern bool osThreadIsAlive ( OSThreadId id ); -extern void interruptOSThread (OSThreadId id); +extern void interruptOSThread ( OSThreadId id ); +extern void joinOSThread ( OSThreadId id ); // // Condition Variables diff --git a/rts/posix/OSThreads.c b/rts/posix/OSThreads.c index b34c292dd8..e5cf9e6d6a 100644 --- a/rts/posix/OSThreads.c +++ b/rts/posix/OSThreads.c @@ -377,6 +377,14 @@ interruptOSThread (OSThreadId id) pthread_kill(id, SIGPIPE); } +void +joinOSThread (OSThreadId id) +{ + if (pthread_join(id, NULL) != 0) { + sysErrorBelch("joinOSThread: error %d", errno); + } +} + KernelThreadId kernelThreadId (void) { #if defined(linux_HOST_OS) diff --git a/rts/sm/NonMoving.c b/rts/sm/NonMoving.c index 4519a33314..538583e687 100644 --- a/rts/sm/NonMoving.c +++ b/rts/sm/NonMoving.c @@ -726,6 +726,7 @@ void nonmovingStop(void) "waiting for nonmoving collector thread to terminate"); ACQUIRE_LOCK(&concurrent_coll_finished_lock); waitCondition(&concurrent_coll_finished, &concurrent_coll_finished_lock); + joinOSThread(mark_thread); } #endif } diff --git a/rts/win32/OSThreads.c b/rts/win32/OSThreads.c index 0942382da2..0ef2a7e293 100644 --- a/rts/win32/OSThreads.c +++ b/rts/win32/OSThreads.c @@ -582,6 +582,15 @@ interruptOSThread (OSThreadId id) CloseHandle(hdl); } +void +joinOSThread (OSThreadId id) +{ + int ret = WaitForSingleObject(id, INFINITE); + if (ret != WAIT_OBJECT_0) { + sysErrorBelch("joinOSThread: error %d", ret); + } +} + void setThreadNode (uint32_t node) { if (osNumaAvailable()) -- cgit v1.2.1