From f20ccdc461dfec44c9451c0ecb69e1bd6d6c60d4 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Tue, 28 Oct 2008 12:04:51 +0000 Subject: [PATCH] Fixed some incorrect behavior pointed out in #2990: * The pthread_mutex_*lock() family should return EDEADLK when re-locking an error-checked mutex. * pthread_mutex_trylock() is supposed to return EBUSY, not B_WOULD_BLOCK. * pthread_mutex_unlock() should return EPERM when the caller is not the owner. It used to print a message and try to unlock anyway. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28354 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../libroot/posix/pthread/pthread_mutex.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/system/libroot/posix/pthread/pthread_mutex.c b/src/system/libroot/posix/pthread/pthread_mutex.c index 78c10e295f..e344a3937f 100644 --- a/src/system/libroot/posix/pthread/pthread_mutex.c +++ b/src/system/libroot/posix/pthread/pthread_mutex.c @@ -85,7 +85,7 @@ mutex_lock(pthread_mutex_t *mutex, bigtime_t timeout) if (MUTEX_TYPE(mutex) == PTHREAD_MUTEX_ERRORCHECK && mutex->owner == thisThread) { // we detect this kind of deadlock and return an error - return B_BUSY; + return EDEADLK; } if (MUTEX_TYPE(mutex) == PTHREAD_MUTEX_RECURSIVE @@ -123,11 +123,12 @@ pthread_mutex_lock(pthread_mutex_t *mutex) int pthread_mutex_trylock(pthread_mutex_t *mutex) { - return mutex_lock(mutex, 0); + status_t status = mutex_lock(mutex, 0); + return status == B_WOULD_BLOCK ? EBUSY : status; } -int +int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *tv) { bool invalidTime = false; @@ -156,11 +157,8 @@ pthread_mutex_unlock(pthread_mutex_t *mutex) if (mutex == NULL) return B_BAD_VALUE; - if (mutex->owner != find_thread(NULL)) { - // this is a bug in the calling application! - // ToDo: should we handle it in another way? - fprintf(stderr, "mutex unlocked from foreign thread!\n"); - } + if (mutex->owner != find_thread(NULL)) + return EPERM; if (MUTEX_TYPE(mutex) == PTHREAD_MUTEX_RECURSIVE) { if (mutex->owner_count-- > 1) @@ -176,7 +174,7 @@ pthread_mutex_unlock(pthread_mutex_t *mutex) } -int +int pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *_prioCeiling) { if (mutex == NULL || _prioCeiling == NULL) @@ -189,7 +187,7 @@ pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *_prioCeiling) } -int +int pthread_mutex_setprioceiling(pthread_mutex_t *mutex, int prioCeiling, int *_oldCeiling) {