fix(libkernel): default mutexes to error checking and add the adaptive type

The PS5 libthr (FreeBSD) creates error-checking mutexes by default and has
PTHREAD_MUTEX_ADAPTIVE_NP (type 4); relocking an error-checking or adaptive
mutex returns EDEADLK. Default mutexes were Normal and type 4 was mapped to
Normal, so a relock threw instead of returning EDEADLK, which aborted an
Unreal Engine title's recursive directory scan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pacuka
2026-09-29 21:33:15 +02:00
parent 2cf901bcff
commit 98f050f879
3 changed files with 32 additions and 4 deletions
@@ -17,6 +17,7 @@ enum class MutexType : std::uint32_t {
ErrorCheck = 1,
Recursive = 2,
Normal = 3,
Adaptive = 4,
};
struct PthreadMutexattrPrivate {
@@ -30,7 +31,7 @@ struct PthreadMutexPrivate {
std::atomic<std::thread::id> _owner;
int _count;
PthreadMutexPrivate() : _type(MutexType::Normal), _owner(std::thread::id{}), _count(0) {}
PthreadMutexPrivate() : _type(MutexType::ErrorCheck), _owner(std::thread::id{}), _count(0) {}
};
struct PthreadRwlockattrPrivate {
@@ -52,7 +52,7 @@ int acquireMutex(PthreadMutex mutex, TAcquire acquire, int unavailable, bool try
if (owned && mutex->_type != MutexType::Recursive) {
if (tryOnly)
return sceBusy;
if (mutex->_type == MutexType::ErrorCheck)
if (mutex->_type == MutexType::ErrorCheck || mutex->_type == MutexType::Adaptive)
return sceDeadlock;
throw std::runtime_error("Mutex is already owned by the current thread");
}
@@ -90,7 +90,7 @@ extern "C" {
int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr) {
if (!attr)
throw std::invalid_argument("Mutex attribute pointer is null");
*attr = new PthreadMutexattrPrivate{MutexType::Normal};
*attr = new PthreadMutexattrPrivate{MutexType::ErrorCheck};
return 0;
}
@@ -109,7 +109,7 @@ int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type) {
case 1: (*attr)->type = MutexType::ErrorCheck; break;
case 2: (*attr)->type = MutexType::Recursive; break;
case 3: (*attr)->type = MutexType::Normal; break;
case 4: (*attr)->type = MutexType::Normal; break;
case 4: (*attr)->type = MutexType::Adaptive; break;
default: throw std::invalid_argument("Invalid mutex type");
}
return 0;
+27
View File
@@ -34,6 +34,7 @@ static constexpr int MUTEX_TYPE_ERRORCHECK = 1;
static constexpr int PRIO_NONE = 0;
static constexpr int PRIO_INHERIT = 1;
static constexpr int PRIO_PROTECT = 2;
static constexpr int MUTEX_TYPE_ADAPTIVE = 4;
static void Require(bool value) { if (!value) std::abort(); }
@@ -88,4 +89,30 @@ int main() {
Require(scePthreadMutexLock(&mutex) == SCE_KERNEL_ERROR_EDEADLK);
Require(scePthreadMutexUnlock(&mutex) == SCE_OK);
Require(scePthreadMutexDestroy(&mutex) == SCE_OK);
Require(scePthreadMutexattrInit(&attr) == SCE_OK);
Require(scePthreadMutexattrSettype(&attr, MUTEX_TYPE_ADAPTIVE) == SCE_OK);
PthreadMutex adaptive = nullptr;
Require(scePthreadMutexInit(&adaptive, &attr, nullptr) == SCE_OK);
Require(scePthreadMutexattrDestroy(&attr) == SCE_OK);
Require(scePthreadMutexLock(&adaptive) == SCE_OK);
Require(scePthreadMutexLock(&adaptive) == SCE_KERNEL_ERROR_EDEADLK);
Require(scePthreadMutexUnlock(&adaptive) == SCE_OK);
Require(scePthreadMutexDestroy(&adaptive) == SCE_OK);
Require(scePthreadMutexattrInit(&attr) == SCE_OK);
PthreadMutex defaulted = nullptr;
Require(scePthreadMutexInit(&defaulted, &attr, nullptr) == SCE_OK);
Require(scePthreadMutexattrDestroy(&attr) == SCE_OK);
Require(scePthreadMutexLock(&defaulted) == SCE_OK);
Require(scePthreadMutexLock(&defaulted) == SCE_KERNEL_ERROR_EDEADLK);
Require(scePthreadMutexUnlock(&defaulted) == SCE_OK);
Require(scePthreadMutexDestroy(&defaulted) == SCE_OK);
PthreadMutex unattributed = nullptr;
Require(scePthreadMutexInit(&unattributed, nullptr, nullptr) == SCE_OK);
Require(scePthreadMutexLock(&unattributed) == SCE_OK);
Require(scePthreadMutexLock(&unattributed) == SCE_KERNEL_ERROR_EDEADLK);
Require(scePthreadMutexUnlock(&unattributed) == SCE_OK);
Require(scePthreadMutexDestroy(&unattributed) == SCE_OK);
}