From 55e1c4a23872c0cbb861a841db5efe6ce62fd658 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Sat, 26 Sep 2026 17:07:34 +0300 Subject: [PATCH] feat(libkernel): implement application heap and thread atexit registration --- core/libs/SceTypes.hpp | 6 +- .../prx/libkernel/Pthread/Posix/Thread.cpp | 4 +- .../Pthread/include/ThreadLifecycle.hpp | 15 +++++ .../libs/prx/libkernel/Pthread/src/Thread.cpp | 56 +++++++++++++++---- core/libs/prx/libkernel/System/src/Rtld.cpp | 32 ++++------- 5 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 core/libs/prx/libkernel/Pthread/include/ThreadLifecycle.hpp diff --git a/core/libs/SceTypes.hpp b/core/libs/SceTypes.hpp index 3b149bce3..51b0a9caf 100644 --- a/core/libs/SceTypes.hpp +++ b/core/libs/SceTypes.hpp @@ -173,9 +173,9 @@ using PthreadCondattr = PthreadCondattrPrivate*; using PthreadKey = int; using pthread_entry_func_t = void* (*)(void*); using pthread_key_destructor_func_t = void (*)(void*); -using thread_dtors_func_t = void (*)(); -using get_thread_atexit_count_func_t = int (*)(KernelModule); -using thread_atexit_report_func_t = void (*)(KernelModule); +using thread_dtors_func_t = void (APS5_VABI *)(); +using get_thread_atexit_count_func_t = int (APS5_VABI *)(KernelModule); +using thread_atexit_report_func_t = void (APS5_VABI *)(KernelModule); struct FileStat { std::uint32_t st_dev; diff --git a/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp b/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp index da9083fc4..3b585b583 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Thread.cpp @@ -1,6 +1,7 @@ #include #include #include "SceTypes.hpp" +#include "../include/ThreadLifecycle.hpp" #include "prx/libc/include/General.hpp" @@ -32,8 +33,7 @@ int APS5_VABI pthread_detach_nid_postfix(Pthread thread) { } void APS5_VABI pthread_exit_nid_postfix(void* value) { - (void)value; - NotImplemented_nid_no_patch(__func__); + scePthreadExit(value); } int APS5_VABI pthread_getschedparam_nid_postfix(Pthread thread, int* policy, KernelSchedParam* param) { diff --git a/core/libs/prx/libkernel/Pthread/include/ThreadLifecycle.hpp b/core/libs/prx/libkernel/Pthread/include/ThreadLifecycle.hpp new file mode 100644 index 000000000..f342bb0ca --- /dev/null +++ b/core/libs/prx/libkernel/Pthread/include/ThreadLifecycle.hpp @@ -0,0 +1,15 @@ +#ifndef CORE_LIBS_PRX_LIBKERNEL_PTHREAD_THREADLIFECYCLE_HPP +#define CORE_LIBS_PRX_LIBKERNEL_PTHREAD_THREADLIFECYCLE_HPP + +#include "SceTypes.hpp" + +class ThreadLifecycle { +public: + static void SetThreadDtors(thread_dtors_func_t callback); + static void SetThreadAtexitCount(get_thread_atexit_count_func_t callback); + static void SetThreadAtexitReport(thread_atexit_report_func_t callback); +}; + +extern "C" void APS5_VABI scePthreadExit(void* retval); + +#endif diff --git a/core/libs/prx/libkernel/Pthread/src/Thread.cpp b/core/libs/prx/libkernel/Pthread/src/Thread.cpp index 7f4f857dd..6be2a0565 100644 --- a/core/libs/prx/libkernel/Pthread/src/Thread.cpp +++ b/core/libs/prx/libkernel/Pthread/src/Thread.cpp @@ -1,4 +1,5 @@ #include "../include/Pthread.hpp" +#include "../include/ThreadLifecycle.hpp" #include "prx/libc/include/General.hpp" #include #include @@ -29,7 +30,44 @@ struct ThreadArgs { PthreadPrivate* self; }; -static void FinishThread(PthreadPrivate* self, void* retval) { +static std::atomic threadDtors{nullptr}; +static std::atomic threadAtexitCount{nullptr}; +static std::atomic threadAtexitReport{nullptr}; +static thread_local PthreadPrivate* currentThread = nullptr; +static thread_local bool threadFinishing = false; + +void ThreadLifecycle::SetThreadDtors(thread_dtors_func_t callback) { + if (!callback) + throw std::runtime_error("Thread destructor callback is null"); + thread_dtors_func_t expected = nullptr; + if (!threadDtors.compare_exchange_strong(expected, callback)) + throw std::runtime_error("Thread destructor callback is already registered"); +} + +void ThreadLifecycle::SetThreadAtexitCount(get_thread_atexit_count_func_t callback) { + if (!callback) + throw std::runtime_error("Thread atexit count callback is null"); + get_thread_atexit_count_func_t expected = nullptr; + if (!threadAtexitCount.compare_exchange_strong(expected, callback)) + throw std::runtime_error("Thread atexit count callback is already registered"); +} + +void ThreadLifecycle::SetThreadAtexitReport(thread_atexit_report_func_t callback) { + if (!callback) + throw std::runtime_error("Thread atexit report callback is null"); + thread_atexit_report_func_t expected = nullptr; + if (!threadAtexitReport.compare_exchange_strong(expected, callback)) + throw std::runtime_error("Thread atexit report callback is already registered"); +} + +static void finishThread(PthreadPrivate* self, void* retval) { + if (!self || self != currentThread) + throw std::runtime_error("Finishing an unregistered guest thread"); + if (threadFinishing) + throw std::runtime_error("Guest thread is already finishing"); + threadFinishing = true; + if (const auto callback = threadDtors.load()) + callback(); { std::unique_lock lk(self->_join_mtx); self->_retval = retval; @@ -43,13 +81,13 @@ static void RunThread(std::unique_ptr args) { const auto entry = args->entry; void* arg = args->arg; PthreadPrivate* self = args->self; + currentThread = self; args.reset(); - FinishThread(self, entry(arg)); + finishThread(self, entry(arg)); + currentThread = nullptr; } #ifdef _WIN32 -static thread_local PthreadPrivate* currentThread = nullptr; - static void ReleaseThread(PthreadPrivate* thread) { if (thread->references.fetch_sub(1, std::memory_order_acq_rel) != 1) return; @@ -183,26 +221,22 @@ int APS5_VABI scePthreadDetach(Pthread thread) { } void APS5_VABI scePthreadExit(void* retval) { -#ifdef _WIN32 if (!currentThread) throw std::runtime_error("scePthreadExit: current thread is not registered"); auto* self = currentThread; - FinishThread(self, retval); + finishThread(self, retval); currentThread = nullptr; +#ifdef _WIN32 ReleaseThread(self); _endthreadex(0); #else pthread_exit(retval); #endif - __builtin_unreachable(); + throw std::runtime_error("Native thread exit returned"); } Pthread APS5_VABI scePthreadSelf() { -#ifdef _WIN32 return currentThread; -#else - return nullptr; -#endif } void APS5_VABI scePthreadYield() { diff --git a/core/libs/prx/libkernel/System/src/Rtld.cpp b/core/libs/prx/libkernel/System/src/Rtld.cpp index 0d131700b..e47bf5b13 100644 --- a/core/libs/prx/libkernel/System/src/Rtld.cpp +++ b/core/libs/prx/libkernel/System/src/Rtld.cpp @@ -3,6 +3,7 @@ #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include "prx/libc/include/ApplicationHeap.hpp" +#include "prx/libkernel/Pthread/include/ThreadLifecycle.hpp" extern "C" { @@ -23,31 +24,23 @@ int APS5_VABI sceKernelRtldThreadAtexitIncrement(uint64_t* c) { } void APS5_VABI sceKernelSetThreadAtexitCount(get_thread_atexit_count_func_t func) { - (void)func; - NotImplemented_nid_no_patch(__func__); + ThreadLifecycle::SetThreadAtexitCount(func); } void APS5_VABI sceKernelSetThreadAtexitReport(thread_atexit_report_func_t func) { - (void)func; - NotImplemented_nid_no_patch(__func__); + ThreadLifecycle::SetThreadAtexitReport(func); } void APS5_VABI sceKernelSetThreadDtors(thread_dtors_func_t dtors) { - (void)dtors; - NotImplemented_nid_no_patch(__func__); + ThreadLifecycle::SetThreadDtors(dtors); } } -using ThreadAtexitCount = int (APS5_VABI *)(KernelModule module); -using ThreadAtexitReport = void (APS5_VABI *)(KernelModule module); -using ThreadDtors = void (APS5_VABI *)(); - extern "C" { void APS5_VABI _sceKernelRtldSetApplicationHeapAPI_nid_postfix(void* api[]) { - (void)api; - NotImplemented_nid_no_patch(__func__); + ApplicationHeapRegister_nid_no_patch(api); } int APS5_VABI _sceKernelRtldThreadAtexitDecrement_nid_postfix(std::uint64_t* counter) { @@ -62,19 +55,16 @@ int APS5_VABI _sceKernelRtldThreadAtexitIncrement_nid_postfix(std::uint64_t* cou return 0; } -void APS5_VABI _sceKernelSetThreadAtexitCount_nid_postfix(ThreadAtexitCount callback) { - (void)callback; - NotImplemented_nid_no_patch(__func__); +void APS5_VABI _sceKernelSetThreadAtexitCount_nid_postfix(get_thread_atexit_count_func_t callback) { + ThreadLifecycle::SetThreadAtexitCount(callback); } -void APS5_VABI _sceKernelSetThreadAtexitReport_nid_postfix(ThreadAtexitReport callback) { - (void)callback; - NotImplemented_nid_no_patch(__func__); +void APS5_VABI _sceKernelSetThreadAtexitReport_nid_postfix(thread_atexit_report_func_t callback) { + ThreadLifecycle::SetThreadAtexitReport(callback); } -void APS5_VABI _sceKernelSetThreadDtors_nid_postfix(ThreadDtors callback) { - (void)callback; - NotImplemented_nid_no_patch(__func__); +void APS5_VABI _sceKernelSetThreadDtors_nid_postfix(thread_dtors_func_t callback) { + ThreadLifecycle::SetThreadDtors(callback); } }