Provide more mutex initializers --- libpthread/include/pthread/pthread.h | 2 ++ libpthread/sysdeps/generic/bits/mutex-attr.h | 1 + libpthread/sysdeps/generic/bits/mutex.h | 10 +++++++++- libpthread/sysdeps/generic/pt-mutex-destroy.c | 3 ++- libpthread/sysdeps/generic/pt-mutex-init.c | 11 ++++------- libpthread/sysdeps/generic/pt-mutex-timedlock.c | 22 ++++++++++++++-------- libpthread/sysdeps/generic/pt-mutex-transfer-np.c | 11 +++++++++-- libpthread/sysdeps/generic/pt-mutex-trylock.c | 14 ++++++++++---- libpthread/sysdeps/generic/pt-mutex-unlock.c | 16 +++++++++++----- libpthread/sysdeps/generic/pt-mutexattr.c | 8 ++++++++ 10 files changed, 70 insertions(+), 28 deletions(-) --- a/libpthread/include/pthread/pthread.h +++ b/libpthread/include/pthread/pthread.h @@ -313,6 +313,8 @@ extern int pthread_mutexattr_settype(pth /* Static initializer for recursive mutexes. */ #ifdef __USE_GNU +# define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP \ + __PTHREAD_ERRORCHECK_MUTEX_INITIALIZER # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \ __PTHREAD_RECURSIVE_MUTEX_INITIALIZER #endif --- a/libpthread/sysdeps/generic/pt-mutex-destroy.c +++ b/libpthread/sysdeps/generic/pt-mutex-destroy.c @@ -26,7 +26,8 @@ int _pthread_mutex_destroy (pthread_mutex_t *mutex) { - if (mutex->attr == &__pthread_recursive_mutexattr) + if (mutex->attr == __PTHREAD_ERRORCHECK_MUTEXATTR + || mutex->attr == __PTHREAD_RECURSIVE_MUTEXATTR) /* Static attributes. */ ; else --- a/libpthread/sysdeps/generic/pt-mutex-init.c +++ b/libpthread/sysdeps/generic/pt-mutex-init.c @@ -35,14 +35,11 @@ _pthread_mutex_init (pthread_mutex_t *mu /* The default attributes. */ return 0; - if (attr == &__pthread_recursive_mutexattr) - /* Non-default but known attributes. */ - { - mutex->attr = attr; - return 0; - } + if (! mutex->attr + || mutex->attr == __PTHREAD_ERRORCHECK_MUTEXATTR + || mutex->attr == __PTHREAD_RECURSIVE_MUTEXATTR) + mutex->attr = malloc (sizeof *attr); - mutex->attr = malloc (sizeof *attr); if (! mutex->attr) return ENOMEM; --- a/libpthread/sysdeps/generic/pt-mutex-timedlock.c +++ b/libpthread/sysdeps/generic/pt-mutex-timedlock.c @@ -31,6 +31,12 @@ __pthread_mutex_timedlock_internal (stru const struct timespec *abstime) { struct __pthread *self; + const struct __pthread_mutexattr *attr = mutex->attr; + + if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR) + attr = &__pthread_errorcheck_mutexattr; + if (attr == __PTHREAD_RECURSIVE_MUTEXATTR) + attr = &__pthread_recursive_mutexattr; __pthread_spin_lock (&mutex->__lock); if (__pthread_spin_trylock (&mutex->__held) == 0) @@ -50,8 +56,8 @@ __pthread_mutex_timedlock_internal (stru #endif #endif - if (mutex->attr) - switch (mutex->attr->mutex_type) + if (attr) + switch (attr->mutex_type) { case PTHREAD_MUTEX_NORMAL: break; @@ -75,7 +81,7 @@ __pthread_mutex_timedlock_internal (stru self = _pthread_self (); assert (self); - if (! mutex->attr || mutex->attr->mutex_type == PTHREAD_MUTEX_NORMAL) + if (! attr || attr->mutex_type == PTHREAD_MUTEX_NORMAL) { #if defined(ALWAYS_TRACK_MUTEX_OWNER) assert (mutex->owner != self); @@ -83,7 +89,7 @@ __pthread_mutex_timedlock_internal (stru } else { - switch (mutex->attr->mutex_type) + switch (attr->mutex_type) { case PTHREAD_MUTEX_ERRORCHECK: if (mutex->owner == self) @@ -108,7 +114,7 @@ __pthread_mutex_timedlock_internal (stru } #if !defined(ALWAYS_TRACK_MUTEX_OWNER) - if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL) + if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL) #endif assert (mutex->owner); @@ -147,14 +153,14 @@ __pthread_mutex_timedlock_internal (stru __pthread_block (self); #if !defined(ALWAYS_TRACK_MUTEX_OWNER) - if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL) + if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL) #endif { assert (mutex->owner == self); } - if (mutex->attr) - switch (mutex->attr->mutex_type) + if (attr) + switch (attr->mutex_type) { case PTHREAD_MUTEX_NORMAL: break; --- a/libpthread/sysdeps/generic/pt-mutex-transfer-np.c +++ b/libpthread/sysdeps/generic/pt-mutex-transfer-np.c @@ -29,13 +29,20 @@ __pthread_mutex_transfer_np (struct __pt assert (mutex->owner == _pthread_self ()); struct __pthread *thread = __pthread_getid (tid); + const struct __pthread_mutexattr *attr = mutex->attr; + if (! thread) return ESRCH; if (thread == _pthread_self ()) return 0; - if (mutex->attr && mutex->attr->mutex_type == PTHREAD_MUTEX_ERRORCHECK) + if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR) + attr = &__pthread_errorcheck_mutexattr; + if (attr == __PTHREAD_RECURSIVE_MUTEXATTR) + attr = &__pthread_recursive_mutexattr; + + if (attr && attr->mutex_type == PTHREAD_MUTEX_ERRORCHECK) { if (mutex->owner != _pthread_self ()) @@ -46,7 +53,7 @@ __pthread_mutex_transfer_np (struct __pt #ifndef NDEBUG # if !defined(ALWAYS_TRACK_MUTEX_OWNER) - if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL) + if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL) # endif { mutex->owner = thread; --- a/libpthread/sysdeps/generic/pt-mutex-trylock.c +++ b/libpthread/sysdeps/generic/pt-mutex-trylock.c @@ -29,6 +29,12 @@ __pthread_mutex_trylock (struct __pthrea { int err; struct __pthread *self; + const struct __pthread_mutexattr *attr = mutex->attr; + + if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR) + attr = &__pthread_errorcheck_mutexattr; + if (attr == __PTHREAD_RECURSIVE_MUTEXATTR) + attr = &__pthread_recursive_mutexattr; __pthread_spin_lock (&mutex->__lock); if (__pthread_spin_trylock (&mutex->__held) == 0) @@ -48,8 +54,8 @@ __pthread_mutex_trylock (struct __pthrea #endif #endif - if (mutex->attr) - switch (mutex->attr->mutex_type) + if (attr) + switch (attr->mutex_type) { case PTHREAD_MUTEX_NORMAL: break; @@ -70,10 +76,10 @@ __pthread_mutex_trylock (struct __pthrea err = EBUSY; - if (mutex->attr) + if (attr) { self = _pthread_self (); - switch (mutex->attr->mutex_type) + switch (attr->mutex_type) { case PTHREAD_MUTEX_NORMAL: break; --- a/libpthread/sysdeps/generic/pt-mutex-unlock.c +++ b/libpthread/sysdeps/generic/pt-mutex-unlock.c @@ -28,10 +28,16 @@ int __pthread_mutex_unlock (pthread_mutex_t *mutex) { struct __pthread *wakeup; - + const struct __pthread_mutexattr *attr = mutex->attr; + + if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR) + attr = &__pthread_errorcheck_mutexattr; + if (attr == __PTHREAD_RECURSIVE_MUTEXATTR) + attr = &__pthread_recursive_mutexattr; + __pthread_spin_lock (&mutex->__lock); - if (! mutex->attr || mutex->attr->mutex_type == PTHREAD_MUTEX_NORMAL) + if (! attr || attr->mutex_type == PTHREAD_MUTEX_NORMAL) { #if defined(ALWAYS_TRACK_MUTEX_OWNER) # ifndef NDEBUG @@ -45,7 +51,7 @@ __pthread_mutex_unlock (pthread_mutex_t #endif } else - switch (mutex->attr->mutex_type) + switch (attr->mutex_type) { case PTHREAD_MUTEX_ERRORCHECK: case PTHREAD_MUTEX_RECURSIVE: @@ -55,7 +61,7 @@ __pthread_mutex_unlock (pthread_mutex_t return EPERM; } - if (mutex->attr->mutex_type == PTHREAD_MUTEX_RECURSIVE) + if (attr->mutex_type == PTHREAD_MUTEX_RECURSIVE) if (--mutex->locks > 0) { __pthread_spin_unlock (&mutex->__lock); @@ -82,7 +88,7 @@ __pthread_mutex_unlock (pthread_mutex_t #ifndef NDEBUG # if !defined (ALWAYS_TRACK_MUTEX_OWNER) - if (mutex->attr && mutex->attr->mutex_type != PTHREAD_MUTEX_NORMAL) + if (attr && attr->mutex_type != PTHREAD_MUTEX_NORMAL) # endif { mutex->owner = wakeup; --- a/libpthread/sysdeps/generic/pt-mutexattr.c +++ b/libpthread/sysdeps/generic/pt-mutexattr.c @@ -28,6 +28,14 @@ const struct __pthread_mutexattr __pthre mutex_type: PTHREAD_MUTEX_DEFAULT }; +const struct __pthread_mutexattr __pthread_errorcheck_mutexattr = +{ + prioceiling: 0, + protocol: PTHREAD_PRIO_NONE, + pshared: PTHREAD_PROCESS_PRIVATE, + mutex_type: PTHREAD_MUTEX_ERRORCHECK +}; + const struct __pthread_mutexattr __pthread_recursive_mutexattr = { prioceiling: 0, --- a/libpthread/sysdeps/generic/bits/mutex.h +++ b/libpthread/sysdeps/generic/bits/mutex.h @@ -57,9 +57,17 @@ struct __pthread_mutex # define __PTHREAD_MUTEX_INITIALIZER \ { __PTHREAD_SPIN_LOCK_INITIALIZER, __PTHREAD_SPIN_LOCK_INITIALIZER, 0, 0, 0, 0, 0, 0 } +# define __PTHREAD_ERRORCHECK_MUTEXATTR ((struct __pthread_mutexattr *) ((unsigned long) __PTHREAD_MUTEX_ERRORCHECK + 1)) + +# define __PTHREAD_ERRORCHECK_MUTEX_INITIALIZER \ + { __PTHREAD_SPIN_LOCK_INITIALIZER, __PTHREAD_SPIN_LOCK_INITIALIZER, 0, 0, \ + __PTHREAD_ERRORCHECK_MUTEXATTR, 0, 0, 0 } + +# define __PTHREAD_RECURSIVE_MUTEXATTR ((struct __pthread_mutexattr *) ((unsigned long) __PTHREAD_MUTEX_RECURSIVE + 1)) + # define __PTHREAD_RECURSIVE_MUTEX_INITIALIZER \ { __PTHREAD_SPIN_LOCK_INITIALIZER, __PTHREAD_SPIN_LOCK_INITIALIZER, 0, 0, \ - (struct __pthread_mutexattr *) &__pthread_recursive_mutexattr, 0, 0, 0 } + __PTHREAD_RECURSIVE_MUTEXATTR, 0, 0, 0 } # endif #endif /* Not __pthread_mutex_defined. */ --- a/libpthread/sysdeps/generic/bits/mutex-attr.h +++ b/libpthread/sysdeps/generic/bits/mutex-attr.h @@ -35,6 +35,7 @@ struct __pthread_mutexattr }; /* Attributes for a recursive mutex. */ +extern const struct __pthread_mutexattr __pthread_errorcheck_mutexattr; extern const struct __pthread_mutexattr __pthread_recursive_mutexattr; #endif /* bits/mutex-attr.h */