From 8c86ba446367fd676457e51eb44d7af2e5d9a392 Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Sat, 22 Jan 2022 00:12:05 +0000 Subject: htl: Fix cleaning the reply port If any RPC fails, the reply port will already be deallocated. __pthread_thread_terminate thus has to defer taking its name until the very last __thread_terminate_release which doesn't reply a message. But then we have to read from the pthread structure. This introduces __pthread_dealloc_finish() which does the recording of the thread termination, so the slot can be reused really only just before the __thread_terminate_release call. Only the real thread can set it, so let's decouple this from the pthread_state by just removing the PTHREAD_TERMINATED state and add a terminated field. --- sysdeps/mach/htl/pt-thread-terminate.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'sysdeps/mach') diff --git a/sysdeps/mach/htl/pt-thread-terminate.c b/sysdeps/mach/htl/pt-thread-terminate.c index 9bd6c3434d..eb60aa057c 100644 --- a/sysdeps/mach/htl/pt-thread-terminate.c +++ b/sysdeps/mach/htl/pt-thread-terminate.c @@ -35,6 +35,7 @@ __pthread_thread_terminate (struct __pthread *thread) void *stackaddr; size_t stacksize; error_t err; + int self; kernel_thread = thread->kernel_thread; @@ -52,25 +53,32 @@ __pthread_thread_terminate (struct __pthread *thread) wakeup_port = thread->wakeupmsg.msgh_remote_port; - /* Each thread has its own reply port, allocated from MiG stub code calling - __mig_get_reply_port. Destroying it is a bit tricky because the calls - involved are also RPCs, causing the creation of a new reply port if - currently null. The __thread_terminate_release call is actually a one way - simple routine designed not to require a reply port. */ self_ktid = __mach_thread_self (); - reply_port = (self_ktid == kernel_thread) - ? __mig_get_reply_port () : MACH_PORT_NULL; + self = self_ktid == kernel_thread; __mach_port_deallocate (__mach_task_self (), self_ktid); /* The kernel thread won't be there any more. */ thread->kernel_thread = MACH_PORT_DEAD; - /* Finally done with the thread structure. */ + /* Release thread resources. */ __pthread_dealloc (thread); - /* The wake up port is now no longer needed. */ + /* The wake up port (needed for locks in __pthread_dealloc) is now no longer + needed. */ __mach_port_destroy (__mach_task_self (), wakeup_port); + /* Each thread has its own reply port, allocated from MiG stub code calling + __mig_get_reply_port. Destroying it is a bit tricky because the calls + involved are also RPCs, causing the creation of a new reply port if + currently null. The __thread_terminate_release call is actually a one way + simple routine designed not to require a reply port. */ + reply_port = self ? __mig_get_reply_port () : MACH_PORT_NULL; + /* From here we shall not use a MIG reply port any more. */ + + /* Finally done with the thread structure (we still needed it to access the + reply port). */ + __pthread_dealloc_finish (thread); + /* Terminate and release all that's left. */ err = __thread_terminate_release (kernel_thread, mach_task_self (), kernel_thread, reply_port, -- cgit 1.4.1