summaryrefslogtreecommitdiff
path: root/pthread/pt-exit.c
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2013-12-26 21:19:58 +0100
committerRichard Braun <rbraun@sceen.net>2013-12-26 21:19:58 +0100
commitd8569a7a43651ca1c1149540619254e224ed7c9e (patch)
tree165328a64d7c0ef77bedf108e91724b74ec1d810 /pthread/pt-exit.c
parent01f8736f297da40f0ee327d73794579be2ec3ecf (diff)
Implement thread destruction
This change makes libpthread release almost every resource allocated for a thread, including the kernel thread, its send right, its reply port and its stack. This improves resource usage after peaks of activity during which servers can create hundreds or even thousands of threads. To achieve this, the library relies on the recently added thread_terminate_release one-way GNU Mach RPC, which allows threads to release their last resources along with terminating in a single operation. The pthread_exit function unconditionally releases all the resources it can, including other kernel objects (namely the port used for blocking and waking up) and signal states. When releasing the pthread structure, a reference counter is used so that joinable threads remain available. Once the reference counter drops to 0, the pthread structure can be recycled. Thread local storage (TLS) is also recycled since it needs to remain allocated while terminating the thread, as it is there that the reply port is stored. TLS could be released too, after grabbing the reply port name, but it is difficult to make sure no RPC involving a reply port is used afterwards, so the simpler solution of recycling TLS was chosen. * Makefile (libpthread-routines): Replace pt-thread-halt with pt-thread-terminate. * pthread/pt-alloc.c (initialize_pthread): Set reference counter and unconditionally initialize new threads completely. (__pthread_alloc): Remove call to __pthread_thread_halt, update calls to initialize_pthread. * pthread/pt-create.c (__pthread_create_internal): Don't attempt to reuse stacks, handle reference counter, update failure handling. * pthread/pt-dealloc.c: Include bits/pt-atomic.h. (__pthread_dealloc): Make pthread structure available for reuse when reference counter reaches 0. * pthread/pt-detach.c (pthread_detach): Assume the target thread takes care of its own resources and, as a result, simply unreference its pthread struct. * pthread/pt-exit.c (__pthread_exit): Release resources and terminate. * pthread/pt-internal.h (struct __pthread): New `nr_refs' member. (__pthread_alloc): Update description. (__pthread_dealloc): Likewise. (__pthread_thread_dealloc): Likewise. (__pthread_thread_terminate): New declaration. * pthread/pt-join.c (pthread_join): Assume the target thread takes care of its own resources and, as a result, simply unreference its pthread struct. * sysdeps/mach/hurd/pt-sigstate-destroy.c (__pthread_sigstate_destroy): Call _hurd_sigstate_delete. * sysdeps/mach/hurd/pt-sigstate-init.c (__pthread_sigstate_init): Call _hurd_thread_sigstate and _hurd_sigstate_set_global_rcv when appropriate. * sysdeps/mach/hurd/pt-sysdep.c (__pthread_create_internal): Prevent the library from releasing the stack of the main thread. * sysdeps/mach/hurd/pt-sysdep.h (PTHREAD_SYSDEP_MEMBERS): Remove `have_kernel_resources' from the list of sysdep members. * sysdeps/mach/pt-thread-alloc.c (__pthread_thread_alloc): Update thread allocation. * sysdeps/mach/pt-thread-dealloc.c (__pthread_thread_dealloc): Update description. * sysdeps/mach/pt-thread-halt.c: Remove file. * sysdeps/mach/pt-thread-start.c (__pthread_thread_start): Fix the conditions under which a thread should actually be started. * sysdeps/mach/pt-thread-terminate.c: New file.
Diffstat (limited to 'pthread/pt-exit.c')
-rw-r--r--pthread/pt-exit.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/pthread/pt-exit.c b/pthread/pt-exit.c
index 8468b80..ea61732 100644
--- a/pthread/pt-exit.c
+++ b/pthread/pt-exit.c
@@ -48,12 +48,6 @@ __pthread_exit (void *status)
pthread_setcancelstate (oldstate, &oldstate);
- /* Destory any thread specific data. */
- __pthread_destroy_specific (self);
-
- /* Destroy any signal state. */
- __pthread_sigstate_destroy (self);
-
/* Decrease the number of threads. We use an atomic operation to
make sure that only the last thread calls `exit'. */
if (__atomic_dec_and_test (&__pthread_total))
@@ -77,15 +71,8 @@ __pthread_exit (void *status)
break;
case PTHREAD_DETACHED:
- /* Make sure that nobody can reference this thread anymore, and
- mark it as terminated. Our thread ID will immediately become
- available for re-use. For obvious reasons, we cannot
- deallocate our own stack and TLS. However, it will eventually be
- reused when this thread structure is recycled. */
__pthread_mutex_unlock (&self->state_lock);
- __pthread_dealloc (self);
-
break;
case PTHREAD_JOINABLE:
@@ -105,12 +92,22 @@ __pthread_exit (void *status)
break;
}
- /* Note that after this point the resources used by this thread can
- be freed at any moment if another thread joins or detaches us.
- This means that before freeing any resources, such a thread
- should make sure that this thread is really halted. */
-
- __pthread_thread_halt (self);
+ /* Destroy any thread specific data. */
+ __pthread_destroy_specific (self);
+
+ /* 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. */
+ __pthread_thread_terminate (self);
/* NOTREACHED */
abort ();