diff options
author | Neal H. Walfield <neal@gnu.org> | 2008-06-22 09:05:31 +0000 |
---|---|---|
committer | Neal H. Walfield <neal@gnu.org> | 2008-06-22 09:05:31 +0000 |
commit | 05ec15bddd06d4790adb7dd27df03b5cf226d386 (patch) | |
tree | d1e04ffa3f11a6a7f9ab1246c0c86137b3e805bb /sysdeps/generic | |
parent | e59767048830a9a4c8f541aa86c925f767c04bfb (diff) |
2008-06-22 Neal H. Walfield <neal@gnu.org>
* sysdeps/generic/pt-mutex-timedlock.c
(__pthread_mutex_timedlock_internal) [! NDEBUG]: Set MUTEX->OWNER
appropriately and assert that it is consistent.
* sysdeps/generic/pt-mutex-unlock.c (__pthread_mutex_unlock) [!
NDEBUG]: Set MUTEX->OWNER appropriately and assert that it is
consistent.
* sysdeps/generic/pt-mutex-trylock.c (__pthread_mutex_trylock) [!
NDEBUG]: Set MUTEX->OWNER.
Diffstat (limited to 'sysdeps/generic')
-rw-r--r-- | sysdeps/generic/pt-mutex-timedlock.c | 24 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-trylock.c | 14 | ||||
-rw-r--r-- | sysdeps/generic/pt-mutex-unlock.c | 28 |
3 files changed, 59 insertions, 7 deletions
diff --git a/sysdeps/generic/pt-mutex-timedlock.c b/sysdeps/generic/pt-mutex-timedlock.c index 5e222bd3..265a4530 100644 --- a/sysdeps/generic/pt-mutex-timedlock.c +++ b/sysdeps/generic/pt-mutex-timedlock.c @@ -1,5 +1,5 @@ /* Lock a mutex with a timeout. Generic version. - Copyright (C) 2000, 2002, 2005 Free Software Foundation, Inc. + Copyright (C) 2000, 2002, 2005, 2008 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -36,6 +36,18 @@ __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, if (__pthread_spin_trylock (&mutex->__held) == 0) /* Successfully acquired the lock. */ { +#ifndef NDEBUG + self = _pthread_self (); + if (self) + /* The main thread may take a lock before the library is fully + initialized, in particular, before the main thread has a + TCB. */ + { + assert (! mutex->owner); + mutex->owner = _pthread_self (); + } +#endif + if (mutex->attr) switch (mutex->attr->mutex_type) { @@ -59,12 +71,14 @@ __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, /* The lock is busy. */ self = _pthread_self (); + assert (self); if (mutex->attr) { switch (mutex->attr->mutex_type) { case PTHREAD_MUTEX_NORMAL: + assert (mutex->owner != self); break; case PTHREAD_MUTEX_ERRORCHECK: @@ -88,6 +102,10 @@ __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, LOSE; } } + else + assert (mutex->owner != self); + + assert (mutex->owner); if (abstime && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)) return EINVAL; @@ -123,6 +141,10 @@ __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, else __pthread_block (self); +#ifndef NDEBUG + assert (mutex->owner == self); +#endif + if (mutex->attr) switch (mutex->attr->mutex_type) { diff --git a/sysdeps/generic/pt-mutex-trylock.c b/sysdeps/generic/pt-mutex-trylock.c index d56f6e1c..5264dc7b 100644 --- a/sysdeps/generic/pt-mutex-trylock.c +++ b/sysdeps/generic/pt-mutex-trylock.c @@ -1,5 +1,5 @@ /* Try to Lock a mutex. Generic version. - Copyright (C) 2002, 2005 Free Software Foundation, Inc. + Copyright (C) 2002, 2005, 2008 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -34,6 +34,18 @@ __pthread_mutex_trylock (struct __pthread_mutex *mutex) if (__pthread_spin_trylock (&mutex->__held) == 0) /* Acquired the lock. */ { +#ifndef NDEBUG + self = _pthread_self (); + if (self) + /* The main thread may take a lock before the library is fully + initialized, in particular, before the main thread has a + TCB. */ + { + assert (! mutex->owner); + mutex->owner = _pthread_self (); + } +#endif + if (mutex->attr) switch (mutex->attr->mutex_type) { diff --git a/sysdeps/generic/pt-mutex-unlock.c b/sysdeps/generic/pt-mutex-unlock.c index 2f719d3e..d2a42570 100644 --- a/sysdeps/generic/pt-mutex-unlock.c +++ b/sysdeps/generic/pt-mutex-unlock.c @@ -1,5 +1,5 @@ /* Unlock a mutex. Generic version. - Copyright (C) 2000,02 Free Software Foundation, Inc. + Copyright (C) 2000, 2002, 2008 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -31,12 +31,25 @@ __pthread_mutex_unlock (pthread_mutex_t *mutex) __pthread_spin_lock (&mutex->__lock); - if (mutex->attr) + if (! mutex->attr || mutex->attr->mutex_type == PTHREAD_MUTEX_NORMAL) + { +#ifndef NDEBUG + if (_pthread_self ()) + { + assert (mutex->owner); + assertx (mutex->owner == _pthread_self (), + "%p(%x) != %p(%x)", + mutex->owner, + ((struct __pthread *) mutex->owner)->threadid, + _pthread_self (), + _pthread_self ()->threadid); + mutex->owner = NULL; + } +#endif + } + else switch (mutex->attr->mutex_type) { - case PTHREAD_MUTEX_NORMAL: - break; - case PTHREAD_MUTEX_ERRORCHECK: case PTHREAD_MUTEX_RECURSIVE: if (mutex->owner != _pthread_self ()) @@ -59,6 +72,7 @@ __pthread_mutex_unlock (pthread_mutex_t *mutex) LOSE; } + if (mutex->__queue == NULL) { __pthread_spin_unlock (&mutex->__held); @@ -69,6 +83,10 @@ __pthread_mutex_unlock (pthread_mutex_t *mutex) wakeup = mutex->__queue; __pthread_dequeue (wakeup); +#ifndef NDEBUG + mutex->owner = wakeup; +#endif + /* We do not unlock MUTEX->held: we are transferring the ownership to the thread that we are waking up. */ |