diff --git a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp index b860a2262..3c202b17d 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp @@ -1,61 +1,52 @@ +#include "prx/libkernel/Pthread/include/Cond.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 condition variable error"); + return static_cast(error & 0xffffu); +} + +} extern "C" { int APS5_VABI pthread_cond_broadcast_nid_postfix(PthreadCond* cond) { - (void)cond; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondBroadcast(cond)); } int APS5_VABI pthread_cond_init_nid_postfix(PthreadCond* cond, const PthreadCondattr* attr) { - (void)cond; - (void)attr; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondInit(cond, attr, nullptr)); } int APS5_VABI pthread_cond_signal_nid_postfix(PthreadCond* cond) { - (void)cond; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondSignal(cond)); } int APS5_VABI pthread_cond_timedwait_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, const KernelTimespec* abstime) { - (void)cond; - (void)mutex; - (void)abstime; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(CondOperations::AbsoluteTimedwait(cond, mutex, abstime)); } int APS5_VABI pthread_cond_wait_nid_postfix(PthreadCond* cond, PthreadMutex* mutex) { - (void)cond; - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondWait(cond, mutex)); } int APS5_VABI pthread_condattr_destroy_nid_postfix(PthreadCondattr* attr) { - (void)attr; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondattrDestroy(attr)); } int APS5_VABI pthread_condattr_init_nid_postfix(PthreadCondattr* attr) { - (void)attr; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondattrInit(attr)); } int APS5_VABI pthread_condattr_setclock_nid_postfix(PthreadCondattr* attr, KernelClockid clock_id) { - (void)attr; - (void)clock_id; - NotImplemented_nid_no_patch(__func__); - return 0; + return toPosix(scePthreadCondattrSetclock(attr, clock_id)); } } diff --git a/core/libs/prx/libkernel/Pthread/include/Cond.hpp b/core/libs/prx/libkernel/Pthread/include/Cond.hpp new file mode 100644 index 000000000..936b95714 --- /dev/null +++ b/core/libs/prx/libkernel/Pthread/include/Cond.hpp @@ -0,0 +1,26 @@ +#ifndef CORE_LIBS_PRX_LIBKERNEL_PTHREAD_COND_HPP +#define CORE_LIBS_PRX_LIBKERNEL_PTHREAD_COND_HPP + +#include "SceTypes.hpp" + +class CondOperations { +public: + static int AbsoluteTimedwait(PthreadCond* cond, PthreadMutex* mutex, const KernelTimespec* abstime); +}; + +extern "C" { + +int APS5_VABI scePthreadCondattrInit(PthreadCondattr* attr); +int APS5_VABI scePthreadCondattrDestroy(PthreadCondattr* attr); +int APS5_VABI scePthreadCondattrSetclock(PthreadCondattr* attr, KernelClockid clockId); +int APS5_VABI scePthreadCondInit(PthreadCond* cond, const PthreadCondattr* attr, const char* name); +int APS5_VABI scePthreadCondDestroy(PthreadCond* cond); +int APS5_VABI scePthreadCondSignal(PthreadCond* cond); +int APS5_VABI scePthreadCondBroadcast(PthreadCond* cond); +int APS5_VABI scePthreadCondSignalto(PthreadCond* cond, Pthread thread); +int APS5_VABI scePthreadCondWait(PthreadCond* cond, PthreadMutex* mutex); +int APS5_VABI scePthreadCondTimedwait(PthreadCond* cond, PthreadMutex* mutex, KernelUseconds usec); + +} + +#endif diff --git a/core/libs/prx/libkernel/Pthread/src/Attr.cpp b/core/libs/prx/libkernel/Pthread/src/Attr.cpp index 7f71adb80..d32525564 100644 --- a/core/libs/prx/libkernel/Pthread/src/Attr.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Attr.cpp @@ -1,8 +1,10 @@ -#include "../include/Pthread.hpp" +#include "prx/libkernel/Pthread/include/Pthread.hpp" #include "prx/libc/include/General.hpp" #include +#include static constexpr int SCE_OK = 0; +static constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016; static constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000C; static constexpr std::size_t DEFAULT_STACK_SIZE = 1u << 20; @@ -18,7 +20,7 @@ static constexpr int SCHED_FIFO_PS5 = 1; extern "C" { int APS5_VABI scePthreadAttrInit(PthreadAttr* attr) { - if (!attr) throw std::runtime_error("scePthreadAttrInit: null attr"); + if (!attr) throw std::runtime_error(std::string(__func__) + ": null attr"); auto* p = new (std::nothrow) PthreadAttrPrivate{}; if (!p) return SCE_KERNEL_ERROR_ENOMEM; p->_stacksize = DEFAULT_STACK_SIZE; @@ -31,35 +33,35 @@ int APS5_VABI scePthreadAttrInit(PthreadAttr* attr) { } int APS5_VABI scePthreadAttrDestroy(PthreadAttr* attr) { - if (!attr || !*attr) throw std::runtime_error("scePthreadAttrDestroy: null attr"); + if (!attr || !*attr) throw std::runtime_error(std::string(__func__) + ": null attr"); delete *attr; *attr = nullptr; return SCE_OK; } int APS5_VABI scePthreadAttrSetdetachstate(PthreadAttr* attr, int detachstate) { - if (!attr || !*attr) throw std::runtime_error("scePthreadAttrSetdetachstate: null attr"); + if (!attr || !*attr) throw std::runtime_error(std::string(__func__) + ": null attr"); if (detachstate != DETACH_JOINABLE && detachstate != DETACH_DETACHED) - throw std::runtime_error("scePthreadAttrSetdetachstate: invalid state"); + throw std::runtime_error(std::string(__func__) + ": invalid state"); (*attr)->_detachstate = detachstate; return SCE_OK; } int APS5_VABI scePthreadAttrSetschedparam(PthreadAttr* attr, const KernelSchedParam* param) { if (!attr || !*attr || !param) - throw std::runtime_error("scePthreadAttrSetschedparam: null arg"); + throw std::runtime_error(std::string(__func__) + ": null arg"); (*attr)->_schedpriority = param->sched_priority; return SCE_OK; } int APS5_VABI scePthreadAttrSetstacksize(PthreadAttr* attr, std::size_t stacksize) { - if (!attr || !*attr) throw std::runtime_error("scePthreadAttrSetstacksize: null attr"); - if (stacksize < 16384) throw std::runtime_error("scePthreadAttrSetstacksize: too small"); + if (!attr || !*attr) throw std::runtime_error(std::string(__func__) + ": null attr"); + if (stacksize < 16384) throw std::runtime_error(std::string(__func__) + ": too small"); #ifdef _WIN32 SYSTEM_INFO system{}; GetSystemInfo(&system); if (stacksize % system.dwPageSize != 0 || stacksize > std::numeric_limits::max()) - throw std::runtime_error("scePthreadAttrSetstacksize: invalid Windows stack size"); + throw std::runtime_error(std::string(__func__) + ": invalid Windows stack size"); #endif (*attr)->_stacksize = stacksize; return SCE_OK; @@ -67,7 +69,7 @@ int APS5_VABI scePthreadAttrSetstacksize(PthreadAttr* attr, std::size_t stacksiz int APS5_VABI scePthreadAttrGetstack(const PthreadAttr* attr, void** stackaddr, std::size_t* stacksize) { if (!attr || !*attr || !stackaddr || !stacksize) - throw std::runtime_error("scePthreadAttrGetstack: null arg"); + throw std::runtime_error(std::string(__func__) + ": null arg"); *stackaddr = (*attr)->stackAddress; *stacksize = (*attr)->_stacksize; return SCE_OK; @@ -75,7 +77,7 @@ int APS5_VABI scePthreadAttrGetstack(const PthreadAttr* attr, void** stackaddr, int APS5_VABI scePthreadAttrGet(Pthread thread, PthreadAttr* attr) { if (!thread || !attr || !*attr) - throw std::runtime_error("scePthreadAttrGet: null arg"); + return SCE_KERNEL_ERROR_EINVAL; (*attr)->_stacksize = thread->stackSize; (*attr)->stackAddress = thread->stackAddress; (*attr)->_detachstate = thread->_detached ? DETACH_DETACHED : DETACH_JOINABLE; @@ -93,10 +95,9 @@ int APS5_VABI scePthreadAttrGetaffinity(const PthreadAttr* attr, KernelCpumask* } int APS5_VABI scePthreadAttrGetdetachstate(const PthreadAttr* attr, int* state) { - (void)attr; - (void)state; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr || !state) throw std::runtime_error(std::string(__func__) + ": null arg"); + *state = (*attr)->_detachstate; + return SCE_OK; } int APS5_VABI scePthreadAttrGetguardsize(const PthreadAttr* attr, size_t* guard_size) { @@ -107,10 +108,9 @@ int APS5_VABI scePthreadAttrGetguardsize(const PthreadAttr* attr, size_t* guard_ } int APS5_VABI scePthreadAttrGetschedparam(const PthreadAttr* attr, KernelSchedParam* param) { - (void)attr; - (void)param; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr || !param) throw std::runtime_error(std::string(__func__) + ": null arg"); + param->sched_priority = (*attr)->_schedpriority; + return SCE_OK; } int APS5_VABI scePthreadAttrGetsolosched(const PthreadAttr* attr, int* solosched) { @@ -121,17 +121,15 @@ int APS5_VABI scePthreadAttrGetsolosched(const PthreadAttr* attr, int* solosched } int APS5_VABI scePthreadAttrGetstackaddr(const PthreadAttr* attr, void** stack_addr) { - (void)attr; - (void)stack_addr; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr || !stack_addr) throw std::runtime_error(std::string(__func__) + ": null arg"); + *stack_addr = (*attr)->stackAddress; + return SCE_OK; } int APS5_VABI scePthreadAttrGetstacksize(const PthreadAttr* attr, size_t* stack_size) { - (void)attr; - (void)stack_size; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr || !stack_size) throw std::runtime_error(std::string(__func__) + ": null arg"); + *stack_size = (*attr)->_stacksize; + return SCE_OK; } int APS5_VABI scePthreadAttrSetaffinity(PthreadAttr* attr, KernelCpumask mask) { @@ -149,17 +147,15 @@ int APS5_VABI scePthreadAttrSetguardsize(PthreadAttr* attr, size_t guard_size) { } int APS5_VABI scePthreadAttrSetinheritsched(PthreadAttr* attr, int inherit_sched) { - (void)attr; - (void)inherit_sched; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr) throw std::runtime_error(std::string(__func__) + ": null attr"); + (*attr)->_inheritsched = inherit_sched; + return SCE_OK; } int APS5_VABI scePthreadAttrSetschedpolicy(PthreadAttr* attr, int policy) { - (void)attr; - (void)policy; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr) throw std::runtime_error(std::string(__func__) + ": null attr"); + (*attr)->_schedpolicy = policy; + return SCE_OK; } int APS5_VABI scePthreadAttrSetsolosched(PthreadAttr* attr, int solosched) { @@ -170,18 +166,17 @@ int APS5_VABI scePthreadAttrSetsolosched(PthreadAttr* attr, int solosched) { } int APS5_VABI scePthreadAttrSetstack(PthreadAttr* attr, void* addr, size_t size) { - (void)attr; - (void)addr; - (void)size; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr || !addr) throw std::runtime_error(std::string(__func__) + ": null arg"); + if (size < 16384) throw std::runtime_error(std::string(__func__) + ": too small"); + (*attr)->stackAddress = addr; + (*attr)->_stacksize = size; + return SCE_OK; } int APS5_VABI scePthreadAttrSetstackaddr(PthreadAttr* attr, void* addr) { - (void)attr; - (void)addr; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!attr || !*attr || !addr) throw std::runtime_error(std::string(__func__) + ": null arg"); + (*attr)->stackAddress = addr; + return SCE_OK; } } diff --git a/core/libs/prx/libkernel/Pthread/src/Cond.cpp b/core/libs/prx/libkernel/Pthread/src/Cond.cpp index 754a076f3..93c2902fe 100644 --- a/core/libs/prx/libkernel/Pthread/src/Cond.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Cond.cpp @@ -1,85 +1,153 @@ -#include "../include/Pthread.hpp" -#include "prx/libc/include/General.hpp" +#include "prx/libkernel/Pthread/include/Pthread.hpp" +#include "prx/libkernel/Pthread/include/Mutex.hpp" +#include "prx/libkernel/Pthread/include/Cond.hpp" #include +#include #include -static constexpr int SCE_OK = 0; -static constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000C; -static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = 0x80020062; +namespace { + +constexpr int sceTimedOut = static_cast(0x8002003cu); +std::mutex condInitializationMutex; + +PthreadCond destroyedCond() { + return reinterpret_cast(std::uintptr_t{2}); +} + +PthreadCond resolveCond(PthreadCond* cond) { + if (!cond) + throw std::invalid_argument("Condition variable pointer is null"); + std::lock_guard lock(condInitializationMutex); + if (*cond == destroyedCond()) + throw std::runtime_error("Condition variable has been destroyed"); + if (!*cond) + *cond = new PthreadCondPrivate(); + return *cond; +} + +PthreadMutex lockedMutex(PthreadMutex* mutex) { + if (!mutex || !*mutex) + throw std::invalid_argument("Mutex pointer is null"); + auto* current = *mutex; + if (current->_owner.load(std::memory_order_acquire) != std::this_thread::get_id()) + throw std::runtime_error("Condition wait mutex is not owned by the current thread"); + return current; +} + +int waitUntil(PthreadCond* cond, PthreadMutex* mutex, const std::chrono::system_clock::time_point* deadline) { + auto* c = resolveCond(cond); + auto* m = lockedMutex(mutex); + if (m->_type == MutexType::Recursive) { + std::unique_lock lock(m->_rmtx, std::adopt_lock); + const auto previousCount = m->_count; + m->_count = 0; + m->_owner.store(std::thread::id{}, std::memory_order_release); + const bool timedOut = deadline && c->_cv.wait_until(lock, *deadline) == std::cv_status::timeout; + if (!deadline) + c->_cv.wait(lock); + m->_owner.store(std::this_thread::get_id(), std::memory_order_release); + m->_count = previousCount; + lock.release(); + return timedOut ? sceTimedOut : 0; + } + std::unique_lock lock(m->_mtx, std::adopt_lock); + m->_owner.store(std::thread::id{}, std::memory_order_release); + const bool timedOut = deadline && c->_cv.wait_until(lock, *deadline) == std::cv_status::timeout; + if (!deadline) + c->_cv.wait(lock); + m->_owner.store(std::this_thread::get_id(), std::memory_order_release); + lock.release(); + return timedOut ? sceTimedOut : 0; +} + +std::chrono::system_clock::time_point toDeadline(const KernelTimespec* abstime) { + if (!abstime || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + throw std::invalid_argument("Invalid absolute condition variable timeout"); + const auto maximum = std::chrono::nanoseconds::max().count(); + if (abstime->tv_sec > (maximum - abstime->tv_nsec) / 1000000000) + throw std::overflow_error("Absolute condition variable 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 condition variable timeout exceeds the host clock range"); + return std::chrono::system_clock::time_point(std::chrono::duration_cast(duration)); +} + +} + +int CondOperations::AbsoluteTimedwait(PthreadCond* cond, PthreadMutex* mutex, const KernelTimespec* abstime) { + const auto deadline = toDeadline(abstime); + return waitUntil(cond, mutex, &deadline); +} extern "C" { int APS5_VABI scePthreadCondattrInit(PthreadCondattr* attr) { - if (!attr) throw std::runtime_error("scePthreadCondattrInit: null attr"); - auto* p = new (std::nothrow) PthreadCondattrPrivate{0}; - if (!p) return SCE_KERNEL_ERROR_ENOMEM; - *attr = p; - return SCE_OK; + if (!attr) + throw std::invalid_argument("Condition attribute pointer is null"); + *attr = new PthreadCondattrPrivate{0}; + return 0; } int APS5_VABI scePthreadCondattrDestroy(PthreadCondattr* attr) { - if (!attr || !*attr) throw std::runtime_error("scePthreadCondattrDestroy: null attr"); + if (!attr || !*attr) + throw std::invalid_argument("Condition attributes are not initialized"); delete *attr; *attr = nullptr; - return SCE_OK; + return 0; } -int APS5_VABI scePthreadCondInit(PthreadCond* cond, const PthreadCondattr*, const char*) { - if (!cond) throw std::runtime_error("scePthreadCondInit: null cond"); - auto* p = new (std::nothrow) PthreadCondPrivate{}; - if (!p) return SCE_KERNEL_ERROR_ENOMEM; - *cond = p; - return SCE_OK; +int APS5_VABI scePthreadCondattrSetclock(PthreadCondattr* attr, KernelClockid clockId) { + if (!attr || !*attr) + throw std::invalid_argument("Condition attributes are not initialized"); + (*attr)->_clockid = static_cast(clockId); + return 0; +} + +int APS5_VABI scePthreadCondInit(PthreadCond* cond, const PthreadCondattr* attr, const char*) { + if (!cond) + throw std::invalid_argument("Condition variable pointer is null"); + if (attr && !*attr) + throw std::invalid_argument("Condition attributes are not initialized"); + auto replacement = std::make_unique(); + std::lock_guard lock(condInitializationMutex); + *cond = replacement.release(); + return 0; } int APS5_VABI scePthreadCondDestroy(PthreadCond* cond) { - if (!cond || !*cond) throw std::runtime_error("scePthreadCondDestroy: null cond"); + if (!cond) + throw std::invalid_argument("Condition variable pointer is null"); + std::lock_guard lock(condInitializationMutex); + if (*cond == destroyedCond()) + throw std::runtime_error("Condition variable has already been destroyed"); delete *cond; - *cond = nullptr; - return SCE_OK; + *cond = destroyedCond(); + return 0; } int APS5_VABI scePthreadCondSignal(PthreadCond* cond) { - if (!cond || !*cond) throw std::runtime_error("scePthreadCondSignal: null cond"); - (*cond)->_cv.notify_one(); - return SCE_OK; + resolveCond(cond)->_cv.notify_one(); + return 0; } int APS5_VABI scePthreadCondBroadcast(PthreadCond* cond) { - if (!cond || !*cond) throw std::runtime_error("scePthreadCondBroadcast: null cond"); - (*cond)->_cv.notify_all(); - return SCE_OK; -} - -int APS5_VABI scePthreadCondTimedwait(PthreadCond* cond, PthreadMutex* mutex, unsigned int usec) { - if (!cond || !*cond || !mutex || !*mutex) - throw std::runtime_error("scePthreadCondTimedwait: null arg"); - auto* m = *mutex; - auto* c = *cond; - if (m->_type == MutexType::Recursive) { - std::unique_lock lk(m->_rmtx, std::adopt_lock); - auto res = c->_cv.wait_for(lk, std::chrono::microseconds(usec)); - lk.release(); - return res == std::cv_status::timeout ? SCE_KERNEL_ERROR_ETIMEDOUT : SCE_OK; - } - std::unique_lock lk(m->_mtx, std::adopt_lock); - auto res = c->_cv.wait_for(lk, std::chrono::microseconds(usec)); - lk.release(); - return res == std::cv_status::timeout ? SCE_KERNEL_ERROR_ETIMEDOUT : SCE_OK; + resolveCond(cond)->_cv.notify_all(); + return 0; } int APS5_VABI scePthreadCondSignalto(PthreadCond* cond, Pthread thread) { - (void)cond; - (void)thread; - NotImplemented_nid_no_patch(__func__); - return 0; + (void)thread; + resolveCond(cond)->_cv.notify_all(); + return 0; } int APS5_VABI scePthreadCondWait(PthreadCond* cond, PthreadMutex* mutex) { - (void)cond; - (void)mutex; - NotImplemented_nid_no_patch(__func__); - return 0; + return waitUntil(cond, mutex, nullptr); +} + +int APS5_VABI scePthreadCondTimedwait(PthreadCond* cond, PthreadMutex* mutex, KernelUseconds usec) { + const auto deadline = std::chrono::system_clock::now() + std::chrono::microseconds(usec); + return waitUntil(cond, mutex, &deadline); } }