From 064006b6178209a7abb68c2f5aa5fb399ed0f731 Mon Sep 17 00:00:00 2001 From: asimplestray <304690168+asimplestray@users.noreply.github.com> Date: Tue, 29 Sep 2026 22:13:07 +0000 Subject: [PATCH] feat(kernel): implement pthread semaphore lifecycle --- core/libs/CMakeLists.txt | 10 ++ core/libs/SceTypes.hpp | 2 + .../prx/libkernel/Pthread/include/Pthread.hpp | 8 ++ core/libs/prx/libkernel/Pthread/src/Sem.cpp | 115 +++++++++++++----- core/libs/tests/GuestPthreadSem.cpp | 66 ++++++++++ 5 files changed, 168 insertions(+), 33 deletions(-) create mode 100644 core/libs/tests/GuestPthreadSem.cpp diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index a3e7b1519..aceab328f 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -336,6 +336,16 @@ if(BUILD_TESTING) set_tests_properties(guest_cond_timedwait PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_executable(guest_pthread_sem_tests tests/GuestPthreadSem.cpp) + target_include_directories(guest_pthread_sem_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_pthread_sem_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_pthread_sem_tests) + add_test(NAME guest_pthread_sem COMMAND guest_pthread_sem_tests) + set_tests_properties(guest_pthread_sem PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_pthread_sem PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_executable(guest_signal_tests tests/GuestSignals.cpp) target_include_directories(guest_signal_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(guest_signal_tests PRIVATE libkernel libc) diff --git a/core/libs/SceTypes.hpp b/core/libs/SceTypes.hpp index 50971c093..a31b2c658 100644 --- a/core/libs/SceTypes.hpp +++ b/core/libs/SceTypes.hpp @@ -158,6 +158,7 @@ struct PthreadRwlockPrivate; struct PthreadRwlockattrPrivate; struct PthreadCondattrPrivate; struct PthreadCondPrivate; +struct PthreadSemPrivate; using KernelSema = KernelSemaPrivate*; using KernelEventFlag = KernelEventFlagPrivate*; @@ -170,6 +171,7 @@ using PthreadRwlock = PthreadRwlockPrivate*; using PthreadRwlockattr = PthreadRwlockattrPrivate*; using PthreadCond = PthreadCondPrivate*; using PthreadCondattr = PthreadCondattrPrivate*; +using PthreadSem = PthreadSemPrivate*; using PthreadKey = int; using pthread_entry_func_t = void* (*)(void*); using pthread_key_destructor_func_t = void (*)(void*); diff --git a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp index 7d3508525..c1d319502 100644 --- a/core/libs/prx/libkernel/Pthread/include/Pthread.hpp +++ b/core/libs/prx/libkernel/Pthread/include/Pthread.hpp @@ -51,6 +51,14 @@ struct PthreadCondPrivate { int _clockid = 0; }; +struct PthreadSemPrivate { + std::mutex _mutex; + TimedWait::Condition _cv; + int _count = 0; + + explicit PthreadSemPrivate(unsigned int value) : _count(static_cast(value)) {} +}; + static constexpr KernelCpumask DEFAULT_THREAD_AFFINITY = 0x1FFF; static constexpr int DEFAULT_THREAD_PRIORITY = 700; diff --git a/core/libs/prx/libkernel/Pthread/src/Sem.cpp b/core/libs/prx/libkernel/Pthread/src/Sem.cpp index 079711862..7a1d26c3f 100644 --- a/core/libs/prx/libkernel/Pthread/src/Sem.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Sem.cpp @@ -1,53 +1,102 @@ #include "../include/Pthread.hpp" #include "prx/libc/include/General.hpp" +#include "prx/libkernel/Time/include/TimedWait.hpp" +#include +#include +#include +#include + +namespace { + +constexpr int sceBusy = static_cast(0x80020010u); +constexpr int sceTimedOut = static_cast(0x8002003cu); +std::mutex semInitializationMutex; + +PthreadSem destroyedSem() { + return reinterpret_cast(std::uintptr_t{2}); +} + +PthreadSemPrivate* resolveSem(PthreadSem* sem) { + if (!sem) + throw std::invalid_argument("Semaphore pointer is null"); + std::atomic_ref slot(*sem); + const auto current = slot.load(std::memory_order_acquire); + if (!current || current == destroyedSem()) + throw std::runtime_error("Semaphore is not initialized or has been destroyed"); + return current; +} + +} extern "C" { -int APS5_VABI scePthreadSemDestroy(void* sem) { - (void)sem; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemInit(PthreadSem* sem, int flag, unsigned int value, const char* name) { + if (!sem) + throw std::invalid_argument("Semaphore pointer is null"); + (void)flag; + (void)name; + auto replacement = std::make_unique(value); + std::lock_guard lock(semInitializationMutex); + std::atomic_ref(*sem).store(replacement.release(), std::memory_order_release); + return 0; } -int APS5_VABI scePthreadSemGetvalue(void* sem, int* value) { - (void)sem; - (void)value; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemDestroy(PthreadSem* sem) { + if (!sem) + throw std::invalid_argument("Semaphore pointer is null"); + std::lock_guard lock(semInitializationMutex); + auto current = *sem; + if (!current || current == destroyedSem()) + throw std::runtime_error("Semaphore is not initialized or has been destroyed"); + *sem = destroyedSem(); + delete current; + return 0; } -int APS5_VABI scePthreadSemInit(void* sem, int flag, unsigned int value, const char* name) { - (void)sem; - (void)flag; - (void)value; - (void)name; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemPost(PthreadSem* sem) { + auto* current = resolveSem(sem); + { + std::lock_guard lock(current->_mutex); + ++current->_count; + } + current->_cv.NotifyOne(); + return 0; } -int APS5_VABI scePthreadSemPost(void* sem) { - (void)sem; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemWait(PthreadSem* sem) { + auto* current = resolveSem(sem); + std::unique_lock lock(current->_mutex); + current->_cv.Wait(lock, [&] { return current->_count > 0; }); + --current->_count; + return 0; } -int APS5_VABI scePthreadSemTimedwait(void* sem, KernelUseconds usec) { - (void)sem; - (void)usec; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemTrywait(PthreadSem* sem) { + auto* current = resolveSem(sem); + std::lock_guard lock(current->_mutex); + if (current->_count <= 0) + return sceBusy; + --current->_count; + return 0; } -int APS5_VABI scePthreadSemTrywait(void* sem) { - (void)sem; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemTimedwait(PthreadSem* sem, KernelUseconds usec) { + auto* current = resolveSem(sem); + std::unique_lock lock(current->_mutex); + const auto acquired = current->_cv.WaitUntil(lock, TimedWait::DeadlineNanos(usec), [&] { return current->_count > 0; }); + if (!acquired) + return sceTimedOut; + --current->_count; + return 0; } -int APS5_VABI scePthreadSemWait(void* sem) { - (void)sem; - NotImplemented_nid_no_patch(__func__); - return 0; +int APS5_VABI scePthreadSemGetvalue(PthreadSem* sem, int* value) { + auto* current = resolveSem(sem); + if (!value) + throw std::invalid_argument("Semaphore value pointer is null"); + std::lock_guard lock(current->_mutex); + *value = current->_count; + return 0; } } diff --git a/core/libs/tests/GuestPthreadSem.cpp b/core/libs/tests/GuestPthreadSem.cpp new file mode 100644 index 000000000..001fe44fe --- /dev/null +++ b/core/libs/tests/GuestPthreadSem.cpp @@ -0,0 +1,66 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include +#include +#include + +extern "C" { +int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name); +int APS5_VABI scePthreadJoin(Pthread thread, void** retval); +int APS5_VABI scePthreadSemInit(PthreadSem* sem, int flag, unsigned int value, const char* name); +int APS5_VABI scePthreadSemDestroy(PthreadSem* sem); +int APS5_VABI scePthreadSemPost(PthreadSem* sem); +int APS5_VABI scePthreadSemWait(PthreadSem* sem); +int APS5_VABI scePthreadSemTrywait(PthreadSem* sem); +int APS5_VABI scePthreadSemTimedwait(PthreadSem* sem, unsigned int usec); +int APS5_VABI scePthreadSemGetvalue(PthreadSem* sem, int* value); +} + +static constexpr int SCE_OK = 0; +static constexpr int SCE_KERNEL_ERROR_EBUSY = static_cast(0x80020010); +static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = static_cast(0x8002003C); + +static void Require(bool value) { if (!value) std::abort(); } + +struct Context { + PthreadSem sem = nullptr; +}; + +static void* APS5_VABI Poster(void* arg) { + auto& context = *static_cast(arg); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + Require(scePthreadSemPost(&context.sem) == SCE_OK); + Require(scePthreadSemPost(&context.sem) == SCE_OK); + return nullptr; +} + +int main() { + PthreadSem sem = nullptr; + Require(scePthreadSemInit(&sem, 0, 2, nullptr) == SCE_OK); + int value = -1; + Require(scePthreadSemGetvalue(&sem, &value) == SCE_OK && value == 2); + Require(scePthreadSemTrywait(&sem) == SCE_OK); + Require(scePthreadSemTrywait(&sem) == SCE_OK); + Require(scePthreadSemTrywait(&sem) == SCE_KERNEL_ERROR_EBUSY); + Require(scePthreadSemGetvalue(&sem, &value) == SCE_OK && value == 0); + Require(scePthreadSemTimedwait(&sem, 1000) == SCE_KERNEL_ERROR_ETIMEDOUT); + Require(scePthreadSemPost(&sem) == SCE_OK); + Require(scePthreadSemGetvalue(&sem, &value) == SCE_OK && value == 1); + + Context context; + Require(scePthreadSemInit(&context.sem, 0, 0, nullptr) == SCE_OK); + Pthread thread = nullptr; + Require(scePthreadCreate(&thread, nullptr, Poster, &context, nullptr) == SCE_OK); + Require(scePthreadSemTimedwait(&context.sem, 5000000) == SCE_OK); + Require(scePthreadSemWait(&context.sem) == SCE_OK); + Require(scePthreadJoin(thread, nullptr) == SCE_OK); + Require(scePthreadSemDestroy(&context.sem) == SCE_OK); + + bool rejected = false; + try { scePthreadSemWait(&context.sem); } + catch (const std::runtime_error&) { rejected = true; } + Require(rejected); + Require(scePthreadSemDestroy(&sem) == SCE_OK); +}