diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2009-08-23 19:38:06 +0200 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2009-08-23 19:38:06 +0200 |
commit | 4776fb251ce840be1099ffc481116d8a3ee8916a (patch) | |
tree | 85e49d7330191e61d118b35330ef67ea17a3e7c8 | |
parent | 573e2e6130fd6a2dcba09029a97000668c27c6e9 (diff) |
Fix pthread_mutex_t static initializers
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.
-rw-r--r-- | include/pthread/pthread.h | 2 | ||||
-rw-r--r-- | sysdeps/generic/bits/mutex-attr.h | 1 | ||||
-rw-r--r-- | sysdeps/generic/bits/mutex.h | 10 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-destroy.c | 3 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-init.c | 13 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-timedlock.c | 22 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-transfer-np.c | 11 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-trylock.c | 14 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-unlock.c | 16 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutexattr.c | 8 |
10 files changed, 71 insertions, 29 deletions
diff --git a/include/pthread/pthread.h b/include/pthread/pthread.h index dedb09d..fbe3294 100644 --- a/include/pthread/pthread.h +++ b/include/pthread/pthread.h @@ -313,6 +313,8 @@ extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, /* 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 diff --git a/sysdeps/generic/bits/mutex-attr.h b/sysdeps/generic/bits/mutex-attr.h index 9161cda..8514ebe 100644 --- a/sysdeps/generic/bits/mutex-attr.h +++ b/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 */ diff --git a/sysdeps/generic/bits/mutex.h b/sysdeps/generic/bits/mutex.h index 86068cf..c734c39 100644 --- a/sysdeps/generic/bits/mutex.h +++ b/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. */ diff --git a/sysdeps/generic/pt-mutex-destroy.c b/sysdeps/generic/pt-mutex-destroy.c index b0ca00f..3bbc73f 100644 --- a/sysdeps/generic/pt-mutex-destroy.c +++ b/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 diff --git a/sysdeps/generic/pt-mutex-init.c b/sysdeps/generic/pt-mutex-init.c index a818fe3..2f96028 100644 --- a/sysdeps/generic/pt-mutex-init.c +++ b/sysdeps/generic/pt-mutex-init.c @@ -35,14 +35,11 @@ _pthread_mutex_init (pthread_mutex_t *mutex, /* The default attributes. */ return 0; - if (attr == &__pthread_recursive_mutexattr) - /* Non-default but known attributes. */ - { - mutex->attr = attr; - return 0; - } - - mutex->attr = malloc (sizeof *attr); + if (! mutex->attr + || mutex->attr == __PTHREAD_ERRORCHECK_MUTEXATTR + || mutex->attr == __PTHREAD_RECURSIVE_MUTEXATTR) + mutex->attr = malloc (sizeof *attr); + if (! mutex->attr) return ENOMEM; diff --git a/sysdeps/generic/pt-mutex-timedlock.c b/sysdeps/generic/pt-mutex-timedlock.c index ee43219..883e50a 100644 --- a/sysdeps/generic/pt-mutex-timedlock.c +++ b/sysdeps/generic/pt-mutex-timedlock.c @@ -31,6 +31,12 @@ __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, 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 (struct __pthread_mutex *mutex, #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 (struct __pthread_mutex *mutex, 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 (struct __pthread_mutex *mutex, } 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 (struct __pthread_mutex *mutex, } #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 (struct __pthread_mutex *mutex, __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; diff --git a/sysdeps/generic/pt-mutex-transfer-np.c b/sysdeps/generic/pt-mutex-transfer-np.c index 7796ac4..967f1c7 100644 --- a/sysdeps/generic/pt-mutex-transfer-np.c +++ b/sysdeps/generic/pt-mutex-transfer-np.c @@ -29,13 +29,20 @@ __pthread_mutex_transfer_np (struct __pthread_mutex *mutex, pthread_t tid) 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 __pthread_mutex *mutex, pthread_t tid) #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; diff --git a/sysdeps/generic/pt-mutex-trylock.c b/sysdeps/generic/pt-mutex-trylock.c index 3c8cd03..7a54cc9 100644 --- a/sysdeps/generic/pt-mutex-trylock.c +++ b/sysdeps/generic/pt-mutex-trylock.c @@ -29,6 +29,12 @@ __pthread_mutex_trylock (struct __pthread_mutex *mutex) { 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 __pthread_mutex *mutex) #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 __pthread_mutex *mutex) err = EBUSY; - if (mutex->attr) + if (attr) { self = _pthread_self (); - switch (mutex->attr->mutex_type) + switch (attr->mutex_type) { case PTHREAD_MUTEX_NORMAL: break; diff --git a/sysdeps/generic/pt-mutex-unlock.c b/sysdeps/generic/pt-mutex-unlock.c index 7645fd4..09d70f8 100644 --- a/sysdeps/generic/pt-mutex-unlock.c +++ b/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 *mutex) #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 *mutex) 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 *mutex) #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; diff --git a/sysdeps/generic/pt-mutexattr.c b/sysdeps/generic/pt-mutexattr.c index d80a7d7..5ebde6e 100644 --- a/sysdeps/generic/pt-mutexattr.c +++ b/sysdeps/generic/pt-mutexattr.c @@ -28,6 +28,14 @@ const struct __pthread_mutexattr __pthread_default_mutexattr = 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, |