feat(libkernel): implement application heap and thread atexit registration

This commit is contained in:
boykopovar
2026-09-26 17:07:34 +03:00
parent 2e654e4f89
commit 55e1c4a238
5 changed files with 76 additions and 37 deletions
+3 -3
View File
@@ -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;
@@ -1,6 +1,7 @@
#include <cstdint>
#include <cstddef>
#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) {
@@ -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
+45 -11
View File
@@ -1,4 +1,5 @@
#include "../include/Pthread.hpp"
#include "../include/ThreadLifecycle.hpp"
#include "prx/libc/include/General.hpp"
#include <cstdlib>
#include <future>
@@ -29,7 +30,44 @@ struct ThreadArgs {
PthreadPrivate* self;
};
static void FinishThread(PthreadPrivate* self, void* retval) {
static std::atomic<thread_dtors_func_t> threadDtors{nullptr};
static std::atomic<get_thread_atexit_count_func_t> threadAtexitCount{nullptr};
static std::atomic<thread_atexit_report_func_t> 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<std::mutex> lk(self->_join_mtx);
self->_retval = retval;
@@ -43,13 +81,13 @@ static void RunThread(std::unique_ptr<ThreadArgs> 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() {
+11 -21
View File
@@ -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);
}
}