summaryrefslogtreecommitdiff
path: root/sysdeps/mach/pt-thread-alloc.c
AgeCommit message (Collapse)Author
2013-12-26Implement thread destructionRichard Braun
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.
2013-02-04Fix pthread timeout handling and cancellation issuesRichard Braun
This patch solves two issues. The first one is cancellation handling when a cancellation request is sent before reaching a cancellation point (namely, pthread_cond_{timed,}wait). Cancellation is implemented by pushing an appropriate cleanup handler and switching to PTHREAD_CANCEL_ASYNCHRONOUS type. The main problem is that it doesn't handle pending requests, only a cancellation that occurs while blocking. Other problems occur when trying to correctly handle a timeout and a cancellation request through the cleanup routine. The other issue is correctly handling timeouts. This problem was already well known, as explained by the following comment : "FIXME: What do we do if we get a wakeup message before we disconnect ourself? It may remain until the next time we block." In addition, the prevp thread member is inconsistently used. It is sometimes accessed while protected by the appropriate queue lock to determine whether a thread is still queued, while at times, threads are unqueued without holding a lock, as in pthread_cond_broadcast : /* We can safely walk the list of waiting threads without holding the lock since it is now decoupled from the condition. */ __pthread_dequeuing_iterate (wakeup, wakeup) __pthread_wakeup (wakeup); This is the root cause that triggers some assertion failures. The solution brought by this patch is to consistently use the prevp link to determine if both a thread has been unqueued and if a wakeup message has been sent (both are needed to wake up a thread). A thread unblocked because of a timeout can now accurately determine if it needs to drain its message queue. A direct improvement is that the message queue size can be limited to one message, and wakeups are guaranteed to be non-blocking, which allows safely calling __pthread_wakeup from critical sections. As it now affects the cleanup cancellation routine of __pthread_cond_timedwait_internal, cancellation is reworked as well. Cancellation type is forced to PTHREAD_CANCEL_DEFERRED during the call, and actually checked on both entry and return. A hook is set by the blocking thread so that the waker doesn't need to know about the call implementation. Cancellation members are now protected with a mutex for truely safe access. * pthread/pt-alloc.c (initialize_pthread): Initialize the new `cancel_lock', `cancel_hook' and `cancel_hook_args' fields. * pthread/pt-cancel.c (pthread_cancel): Rework cancellation handling. * pthread/pt-internal.h (struct __pthread): Add `cancel_lock', `cancel_hook' and `cancel_hook_args' fields. (__pthread_dequeue): Assert thread->prevp isn't NULL. * pthread/pt-join.c (pthread_join): Describe how the cancellation point is implemented. * pthread/pt-setcancelstate.c (__pthread_setcancelstate): Lock the given thread cancellation lock when switching state. * pthread/pt-setcanceltype.c (__pthread_setcanceltype): Likewise for cancellation type. * pthread/pt-testcancel.c (pthread_testcancel): Likewise for pending cancellations. * sysdeps/generic/pt-cond-brdcast.c (__pthread_cond_broadcast): Dequeue and wake up threads with condition locked. * sysdeps/generic/pt-cond-signal.c (cond_signal): Remove function, move implementation to ... (__pthread_cond_signal): ... this function. Remove unused `unblocked' variable. * sysdeps/generic/pt-cond-timedwait.c (struct cancel_ctx): New structure. (cancel_hook): New static function. (__pthread_cond_timedwait_internal): Fix cancellation and timeout handling. * sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal): Fix timeout handling. * sysdeps/generic/pt-rwlock-timedrdlock.c (__pthread_rwlock_timedrdlock_internal): Likewise. * sysdeps/generic/pt-rwlock-timedwrlock.c (__pthread_rwlock_timedwrlock_internal): Likewise. * sysdeps/generic/pt-rwlock-unlock.c (pthread_rwlock_unlock): Dequeue and wake up threads with rwlock internal lock held. * sysdeps/generic/sem-timedwait.c (__sem_timedwait_internal): Fix timeout handling. * sysdeps/mach/hurd/pt-docancel.c (__pthread_do_cancel): Unlock the given thread cancellation lock. * sysdeps/mach/pt-thread-alloc.c (create_wakeupmsg): Limit the message queue size of the wakeup port to 1. * sysdeps/mach/pt-wakeup.c (__pthread_wakeup): Call __mach_msg in a non-blocking way.
2009-04-082008-08-26 Neal H. Walfield <neal@gnu.org>Neal H. Walfield
* sysdeps/mach/pt-thread-alloc.c (__pthread_thread_alloc): Set THREAD->HAVE_KERNEL_RESOURCES to 1, not true. Reported by Flávio Cruz <Flávio Cruz>.
2009-04-072008-08-16 Neal H. Walfield <neal@gnu.org>Neal H. Walfield
* pthread/pt-alloc.c: Don't include <bits/atomic.h>. (__pthread_free_threads): Change to a struct __pthread *. (__pthread_free_threads_lock): New variable. (__pthread_alloc): When looking for a TCB to reuse, iterate over __pthread_free_threads taking the first for which the STATE field is PTHREAD_TERMINATED. When reusing a TCB, first call __pthread_thread_halt on it. * pthread/pt-dealloc.c: Don't include <bits/atomic.h>. (__pthread_free_threads): Change to a struct __pthread *. (__pthread_free_threads_lock): New declaration. (__pthread_dealloc): Enqueue PTHREAD on __PTHREAD_FREE_THREADS. Set PTHREAD->STATE to PTHREAD_TERMINATED after everything else. * pthread/pt-join.c (pthread_join): Call __pthread_thread_halt before destroying the thread. When destroying the thread, call __pthread_thread_dealloc on it. * pthread/pt-detach.c (pthread_detach): If destroying the thread, call __pthread_thread_halt before deallocating the stack. In this case, also call __pthread_thread_dealloc on the tcb. * pthread/pt-exit.c (pthread_exit): Call __pthread_dealloc only if the thread is detached and then as the last thing we do before calling __pthread_thread_halt. * pthread/pt-internal.h (__pthread_thread_halt): Remove argument NEED_DEALLOC. Update users. * sysdeps/mach/pt-thread-halt.c (__pthread_thread_halt): Remove argument need_dealloc. * sysdeps/mach/hurd/pt-sysdep.h (PTHREAD_SYSDEP_MEMBERS): Add field have_kernel_resources. * sysdeps/mach/hurd/pt-thread-alloc.c (__pthread_thread_alloc): If THREAD->HAVE_KERNEL_RESOURCES is true, just return. After allocating the resources, set THREAD->HAVE_KERNEL_RESOURCES to true.
2005-05-04libpthread/Neal H. Walfield
2005-05-04 Neal H. Walfield <neal@gnu.org> * Makefile (SRCS): Add pt-thread_dealloc.c. * sysdeps/mach/pt-thread-dealloc.c: New file. * pthread/pt-internal.h (__pthread_thread_dealloc): New declaration. (__pthread_thread_halt): Add parameter NEED_DEALLOC. Update callers. * sysdeps/mach/pt-thread-halt.c (__pthread_thread_halt): Respect new NEED_DEALLOC parameter. Move code which deallocates kernel resources from here ... * sysdeps/mach/pt-thread-dealloc.c (__pthread_thread_dealloc): ...to here. * pthread/pt-create.c (__pthread_create_internal): Call __pthread_thread_dealloc on failure. * pthread/pt-exit.c (pthread_exit): Call __pthread_thread_dealloc. * sysdeps/mach/pt-thread-alloc.c (create_wakeupmsg): Call __mach_port_destroy to deallocate the receive right. __mach_port_deallocate won't do it. * pthread/pt-detach.c (pthread_detach): Don't call __pthread_thread_halt a second time. * sysdeps/mach/hurd/pt-sysdep.c (_cthread_init_routine): Fix declaration. (init_routine): Update declaration and remove gratuitous cast.
2002-11-182002-11-18 Neal H. Walfield <neal@cs.uml.edu>Neal H. Walfield
* sysdeps/mach/pt-wakeup.c (__pthread_wakeup): Use the size of THREAD->wakeupmsg which may not be a mach_msg_header_t. * sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal): Really test for equality. * sysdeps/generic/pt-rwlock-timedrdlock.c (__pthread_rwlock_timedrdlock_internal): Likewise. * sysdeps/generic/pt-rwlock-timedwrlock.c (__pthread_rwlock_timedwrlock_internal): Likewise. * sysdeps/generic/pt-cond-timedwait.c (__pthread_cond_timedwait_internal): On timeout, remove our thread structure from the wait queue if necessary. * sysdeps/l4/pt-start.c (__pthread_start): Call L4_Myself, not __mach_thread_self. * sysdeps/mach/hurd/i386/pt-setup.c: Include <mach.h>. (__pthread_setup): Do not leak references from __mach_thread_self. * sysdeps/mach/hurd/pt-docancel.c (__pthread_do_cancel): Likewise. * sysdeps/mach/hurd/pt-sysdep.h (_pthread_self): Likewise. * sysdeps/mach/pt-thread-alloc.c (__pthread_thread_alloc): Likewise. * sysdeps/mach/pt-thread-start.c (__pthread_thread_start): Likewise. * sysdeps/mach/pt-start.c: Remove dead file.
2002-10-10Import libpthread.Neal H. Walfield