From 65a76a0fb932805d6f1714e507c871c863037222 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 6 Jun 2023 23:02:23 -0400 Subject: [PATCH] pthread & os/locks: Add some more assertions and error checks. The first of these assertions in the pthread code is actually possible to trigger under some specific circumstances, which is ticket #18436. This makes that problem more obvious when it does happen. Change-Id: I026ea6e4c569a7c20d82b70722f752d87e57c5a1 Reviewed-on: https://review.haiku-os.org/c/haiku/+/6536 Reviewed-by: waddlesplash Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- src/system/libroot/os/locks/mutex.cpp | 3 +++ src/system/libroot/posix/pthread/pthread_mutex.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/system/libroot/os/locks/mutex.cpp b/src/system/libroot/os/locks/mutex.cpp index ac074a6bda..77a78c87bb 100644 --- a/src/system/libroot/os/locks/mutex.cpp +++ b/src/system/libroot/os/locks/mutex.cpp @@ -95,4 +95,7 @@ __mutex_unlock(mutex *lock) && (oldValue & B_USER_MUTEX_DISABLED) == 0) { _kern_mutex_unlock(&lock->lock, 0); } + + if ((oldValue & B_USER_MUTEX_LOCKED) == 0) + debugger("mutex was not actually locked!"); } diff --git a/src/system/libroot/posix/pthread/pthread_mutex.cpp b/src/system/libroot/posix/pthread/pthread_mutex.cpp index d086b2a3f0..63f6d18486 100644 --- a/src/system/libroot/posix/pthread/pthread_mutex.cpp +++ b/src/system/libroot/posix/pthread/pthread_mutex.cpp @@ -8,6 +8,7 @@ #include #include "pthread_private.h" +#include #include #include #include @@ -91,6 +92,7 @@ __pthread_mutex_lock(pthread_mutex_t* mutex, uint32 flags, bigtime_t timeout) } // we have locked the mutex for the first time + assert(mutex->owner == -1); mutex->owner = thisThread; mutex->owner_count = 1; @@ -176,6 +178,12 @@ pthread_mutex_unlock(pthread_mutex_t* mutex) if ((oldValue & B_USER_MUTEX_WAITING) != 0) _kern_mutex_unlock((int32*)&mutex->lock, 0); + if (MUTEX_TYPE(mutex) == PTHREAD_MUTEX_ERRORCHECK + || MUTEX_TYPE(mutex) == PTHREAD_MUTEX_DEFAULT) { + if ((oldValue & B_USER_MUTEX_LOCKED) == 0) + return EPERM; + } + return 0; }