From aeca2c30eccb3ad9d25d177f31bed2c6c5ae7062 Mon Sep 17 00:00:00 2001 From: roblabla Date: Mon, 11 Dec 2017 23:38:00 +0100 Subject: [PATCH] Close handle on thread release --- pthread/phal.h | 3 +++ pthread/rthread_thread.c | 3 +++ pthread/sys/switch/phal.c | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/pthread/phal.h b/pthread/phal.h index 5ae4699c..a7cacb7d 100644 --- a/pthread/phal.h +++ b/pthread/phal.h @@ -5,6 +5,9 @@ int phal_thread_create(phal_tid *tid, void (*start_routine)(void*), void *arg); void phal_thread_exit(phal_tid *tid); + +// Called after the thread exited, to free up any potential resources +int phal_thread_destroy(phal_tid *tid); void **phal_get_tls(); int phal_semaphore_create(phal_semaphore *sem); diff --git a/pthread/rthread_thread.c b/pthread/rthread_thread.c index 72900c47..a0e69880 100644 --- a/pthread/rthread_thread.c +++ b/pthread/rthread_thread.c @@ -239,6 +239,9 @@ _rthread_reaper(void) // TODO: stack and tib //_rthread_free_stack(thread->stack); //_dl_free_tib(thread->tib, sizeof(*thread)); + int res = phal_thread_destroy(&thread->tib_tid); + if (res) + _rthread_debug(3, "Failed to free tid %d: %d", &thread->tib_tid, res); } else { /* initial thread isn't part of TIB allocation */ _rthread_debug(3, "rthread reaping %p (initial)\n", diff --git a/pthread/sys/switch/phal.c b/pthread/sys/switch/phal.c index 84496740..8bb92eb1 100644 --- a/pthread/sys/switch/phal.c +++ b/pthread/sys/switch/phal.c @@ -27,6 +27,10 @@ void phal_thread_exit(phal_tid *tid) { svcExitThread(); } +int phal_thread_destroy(phal_tid *tid) { + return svcCloseHandle(*tid); +} + int phal_thread_sleep(uint64_t msec) { u64 nanos = msec * 1000 * 1000; return svcSleepThread(nanos);