From 322dc24c9eb378b52346d8437b8bc6bd699c91d7 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 25 Sep 2026 07:22:05 +1000 Subject: [PATCH] feat(kernel): implement guest once initialization --- core/libs/CMakeLists.txt | 10 +++++ core/libs/prx/libkernel/CMakeLists.txt | 1 + .../libs/prx/libkernel/Pthread/Posix/Once.cpp | 43 ++++++++++++++++++ core/libs/tests/GuestOnce.cpp | 44 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 core/libs/prx/libkernel/Pthread/Posix/Once.cpp create mode 100644 core/libs/tests/GuestOnce.cpp diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index d7d1421a1..20103697f 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -225,6 +225,16 @@ if(BUILD_TESTING) set_tests_properties(guest_standard_streams PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_executable(guest_once_tests tests/GuestOnce.cpp) + target_include_directories(guest_once_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_once_tests PRIVATE libkernel libc) + configure_windows_unwind(guest_once_tests) + add_test(NAME guest_once COMMAND guest_once_tests) + set_tests_properties(guest_once PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_once 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/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 08098fd1e..394bad513 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -22,6 +22,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Mutex.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Rwlock.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Thread.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Once.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Tsd.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Sem.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/Socket.cpp diff --git a/core/libs/prx/libkernel/Pthread/Posix/Once.cpp b/core/libs/prx/libkernel/Pthread/Posix/Once.cpp new file mode 100644 index 000000000..bd7d8a24f --- /dev/null +++ b/core/libs/prx/libkernel/Pthread/Posix/Once.cpp @@ -0,0 +1,43 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include + +namespace { +// FreeBSD guest ABI: PTHREAD_ONCE_INIT is {0, nullptr}; completed state is 1. +struct GuestOnce { + std::int32_t state; + void* mutex; +}; +static_assert(offsetof(GuestOnce, mutex) == 8 && sizeof(GuestOnce) == 16); +std::mutex onceMutex; +std::condition_variable onceChanged; +} + +extern "C" int APS5_VABI pthread_once_nid_postfix( + GuestOnce* control, void (APS5_VABI *initialize)()) { + if (!control || !initialize) return 22; + std::unique_lock lock(onceMutex); + onceChanged.wait(lock, [&] { return control->state != 2; }); + if (control->state == 1) return 0; + if (control->state != 0) return 22; + control->state = 2; + lock.unlock(); + // Initializers may initialize other once controls. Never run guest code + // under the shared lock. The lock publishes all initializer writes. + try { + initialize(); + } catch (...) { + lock.lock(); + control->state = 0; + lock.unlock(); + onceChanged.notify_all(); + throw; + } + lock.lock(); + control->state = 1; + lock.unlock(); + onceChanged.notify_all(); + return 0; +} diff --git a/core/libs/tests/GuestOnce.cpp b/core/libs/tests/GuestOnce.cpp new file mode 100644 index 000000000..b82ec22ef --- /dev/null +++ b/core/libs/tests/GuestOnce.cpp @@ -0,0 +1,44 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +#include +#include + +struct GuestOnce { std::int32_t state; void* mutex; }; +static_assert(sizeof(GuestOnce) == 16 && offsetof(GuestOnce, mutex) == 8); +using Initializer = void (APS5_VABI *)(); +extern "C" int APS5_VABI pthread_once_nid_postfix(GuestOnce*, Initializer); +static void Require(bool value) { if (!value) std::abort(); } +static GuestOnce control{}, nested{}, retry{}; +static std::atomic calls{0}, arrived{0}; +static int published = 0, nestedValue = 0, attempts = 0; +static void APS5_VABI Nested() { nestedValue = 17; } +static void APS5_VABI Initialize() { + ++calls; + while (arrived.load() != 16) std::this_thread::yield(); + Require(pthread_once_nid_postfix(&nested, Nested) == 0); + published = 42; +} +static void APS5_VABI Retry() { + if (++attempts == 1) throw 7; +} +int main() { + std::array workers; + for (auto& worker : workers) worker = std::thread([] { + ++arrived; + Require(pthread_once_nid_postfix(&control, Initialize) == 0); + Require(published == 42 && nestedValue == 17); + }); + for (auto& worker : workers) worker.join(); + Require(calls == 1 && control.state == 1 && control.mutex == nullptr); + Require(pthread_once_nid_postfix(&control, Initialize) == 0 && calls == 1); + try { pthread_once_nid_postfix(&retry, Retry); std::abort(); } + catch (int value) { Require(value == 7 && retry.state == 0); } + Require(pthread_once_nid_postfix(&retry, Retry) == 0 && attempts == 2); + Require(pthread_once_nid_postfix(nullptr, Initialize) == 22); + Require(pthread_once_nid_postfix(&control, nullptr) == 22); + GuestOnce invalid{9, nullptr}; + Require(pthread_once_nid_postfix(&invalid, Initialize) == 22); +}