diff options
author | Richard Braun <rbraun@sceen.net> | 2014-01-22 01:03:00 +0100 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2014-01-22 01:03:00 +0100 |
commit | 014966443ac044ca9d53b6bcec6678b9360cba78 (patch) | |
tree | ee013da78c6c898b94e0e97759c54e051459c8fe | |
parent | 9329c3c49fe5f4cde9a84edd5b9b8f26ce38b76e (diff) |
Fix thread destruction
Releasing a thread reference uses a mutex, which means the kernel
resources used for synchronization must be available until late during
the destruction process. For simplicity, merge __pthread_thread_dealloc
into __pthread_thread_terminate.
* Makefile (libpthread-routines): Remove pt-thread-dealloc.
* pthread/pt-create.c (__pthread_create_internal): Remove call to
__pthread_thread_dealloc.
* pthread/pt-exit.c (__pthread_exit): Likewise.
* pthread/pt-internal.h (__pthread_thread_dealloc): Remove declaration.
(__pthread_thread_terminate): Update description.
* sysdeps/mach/pt-thread-dealloc.c: Remove file.
* sysdeps/mach/pt-thread-terminate.c (__pthread_thread_terminate):
Destroy the wake up port.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | pthread/pt-create.c | 1 | ||||
-rw-r--r-- | pthread/pt-exit.c | 8 | ||||
-rw-r--r-- | pthread/pt-internal.h | 6 | ||||
-rw-r--r-- | sysdeps/mach/pt-thread-dealloc.c | 40 | ||||
-rw-r--r-- | sysdeps/mach/pt-thread-terminate.c | 7 |
6 files changed, 10 insertions, 53 deletions
@@ -119,7 +119,6 @@ libpthread-routines := pt-attr pt-attr-destroy pt-attr-getdetachstate \ \ pt-stack-alloc \ pt-thread-alloc \ - pt-thread-dealloc \ pt-thread-start \ pt-thread-terminate \ pt-startup \ diff --git a/pthread/pt-create.c b/pthread/pt-create.c index 99d1b47..5358b08 100644 --- a/pthread/pt-create.c +++ b/pthread/pt-create.c @@ -217,7 +217,6 @@ __pthread_create_internal (struct __pthread **thread, pthread->tcb = NULL; failed_thread_tls_alloc: #endif /* ENABLE_TLS */ - __pthread_thread_dealloc (pthread); __pthread_thread_terminate (pthread); /* __pthread_thread_terminate has taken care of deallocating the stack and diff --git a/pthread/pt-exit.c b/pthread/pt-exit.c index ea61732..6d827db 100644 --- a/pthread/pt-exit.c +++ b/pthread/pt-exit.c @@ -98,15 +98,11 @@ __pthread_exit (void *status) /* Destroy any signal state. */ __pthread_sigstate_destroy (self); - /* Kernel resources may be used to implement synchronization objects, - release them late. */ - __pthread_thread_dealloc (self); - /* Self terminating requires TLS, so defer the release of the TCB until the thread structure is reused. */ - /* Terminate the kernel thread, release the stack and drop the - self reference. */ + /* Release kernel resources, including the kernel thread and the stack, + and drop the self reference. */ __pthread_thread_terminate (self); /* NOTREACHED */ diff --git a/pthread/pt-internal.h b/pthread/pt-internal.h index 9747f1f..7465761 100644 --- a/pthread/pt-internal.h +++ b/pthread/pt-internal.h @@ -245,14 +245,12 @@ extern int __pthread_setup (struct __pthread *__restrict thread, resources) for THREAD; it must not be placed on the run queue. */ extern int __pthread_thread_alloc (struct __pthread *thread); -/* Deallocate any kernel resources associated with THREAD. */ -extern void __pthread_thread_dealloc (struct __pthread *thread); - /* Start THREAD making it eligible to run. */ extern int __pthread_thread_start (struct __pthread *thread); /* Terminate the kernel thread associated with THREAD, and deallocate its - stack. In addition, THREAD loses one reference. + stack as well as any other kernel resource associated with it. + In addition, THREAD looses one reference. This function can be called by any thread, including the target thread. Since some resources that are destroyed along the kernel thread are diff --git a/sysdeps/mach/pt-thread-dealloc.c b/sysdeps/mach/pt-thread-dealloc.c deleted file mode 100644 index e977c3f..0000000 --- a/sysdeps/mach/pt-thread-dealloc.c +++ /dev/null @@ -1,40 +0,0 @@ -/* Deallocate the kernel thread resources. Mach version. - Copyright (C) 2005 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#include <assert.h> -#include <errno.h> -#include <mach.h> - -#include <pt-internal.h> - -/* Deallocate any kernel resources associated with THREAD except don't - terminate the thread itself. */ -void -__pthread_thread_dealloc (struct __pthread *thread) -{ - /* Why no assert? Easy. When Mach kills a task, it starts by - invalidating the task port and then terminating the threads one - by one. But while it is terminating them, they are still - eligible to be scheduled. Imagine we have two threads, one calls - exit, one calls pthread_exit. The second one may run this after - the mask port can been destroyed thus gratuitously triggering the - assert. */ - __mach_port_destroy (__mach_task_self (), - thread->wakeupmsg.msgh_remote_port); -} diff --git a/sysdeps/mach/pt-thread-terminate.c b/sysdeps/mach/pt-thread-terminate.c index b9d08fc..6672065 100644 --- a/sysdeps/mach/pt-thread-terminate.c +++ b/sysdeps/mach/pt-thread-terminate.c @@ -32,7 +32,7 @@ void __pthread_thread_terminate (struct __pthread *thread) { thread_t kernel_thread, self_ktid; - mach_port_t reply_port; + mach_port_t wakeup_port, reply_port; void *stackaddr; size_t stacksize; error_t err; @@ -50,6 +50,8 @@ __pthread_thread_terminate (struct __pthread *thread) stacksize = 0; } + 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 @@ -64,6 +66,9 @@ __pthread_thread_terminate (struct __pthread *thread) /* Finally done with the thread structure. */ __pthread_dealloc (thread); + /* The wake up port is now no longer needed. */ + __mach_port_destroy (__mach_task_self (), wakeup_port); + /* Terminate and release all that's left. */ err = __thread_terminate_release (kernel_thread, mach_task_self (), kernel_thread, reply_port, |