From a709e220f2191427f6fc9866065a61a3cab9e7de Mon Sep 17 00:00:00 2001 From: boykopovar Date: Sat, 26 Sep 2026 17:43:44 +0300 Subject: [PATCH] feat(libkernel): implement posix mutex locking and timed waits --- .../prx/libkernel/Pthread/Posix/Mutex.cpp | 64 ++--- .../prx/libkernel/Pthread/include/Mutex.hpp | 26 ++ .../prx/libkernel/Pthread/include/Pthread.hpp | 2 +- core/libs/prx/libkernel/Pthread/src/Mutex.cpp | 223 +++++++++++------- 4 files changed, 190 insertions(+), 125 deletions(-) create mode 100644 core/libs/prx/libkernel/Pthread/include/Mutex.hpp diff --git a/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp b/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp index 1409b3450..5482859ba 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Mutex.cpp @@ -1,72 +1,60 @@ +#include "../include/Mutex.hpp" #include -#include -#include "SceTypes.hpp" -#include "prx/libc/include/General.hpp" +#include + +namespace { + +int toPosix(int result) { + if (result == 0) + return 0; + const auto error = static_cast(result); + if ((error & 0xffff0000u) != 0x80020000u) + throw std::runtime_error("Unexpected SCE mutex error"); + return static_cast(error & 0xffffu); +} + +} extern "C" { int APS5_VABI pthread_mutex_destroy_nid_postfix(PthreadMutex* mutex) { - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexDestroy(mutex)); } int APS5_VABI pthread_mutex_init_nid_postfix(PthreadMutex* mutex, const PthreadMutexattr* attr) { - (void)mutex; - (void)attr; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexInit(mutex, attr, nullptr)); } int APS5_VABI pthread_mutex_lock_nid_postfix(PthreadMutex* mutex) { - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexLock(mutex)); } int APS5_VABI pthread_mutex_timedlock_nid_postfix(PthreadMutex* mutex, const KernelTimespec* abstime) { - (void)mutex; - (void)abstime; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(MutexOperations::Timedlock(mutex, abstime)); } int APS5_VABI pthread_mutex_trylock_nid_postfix(PthreadMutex* mutex) { - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexTrylock(mutex)); } int APS5_VABI pthread_mutex_unlock_nid_postfix(PthreadMutex* mutex) { - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexUnlock(mutex)); } int APS5_VABI pthread_mutexattr_destroy_nid_postfix(PthreadMutexattr* attr) { - (void)attr; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexattrDestroy(attr)); } int APS5_VABI pthread_mutexattr_init_nid_postfix(PthreadMutexattr* attr) { - (void)attr; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexattrInit(attr)); } int APS5_VABI pthread_mutexattr_setprotocol_nid_postfix(PthreadMutexattr* attr, int protocol) { - (void)attr; - (void)protocol; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexattrSetprotocol(attr, protocol)); } int APS5_VABI pthread_mutexattr_settype_nid_postfix(PthreadMutexattr* attr, int type) { - (void)attr; - (void)type; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadMutexattrSettype(attr, type)); } -} +} \ No newline at end of file diff --git a/core/libs/prx/libkernel/Pthread/include/Mutex.hpp b/core/libs/prx/libkernel/Pthread/include/Mutex.hpp new file mode 100644 index 000000000..ca734c603 --- /dev/null +++ b/core/libs/prx/libkernel/Pthread/include/Mutex.hpp @@ -0,0 +1,26 @@ +#ifndef CORE_LIBS_PRX_LIBKERNEL_PTHREAD_MUTEX_HPP +#define CORE_LIBS_PRX_LIBKERNEL_PTHREAD_MUTEX_HPP + +#include "SceTypes.hpp" + +class MutexOperations { +public: + static int Timedlock(PthreadMutex* mutex, const KernelTimespec* abstime); +}; + +extern "C" { + +int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr); +int APS5_VABI scePthreadMutexattrDestroy(PthreadMutexattr* attr); +int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type); +int APS5_VABI scePthreadMutexattrSetprotocol(PthreadMutexattr* attr, int protocol); +int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* attr, const char* name); +int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex); +int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex); +int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex); +int APS5_VABI scePthreadMutexTimedlock(PthreadMutex* mutex, KernelUseconds usec); +int APS5_VABI scePthreadMutexTrylock(PthreadMutex* mutex); + +} + +#endif diff --git a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp index e0adf92b8..b9b503a1b 100644 --- a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp +++ b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp @@ -28,7 +28,7 @@ struct PthreadMutexPrivate { std::atomic _owner; int _count; - PthreadMutexPrivate() : _type(MutexType::Normal), _count(0) {} + PthreadMutexPrivate() : _type(MutexType::Normal), _owner(std::thread::id{}), _count(0) {} }; struct PthreadCondattrPrivate { diff --git a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp index cc9ac1881..ccf895382 100644 --- a/core/libs/prx/libkernel/Pthread/src/Mutex.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Mutex.cpp @@ -1,113 +1,164 @@ #include "../include/Pthread.hpp" -#include "prx/libc/include/General.hpp" -#include +#include "../include/Mutex.hpp" #include +#include +#include #include -static constexpr int SCE_OK = 0; -static constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000C; -static constexpr int SCE_KERNEL_ERROR_EDEADLK = 0x80020023; -static constexpr int SCE_KERNEL_ERROR_EPERM = 0x80020001; +namespace { + +constexpr int sceBusy = static_cast(0x80020010u); +constexpr int sceTimedOut = static_cast(0x8002003cu); +std::mutex initializationMutex; + +PthreadMutex destroyedMutex() { + return reinterpret_cast(std::uintptr_t{2}); +} + +PthreadMutex resolveMutex(PthreadMutex* mutex, bool initialize) { + if (!mutex) + throw std::invalid_argument("Mutex pointer is null"); + std::lock_guard lock(initializationMutex); + if (*mutex == destroyedMutex()) + throw std::runtime_error("Mutex has been destroyed"); + if (reinterpret_cast(*mutex) == 1) + throw std::runtime_error("Adaptive mutex initializer is unsupported"); + if (!*mutex) { + if (!initialize) + throw std::runtime_error("Mutex is not initialized"); + *mutex = new PthreadMutexPrivate(); + } + return *mutex; +} + +template +int acquireMutex(PthreadMutex mutex, TAcquire acquire, int unavailable, bool tryOnly) { + const auto thread = std::this_thread::get_id(); + const bool owned = mutex->_owner.load(std::memory_order_acquire) == thread; + if (owned && mutex->_type != MutexType::Recursive) { + if (tryOnly) + return sceBusy; + throw std::runtime_error("Mutex is already owned by the current thread"); + } + if (owned && mutex->_count == std::numeric_limits::max()) + throw std::overflow_error("Recursive mutex lock count overflow"); + if (mutex->_type == MutexType::Recursive) { + if (!acquire(mutex->_rmtx)) + return unavailable; + ++mutex->_count; + } else { + if (!acquire(mutex->_mtx)) + return unavailable; + } + mutex->_owner.store(thread, std::memory_order_release); + return 0; +} + +} + +int MutexOperations::Timedlock(PthreadMutex* mutex, const KernelTimespec* abstime) { + if (!abstime || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + throw std::invalid_argument("Invalid absolute mutex timeout"); + const auto maximum = std::chrono::nanoseconds::max().count(); + if (abstime->tv_sec > (maximum - abstime->tv_nsec) / 1000000000) + throw std::overflow_error("Absolute mutex timeout exceeds the host clock range"); + const auto duration = std::chrono::nanoseconds(abstime->tv_sec * 1000000000 + abstime->tv_nsec); + if (std::chrono::duration(duration) >= std::chrono::duration(std::chrono::system_clock::duration::max())) + throw std::overflow_error("Absolute mutex timeout exceeds the host clock range"); + const auto deadline = std::chrono::system_clock::time_point(std::chrono::duration_cast(duration)); + return acquireMutex(resolveMutex(mutex, true), [&](auto& native) { return native.try_lock_until(deadline); }, sceTimedOut, false); +} extern "C" { int APS5_VABI scePthreadMutexattrInit(PthreadMutexattr* attr) { - if (!attr) throw std::runtime_error("scePthreadMutexattrInit: null attr"); - auto* p = new (std::nothrow) PthreadMutexattrPrivate{MutexType::Normal}; - if (!p) return SCE_KERNEL_ERROR_ENOMEM; - *attr = p; - return SCE_OK; + if (!attr) + throw std::invalid_argument("Mutex attribute pointer is null"); + *attr = new PthreadMutexattrPrivate{MutexType::Normal}; + return 0; } int APS5_VABI scePthreadMutexattrDestroy(PthreadMutexattr* attr) { - if (!attr || !*attr) throw std::runtime_error("scePthreadMutexattrDestroy: null attr"); + if (!attr || !*attr) + throw std::invalid_argument("Mutex attributes are not initialized"); delete *attr; *attr = nullptr; - return SCE_OK; + return 0; } int APS5_VABI scePthreadMutexattrSettype(PthreadMutexattr* attr, int type) { - if (!attr || !*attr) throw std::runtime_error("scePthreadMutexattrSettype: null attr"); + if (!attr || !*attr) + throw std::invalid_argument("Mutex attributes are not initialized"); switch (type) { case 1: (*attr)->type = MutexType::ErrorCheck; break; case 2: (*attr)->type = MutexType::Recursive; break; case 3: (*attr)->type = MutexType::Normal; break; - default: throw std::runtime_error("scePthreadMutexattrSettype: invalid type"); + default: throw std::invalid_argument("Invalid mutex type"); } - return SCE_OK; -} - -int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* attr, const char*) { - if (!mutex) throw std::runtime_error("scePthreadMutexInit: null mutex"); - MutexType t = MutexType::Normal; - if (attr && *attr) t = (*attr)->type; - auto* p = new (std::nothrow) PthreadMutexPrivate(); - if (!p) return SCE_KERNEL_ERROR_ENOMEM; - p->_type = t; - *mutex = p; - return SCE_OK; -} - -int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex) { - if (!mutex || !*mutex) throw std::runtime_error("scePthreadMutexDestroy: null mutex"); - delete *mutex; - *mutex = nullptr; - return SCE_OK; -} - -int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex) { - if (!mutex || !*mutex) throw std::runtime_error("scePthreadMutexLock: null mutex"); - auto* m = *mutex; - const auto tid = std::this_thread::get_id(); - if (m->_type == MutexType::Recursive) { - m->_rmtx.lock(); - m->_owner.store(tid, std::memory_order_relaxed); - ++m->_count; - return SCE_OK; - } - if (m->_type == MutexType::ErrorCheck) { - if (m->_owner.load(std::memory_order_acquire) == tid) return SCE_KERNEL_ERROR_EDEADLK; - } - m->_mtx.lock(); - m->_owner.store(tid, std::memory_order_relaxed); - return SCE_OK; -} - -int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex) { - if (!mutex || !*mutex) throw std::runtime_error("scePthreadMutexUnlock: null mutex"); - auto* m = *mutex; - if (m->_type == MutexType::ErrorCheck || m->_type == MutexType::Normal) { - if (m->_owner.load(std::memory_order_acquire) != std::this_thread::get_id()) - return SCE_KERNEL_ERROR_EPERM; - } - if (m->_type == MutexType::Recursive) { - if (--m->_count == 0) m->_owner.store(std::thread::id{}, std::memory_order_relaxed); - m->_rmtx.unlock(); - return SCE_OK; - } - m->_owner.store(std::thread::id{}, std::memory_order_relaxed); - m->_mtx.unlock(); - return SCE_OK; -} - -int APS5_VABI scePthreadMutexTimedlock(PthreadMutex* mutex, KernelUseconds usec) { - (void)mutex; - (void)usec; - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI scePthreadMutexTrylock(PthreadMutex* mutex) { - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return 0; } int APS5_VABI scePthreadMutexattrSetprotocol(PthreadMutexattr* attr, int protocol) { - (void)attr; - (void)protocol; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr) + throw std::invalid_argument("Mutex attributes are not initialized"); + if (protocol != 0) + throw std::invalid_argument("Mutex priority inheritance and protection are unsupported"); + return 0; +} + +int APS5_VABI scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* attr, const char*) { + if (!mutex) + throw std::invalid_argument("Mutex pointer is null"); + if (attr && !*attr) + throw std::invalid_argument("Mutex attributes are not initialized"); + auto replacement = std::make_unique(); + if (attr) + replacement->_type = (*attr)->type; + std::lock_guard lock(initializationMutex); + *mutex = replacement.release(); + return 0; +} + +int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex) { + if (!mutex) + throw std::invalid_argument("Mutex pointer is null"); + std::lock_guard lock(initializationMutex); + if (*mutex == destroyedMutex()) + throw std::runtime_error("Mutex has already been destroyed"); + if (reinterpret_cast(*mutex) == 1) + throw std::runtime_error("Adaptive mutex initializer is unsupported"); + if (*mutex && (*mutex)->_owner.load(std::memory_order_acquire) != std::thread::id{}) + throw std::runtime_error("Cannot destroy a locked mutex"); + delete *mutex; + *mutex = destroyedMutex(); + return 0; +} + +int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex) { + return acquireMutex(resolveMutex(mutex, true), [](auto& native) { native.lock(); return true; }, 0, false); +} + +int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex) { + auto* current = resolveMutex(mutex, false); + if (current->_owner.load(std::memory_order_acquire) != std::this_thread::get_id()) + throw std::runtime_error("Cannot unlock a mutex owned by another thread"); + if (current->_type == MutexType::Recursive) { + if (--current->_count == 0) + current->_owner.store(std::thread::id{}, std::memory_order_release); + current->_rmtx.unlock(); + } else { + current->_owner.store(std::thread::id{}, std::memory_order_release); + current->_mtx.unlock(); + } + return 0; +} + +int APS5_VABI scePthreadMutexTimedlock(PthreadMutex* mutex, KernelUseconds usec) { + return acquireMutex(resolveMutex(mutex, true), [=](auto& native) { return native.try_lock_for(std::chrono::microseconds(usec)); }, sceTimedOut, false); +} + +int APS5_VABI scePthreadMutexTrylock(PthreadMutex* mutex) { + return acquireMutex(resolveMutex(mutex, true), [](auto& native) { return native.try_lock(); }, sceBusy, true); } }