mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(kernel): implement guest once initialization
This commit is contained in:
@@ -225,6 +225,16 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_standard_streams PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
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:$<TARGET_FILE_DIR:libkernel>")
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <thread>
|
||||
|
||||
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<int> 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<std::thread, 16> 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);
|
||||
}
|
||||
Reference in New Issue
Block a user