From 8776eba8c89ae65aac3c0a7d0787a8843d216edd Mon Sep 17 00:00:00 2001 From: Vladislav Mikhalin Date: Sun, 13 Oct 2024 12:22:14 +0300 Subject: [PATCH] Added adaptive mutex initializer handling (#1353) --- .../libraries/kernel/thread_management.cpp | 19 ++++++++++++------- src/core/libraries/kernel/thread_management.h | 9 +++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/core/libraries/kernel/thread_management.cpp b/src/core/libraries/kernel/thread_management.cpp index aa53d757..39c0eaf8 100644 --- a/src/core/libraries/kernel/thread_management.cpp +++ b/src/core/libraries/kernel/thread_management.cpp @@ -36,6 +36,10 @@ void init_pthreads() { ScePthreadMutexattr default_mutexattr = nullptr; scePthreadMutexattrInit(&default_mutexattr); g_pthread_cxt->setDefaultMutexattr(default_mutexattr); + ScePthreadMutexattr adaptive_mutexattr = nullptr; + scePthreadMutexattrInit(&adaptive_mutexattr); + scePthreadMutexattrSettype(&adaptive_mutexattr, ORBIS_PTHREAD_MUTEX_ADAPTIVE); + g_pthread_cxt->setAdaptiveMutexattr(adaptive_mutexattr); // default cond init ScePthreadCondattr default_condattr = nullptr; scePthreadCondattrInit(&default_condattr); @@ -412,7 +416,8 @@ int PS4_SYSV_ABI scePthreadGetaffinity(ScePthread thread, /*SceKernelCpumask*/ u } ScePthreadMutex* createMutex(ScePthreadMutex* addr) { - if (addr == nullptr || *addr != nullptr) { + if (addr == nullptr || + (*addr != nullptr && *addr != ORBIS_PTHREAD_MUTEX_ADAPTIVE_INITIALIZER)) { return addr; } @@ -429,14 +434,14 @@ int PS4_SYSV_ABI scePthreadMutexInit(ScePthreadMutex* mutex, const ScePthreadMut if (mutex == nullptr) { return SCE_KERNEL_ERROR_EINVAL; } - if (mutex_attr == nullptr) { - attr = g_pthread_cxt->getDefaultMutexattr(); - } else { - if (*mutex_attr == nullptr) { - attr = g_pthread_cxt->getDefaultMutexattr(); + if (mutex_attr == nullptr || *mutex_attr == nullptr) { + if (*mutex == ORBIS_PTHREAD_MUTEX_ADAPTIVE_INITIALIZER) { + attr = g_pthread_cxt->getAdaptiveMutexattr(); } else { - attr = mutex_attr; + attr = g_pthread_cxt->getDefaultMutexattr(); } + } else { + attr = mutex_attr; } *mutex = new PthreadMutexInternal{}; diff --git a/src/core/libraries/kernel/thread_management.h b/src/core/libraries/kernel/thread_management.h index 7385b55c..3cdb300f 100644 --- a/src/core/libraries/kernel/thread_management.h +++ b/src/core/libraries/kernel/thread_management.h @@ -13,6 +13,8 @@ #include "common/types.h" +#define ORBIS_PTHREAD_MUTEX_ADAPTIVE_INITIALIZER (reinterpret_cast(1)) + namespace Core::Loader { class SymbolsResolver; } @@ -134,6 +136,12 @@ public: void setDefaultMutexattr(ScePthreadMutexattr attr) { m_default_mutexattr = attr; } + ScePthreadMutexattr* getAdaptiveMutexattr() { + return &m_adaptive_mutexattr; + } + void setAdaptiveMutexattr(ScePthreadMutexattr attr) { + m_adaptive_mutexattr = attr; + } ScePthreadCondattr* getDefaultCondattr() { return &m_default_condattr; } @@ -161,6 +169,7 @@ public: private: ScePthreadMutexattr m_default_mutexattr = nullptr; + ScePthreadMutexattr m_adaptive_mutexattr = nullptr; ScePthreadCondattr m_default_condattr = nullptr; ScePthreadAttr m_default_attr = nullptr; PThreadPool* m_pthread_pool = nullptr;