Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/libs/prx/libc/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,9 @@ void APS5_VABI _ZNSt4_Pad7_LaunchEPKcPP12pthread_attrPP7pthread_nid_postfix(Gues
void APS5_VABI PadLaunchWithNameAttr_nid_no_patch(GuestPad* pad, const char* name, void** attrSlot, void** thread);
int APS5_VABI _Thrd_join_nid_postfix(void* thread, int* code);
int APS5_VABI _Thrd_detach_nid_postfix(void* thread);
void APS5_VABI _ZNSt3__16thread4joinEv_nid_postfix(Pthread* thread);
void APS5_VABI _ZNSt3__16thread6detachEv_nid_postfix(Pthread* thread);
void APS5_VABI _ZNSt3__16threadD1Ev_nid_postfix(Pthread* thread);
void* APS5_VABI _Thrd_id_nid_postfix(void);
void APS5_VABI _Call_once_nid_postfix(std::int32_t* flag, void (APS5_VABI* function)(void));
int APS5_VABI _Tss_create_nid_postfix(int* key, void (APS5_VABI* destructor)(void*));
Expand Down
33 changes: 33 additions & 0 deletions core/libs/prx/libc/src/Threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,39 @@ int APS5_VABI _Thrd_detach_nid_postfix(void* thread) {
return detach(thread) == 0 ? threadSuccess : threadError;
}

static Pthread CheckedLibcxxThread(Pthread* thread, const char* function) {
Pthread value = nullptr;
if (reinterpret_cast<std::uintptr_t>(thread) % alignof(Pthread) != 0 ||
!GuestWritable_nid_no_patch(thread, sizeof(Pthread)) ||
!GuestCopyIn_nid_no_patch(&value, thread, sizeof(value)))
throw std::invalid_argument(std::string(function) + ": invalid thread address");
return value;
}

void APS5_VABI _ZNSt3__16thread4joinEv_nid_postfix(Pthread* thread) {
const auto value = CheckedLibcxxThread(thread, __func__);
if (!value) throw std::system_error(EINVAL, std::generic_category(), "thread::join failed");
const auto join = registeredPthreadJoin.load(std::memory_order_acquire);
if (!join) throw std::runtime_error("thread::join: pthread_join is not registered");
const int error = join(value, nullptr);
if (error != 0) throw std::system_error(error, std::generic_category(), "thread::join failed");
*thread = nullptr;
}

void APS5_VABI _ZNSt3__16thread6detachEv_nid_postfix(Pthread* thread) {
const auto value = CheckedLibcxxThread(thread, __func__);
if (!value) throw std::system_error(EINVAL, std::generic_category(), "thread::detach failed");
const auto detach = registeredPthreadDetach.load(std::memory_order_acquire);
if (!detach) throw std::runtime_error("thread::detach: pthread_detach is not registered");
const int error = detach(value);
if (error != 0) throw std::system_error(error, std::generic_category(), "thread::detach failed");
*thread = nullptr;
}

void APS5_VABI _ZNSt3__16threadD1Ev_nid_postfix(Pthread* thread) {
if (CheckedLibcxxThread(thread, __func__)) std::terminate();
}

[[noreturn]] void APS5_VABI _ZSt16_Throw_Cpp_errori_nid_postfix(int code) {
ThrowCppError_nid_no_patch(code);
}
Expand Down
10 changes: 10 additions & 0 deletions core/libs/prx/libc/tests/cmake/guest_libcxx_thread.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_test_executable(guest_libcxx_thread_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibcxxThread.cpp)
target_include_directories(guest_libcxx_thread_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libcxx_thread_tests PRIVATE libc libkernel)
configure_windows_unwind(guest_libcxx_thread_tests)
add_test(NAME guest_libcxx_thread COMMAND guest_libcxx_thread_tests)
set_tests_properties(guest_libcxx_thread PROPERTIES TIMEOUT 15)
if(WIN32)
set_tests_properties(guest_libcxx_thread PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>;PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
87 changes: 87 additions & 0 deletions core/libs/tests/GuestLibcxxThread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "prx/libc/include/Api.hpp"
#include "prx/libkernel/include/Api.hpp"

#include <atomic>
#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <system_error>
#include <thread>

namespace {

void Require(bool condition, const char* message) {
if (!condition) {
std::fprintf(stderr, "%s\n", message);
std::exit(1);
}
}

struct WorkerState {
std::atomic<bool> release{false};
std::atomic<bool> finished{false};
};

void* APS5_VABI Worker(void* opaque) {
auto* state = static_cast<WorkerState*>(opaque);
while (!state->release.load()) std::this_thread::yield();
state->finished.store(true);
return nullptr;
}

void CheckEmpty() {
Pthread empty = nullptr;
for (const auto call : {_ZNSt3__16thread4joinEv_nid_postfix, _ZNSt3__16thread6detachEv_nid_postfix}) {
bool rejected = false;
try {
call(&empty);
} catch (const std::system_error& error) {
rejected = error.code().value() == EINVAL;
}
Require(rejected && empty == nullptr, "empty thread must report EINVAL");
}
_ZNSt3__16threadD1Ev_nid_postfix(&empty);
}

void CheckJoin() {
Pthread self = pthread_self_nid_postfix();
Require(self != nullptr, "find current guest thread");
bool selfJoinRejected = false;
try {
_ZNSt3__16thread4joinEv_nid_postfix(&self);
} catch (const std::system_error& error) {
selfJoinRejected = error.code().value() != 0;
}
Require(selfJoinRejected && self == pthread_self_nid_postfix(), "failed join retains thread handle");

WorkerState state;
Pthread thread = nullptr;
Require(pthread_create_nid_postfix(&thread, nullptr, Worker, &state) == 0, "create joinable thread");
state.release.store(true);
_ZNSt3__16thread4joinEv_nid_postfix(&thread);
Require(thread == nullptr && state.finished.load(), "join waits and clears thread handle");
_ZNSt3__16threadD1Ev_nid_postfix(&thread);
}

void CheckDetach() {
WorkerState state;
Pthread thread = nullptr;
Require(pthread_create_nid_postfix(&thread, nullptr, Worker, &state) == 0, "create detachable thread");
_ZNSt3__16thread6detachEv_nid_postfix(&thread);
Require(thread == nullptr, "detach clears thread handle");
state.release.store(true);
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
while (!state.finished.load() && std::chrono::steady_clock::now() < deadline)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
Require(state.finished.load(), "detached thread finishes");
_ZNSt3__16threadD1Ev_nid_postfix(&thread);
}

}

int main() {
CheckEmpty();
CheckJoin();
CheckDetach();
}
5 changes: 5 additions & 0 deletions docs/dev/debt/changes/feat-libc-libcxx-thread-lifecycle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libc

## add Functional

- [std::__1::thread join, detach and destructor](../../../core/libs/prx/libc/src/Threads.cpp) (libc) - provides the three imports listed for Azahar in RetroArch (PPSA99169, #5927). [LLVM libc++ 17](https://github.com/llvm/llvm-project/blob/llvmorg-17.0.6/libcxx/src/thread.cpp) joins or detaches the pthread handle, clears it only on success, and terminates if a thread is still joinable at destruction. The guest thread object is assumed to hold one pointer-sized pthread handle, as in the libc++ pthread backend. The console provider, exact firmware class layout, guest C++ exception ABI on error paths, process termination behavior on a joinable destructor, and title-level behavior have not been measured.
Loading