Skip to content
Closed
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
10 changes: 10 additions & 0 deletions core/libs/prx/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ if(WIN32)
endif()

if(BUILD_TESTING)
add_test_executable(guest_hardware_concurrency_tests ${LIBS_INCLUDE_DIR}/tests/GuestHardwareConcurrency.cpp)
target_include_directories(guest_hardware_concurrency_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_hardware_concurrency_tests PRIVATE libc)
configure_windows_unwind(guest_hardware_concurrency_tests)
add_test(NAME guest_hardware_concurrency COMMAND guest_hardware_concurrency_tests)
set_tests_properties(guest_hardware_concurrency PROPERTIES TIMEOUT 30)
if(WIN32)
set_tests_properties(guest_hardware_concurrency PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_time_conversion_tests ${LIBS_INCLUDE_DIR}/tests/GuestTimeConversion.cpp)
target_include_directories(guest_time_conversion_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_time_conversion_tests PRIVATE libc)
Expand Down
5 changes: 5 additions & 0 deletions core/libs/prx/libc/src/CxxAbiSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
#include <functional>
#include <mutex>
#include <memory>
#include <thread>

#include "prx/libc/include/General.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"

extern "C" {

unsigned int APS5_VABI _ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix() noexcept {
return std::thread::hardware_concurrency();
}

void* APS5_VABI __cxa_demangle_nid_postfix(const char* mangled, char* buf, std::size_t* len, int* status) {
if (mangled == nullptr || (buf != nullptr && len == nullptr)) {
if (status != nullptr) *status = -3;
Expand Down
38 changes: 38 additions & 0 deletions core/libs/tests/GuestHardwareConcurrency.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <array>
#include <cstdio>
#include <thread>
#include <type_traits>

extern "C" unsigned int APS5_VABI _ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix() noexcept;

static_assert(sizeof(unsigned int) == 4);
static_assert(std::is_same_v<decltype(_ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix()), unsigned int>);
static_assert(noexcept(_ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix()));

int main() {
const unsigned int expected = std::thread::hardware_concurrency();
const auto query = _ZNSt8__sce_v226_Thrd_hardware_concurrencyEv_nid_postfix;
const unsigned int mainResult = query();
if (mainResult != expected) {
std::fprintf(stderr, "guest processor hint %u differs from host hint %u\n", mainResult, expected);
return 1;
}
std::array<unsigned int, 2> results{};
std::array<bool, 2> consistent{true, true};
std::array<std::thread, 2> workers;
for (std::size_t index = 0; index < workers.size(); ++index)
workers[index] = std::thread([&, index] {
for (unsigned int iteration = 0; iteration < 128; ++iteration) {
results[index] = query();
if (results[index] != std::thread::hardware_concurrency()) consistent[index] = false;
}
});
for (auto& worker : workers) worker.join();
for (std::size_t index = 0; index < results.size(); ++index) {
if (!consistent[index] || results[index] != mainResult) {
std::fprintf(stderr, "guest processor hint differs across threads\n");
return 1;
}
}
}