summaryrefslogtreecommitdiff
path: root/sysdeps/generic/pt-mutex-timedlock.c
AgeCommit message (Collapse)Author
2015-03-22Avoid exposing internals of structuresSamuel Thibault
* sysdeps/generic/bits/barrier-attr.h (__pthread_barrierattr): Rename `pshared' field to `__pshared'. * sysdeps/generic/bits/barrier.h (__pthread_barrier): Rename `lock', `'queue', `pending', `count', `attr', and `data' fields to `__lock', `__queue', `__pending', `__count', `__attr', and `__data'. * sysdeps/generic/bits/cancelation.h (__pthread_cancelation_handler): Rename `handler', `arg', and `next' fields to `__handler', `__arg', and `__next'. * sysdeps/generic/bits/condition-attr.h (__pthread_condattr): Rename `pshared' and `clock' fields to `__pshared', and `__clock'. * sysdeps/generic/bits/mutex-attr.h (__pthread_mutexattr): Rename `prioceiling', `protocol', `pshared', and `mutex_type' fields to `__prioceiling', `__protocol', `__pshared', and `__mutex_type'. * sysdeps/generic/bits/mutex.h (__pthread_mutex): Rename `cthreadscompat1', `attr', `data', `owner', and `locks' fields to `__cthreadscompat1', `__attr', `__data', `__owner', and `__locks'. * sysdeps/generic/bits/once.h (__pthread_once): Rename `run' and `lock' fields to `__run' and `__lock'. * sysdeps/generic/bits/rwlock-attr.h (__pthread_rwlockattr): Rename `psharead' field to `__pshared'. * sysdeps/generic/bits/rwlock.h (__pthread_rwlock): Rename `readers', `readerqueue', and `writerqueue' fields to `__readers', `__readerqueue', and `__writerqueue'. * sysdeps/generic/bits/thread-attr.h (__pthread_contentionscope): Rename `schedparam', `stackaddr', `stacksize', `guardsize', `detachstate', `inheritsched', `contentionscope', and `schedpolicy' fields to `__schedparam', `__stackaddr', `__stacksize', `__guardsize', `__detachstate', `__inheritsched', `__contentionscope', and `__schedpolicy''. * sysdeps/generic/bits/cancelation.h: Update code accordingly. * pthread/pt-create.c: Likewise. * pthread/pt-exit.c: Likewise. * pthread/pt-getattr.c: Likewise. * sysdeps/generic/pt-attr-getdetachstate.c: Likewise. * sysdeps/generic/pt-attr-getguardsize.c: Likewise. * sysdeps/generic/pt-attr-getinheritsched.c: Likewise. * sysdeps/generic/pt-attr-getschedparam.c: Likewise. * sysdeps/generic/pt-attr-getschedpolicy.c: Likewise. * sysdeps/generic/pt-attr-getscope.c: Likewise. * sysdeps/generic/pt-attr-getstackaddr.c: Likewise. * sysdeps/generic/pt-attr-getstacksize.c: Likewise. * sysdeps/generic/pt-attr-setdetachstate.c: Likewise. * sysdeps/generic/pt-attr-setguardsize.c: Likewise. * sysdeps/generic/pt-attr-setinheritsched.c: Likewise. * sysdeps/generic/pt-attr-setschedparam.c: Likewise. * sysdeps/generic/pt-attr-setschedpolicy.c: Likewise. * sysdeps/generic/pt-attr-setscope.c: Likewise. * sysdeps/generic/pt-attr-setstackaddr.c: Likewise. * sysdeps/generic/pt-attr-setstacksize.c: Likewise. * sysdeps/generic/pt-attr.c: Likewise. * sysdeps/generic/pt-barrier-init.c: Likewise. * sysdeps/generic/pt-barrier-wait.c: Likewise. * sysdeps/generic/pt-barrier.c: Likewise. * sysdeps/generic/pt-barrierattr-getpshared.c: Likewise. * sysdeps/generic/pt-barrierattr-setpshared.c: Likewise. * sysdeps/generic/pt-cond-timedwait.c: Likewise. * sysdeps/generic/pt-cond.c: Likewise. * sysdeps/generic/pt-condattr-getclock.c: Likewise. * sysdeps/generic/pt-condattr-getpshared.c: Likewise. * sysdeps/generic/pt-condattr-setclock.c: Likewise. * sysdeps/generic/pt-condattr-setpshared.c: Likewise. * sysdeps/generic/pt-mutex-destroy.c: Likewise. * sysdeps/generic/pt-mutex-init.c: Likewise. * sysdeps/generic/pt-mutex-timedlock.c: Likewise. * sysdeps/generic/pt-mutex-transfer-np.c: Likewise. * sysdeps/generic/pt-mutex-trylock.c: Likewise. * sysdeps/generic/pt-mutex-unlock.c: Likewise. * sysdeps/generic/pt-mutexattr-getprotocol.c: Likewise. * sysdeps/generic/pt-mutexattr-getpshared.c: Likewise. * sysdeps/generic/pt-mutexattr-gettype.c: Likewise. * sysdeps/generic/pt-mutexattr-setprotocol.c: Likewise. * sysdeps/generic/pt-mutexattr-setpshared.c: Likewise. * sysdeps/generic/pt-mutexattr-settype.c: Likewise. * sysdeps/generic/pt-mutexattr.c: Likewise. * sysdeps/generic/pt-once.c: Likewise. * sysdeps/generic/pt-rwlock-attr.c: Likewise. * sysdeps/generic/pt-rwlock-timedrdlock.c: Likewise. * sysdeps/generic/pt-rwlock-timedwrlock.c: Likewise. * sysdeps/generic/pt-rwlock-tryrdlock.c: Likewise. * sysdeps/generic/pt-rwlock-trywrlock.c: Likewise. * sysdeps/generic/pt-rwlock-unlock.c: Likewise. * sysdeps/generic/pt-rwlockattr-getpshared.c: Likewise. * sysdeps/generic/pt-rwlockattr-setpshared.c: Likewise. * sysdeps/mach/hurd/pt-attr-setstackaddr.c: Likewise. * sysdeps/mach/hurd/pt-attr-setstacksize.c: Likewise. * sysdeps/mach/hurd/pt-hurd-cond-timedwait.c: Likewise.
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.
2012-04-22__pthread_timedblock: add an argument for the clock idPino Toscano
To make `__pthread_timedblock' properly measure time using the right clock, add a new argument representing the clock to use. * pthread/pt-internal.h (__pthread_timedblock): New argument CLOCK_ID. * sysdeps/l4/pt-timedblock.c (__pthread_timedblock): Likewise. * sysdeps/mach/pt-timedblock.c (__pthread_timedblock): Likewise. * sysdeps/generic/pt-cond-timedwait.c (__pthread_cond_timedwait_internal): Pass the clock of the `pthread_cond' to `__pthread_timedblock'. * sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal): Pass CLOCK_REALTIME to `__pthread_timedblock'. * sysdeps/generic/pt-rwlock-timedrdlock.c (__pthread_rwlock_timedrdlock_internal): Likewise. * sysdeps/generic/pt-rwlock-timedwrlock.c (__pthread_rwlock_timedwrlock_internal): Likewise. * sysdeps/generic/sem-timedwait.c (__sem_timedwait_internal): Likewise.
2009-08-23Fix pthread_mutex_t static initializersSamuel Thibault
Fix pthread_mutex_t static initializers by not leaving references to __pthread_recursive_mutexattr. * include/pthread/pthread.h.orig (PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP): New macro. * sysdeps/generic/pt-mutex-destroy.c (_pthread_mutex_destroy): Compare mutex->attr with __PTHREAD_ERRORCHECK_MUTEXATTR and __PTHREAD_RECURSIVE_MUTEXATTR instead of __pthread_recursive_mutexattr. * sysdeps/generic/pt-mutex-init.c (_pthread_mutex_init): Likewise. * sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal): Likewise. * sysdeps/generic/pt-mutex-transfer-np.c (__pthread_mutex_transfer_np): Likewise. * sysdeps/generic/pt-mutex-trylock.c (__pthread_mutex_trylock): Likewise. * sysdeps/generic/pt-mutex-unlock.c (__pthread_mutex_unlock): Likewise. * sysdeps/generic/pt-mutexattr.c (__pthread_errorcheck_mutexattr): New const. * sysdeps/generic/bits/mutex-attr.h (__pthread_errorcheck_mutexattr): Declare const. * sysdeps/generic/bits/mutex.h (__PTHREAD_ERRORCHECK_MUTEXATTR, __PTHREAD_ERRORCHECK_MUTEX_INITIALIZER, __PTHREAD_RECURSIVE_MUTEXATTR): New macros. (__PTHREAD_RECURSIVE_MUTEX_INITIALIZER): Use __PTHREAD_RECURSIVE_MUTEXATTR macro instead of &__pthread_recursive_mutexattr.
2009-04-082008-10-02 Neal H. Walfield <neal@gnu.org>Neal H. Walfield
* sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal) [! ALWAYS_TRACK_MUTEX_OWNER]: Don't update MUTEX->OWNER or assert MUTEX->OWNER for normal mutexes. * sysdeps/generic/pt-mutex-unlock.c (__pthread_mutex_unlock) [! ALWAYS_TRACK_MUTEX_OWNER]: Don't update MUTEX->OWNER or assert MUTEX->OWNER for normal mutexes. * sysdeps/generic/pt-mutex-transfer-np.c (__pthread_mutex_transfer_np) [! ALWAYS_TRACK_MUTEX_OWNER]: Don't update MUTEX->OWNER for normal mutexes.
2009-04-072008-06-22 Neal H. Walfield <neal@gnu.org>Neal H. Walfield
* sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal): Don't set MUTEX->OWNER here. * sysdeps/generic/pt-mutex-unlock.c (__pthread_mutex_unlock): Set it here to WAKEUP.
2009-04-072008-01-01 Neal H. Walfield <neal@gnu.org>Neal H. Walfield
* sysdeps/generic/pt-mutex-timedlock.c (__pthread_mutex_timedlock_internal): Add additional asserts. [! NDEBUG]: Keep MUTEX->OWNER up to date. * sysdeps/generic/pt-mutex-trylock.c (__pthread_mutex_trylock): Add additional asserts. [! NDEBUG]: Keep MUTEX->OWNER up to date. * sysdeps/generic/pt-mutex-unlock.c (__pthread_mutex_unlock): Add additional asserts. [! NDEBUG]: Keep MUTEX->OWNER up to date.
2005-05-12libpthread/Neal H. Walfield
2005-05-12 Neal H. Walfield <neal@gnu.org> * include/pthread/pthread.h (pthread_exit): Add noreturn attribute. * sysdeps/generic/bits/condition.h (__PTHREAD_COND_INITIALIZER): Don't create a compound literal. * sysdeps/generic/bits/mutex.h (__PTHREAD_MUTEX_INITIALIZER): Don't create a compound literal. (pthread_mutex_init): Don't assign to *__MUTEX directly. Initialize an intermediate local variable and then copy the result. * sysdeps/generic/bits/rwlock.h (__PTHREAD_RWLOCK_INITIALIZER): Don't create a compound literal. (pthread_rwlock_init): Don't assign to *__RWLOCK directly. Initialize an intermediate local variable and then copy the result. * sysdeps/i386/bits/spin-lock.h (__SPIN_LOCK_INITIALIZER): Don't create a compound literal. * pthread/pt-alloc.c (initialize_pthread): Cast PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER to create a compound literal. * tests/test-1.c (main): Use pthread_mutex_init, not PTHREAD_MUTEX_INIT for mutex initialization * sysdeps/generic/pt-barrier-init.c (pthread_barrier_init): Remove assert. Copy ATTR if non-defaults are used. * sysdeps/generic/pt-cond-init.c (pthread_cond_init): Include <string.h>. Remove assert. Copy ATTR if non-defaults are used. * sysdeps/generic/pt-mutex-init.c (_pthread_mutex_init): Cast PTHREAD_MUTEX_INITIALIZER to create a compound literal. * sysdeps/generic/pt-rwlock-init.c: Include <string.h>. (_pthread_rwlock_init): Cast __PTHREAD_RWLOCK_INITIALIZER to create a compound literal. Copy ATTR if non-defaults are used. * sysdeps/generic/pt-cond-timedwait.c (__pthread_cond_timedwait_internal): Check that ABSTIME->TV_NSEC is valid. Don't shadow ERR. Don't return before cleaning up. * sysdeps/generic/pt-mutex-timedlock.c (pthread_mutex_timedlock): Move after __pthread_mutex_timedlock_internal. Check that ABSTIME->TV_NSEC is valid. * sysdeps/generic/pt-rwlock-timedrdlock.c (pthread_rwlock_timedrdlock): Move after. __pthread_rwlock_timedrdlock_internal. (__pthread_rwlock_timedrdlock_internal): Check that ABSTIME->TV_NSEC is valid. * sysdeps/generic/pt-rwlock-timedwrlock.c (pthread_rwlock_timedwrlock): Move after __pthread_rwlock_timedwrlock_internal. (__pthread_rwlock_timedwrlock_internal): Check that ABSTIME->TV_NSEC is valid. * sysdeps/generic/sem-timedwait.c (__sem_timedwait_internal): Check that TIMEOUT->TV_NSEC is valid before enqueuing the thread. * sysdeps/generic/pt-rwlock-rdlock.c (__pthread_rwlock_timedrdlock_internal): Fix declaration. * sysdeps/generic/pt-mutex-trylock.c (__pthread_mutex_trylock): Don't return EDEADLK. POSIX does not allow it. * sysdeps/mach/pt-timedblock.c (__pthread_timedblock): Calculate the relative timeout without overflowing. * sysdeps/mach/hurd/i386/pt-setup.c (stack_setup): Cast THREAD.
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