mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #168 from asimplestray/feat/kernel-psem
feat(kernel): implement pthread semaphore lifecycle
This commit is contained in:
@@ -371,6 +371,16 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(guest_cond_timedwait PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
endif()
|
||||
add_executable(guest_pthread_sem_tests tests/GuestPthreadSem.cpp)
|
||||
target_include_directories(guest_pthread_sem_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_pthread_sem_tests PRIVATE libkernel libc)
|
||||
configure_windows_unwind(guest_pthread_sem_tests)
|
||||
add_test(NAME guest_pthread_sem COMMAND guest_pthread_sem_tests)
|
||||
set_tests_properties(guest_pthread_sem PROPERTIES TIMEOUT 20)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_pthread_sem 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)
|
||||
|
||||
@@ -158,6 +158,7 @@ struct PthreadRwlockPrivate;
|
||||
struct PthreadRwlockattrPrivate;
|
||||
struct PthreadCondattrPrivate;
|
||||
struct PthreadCondPrivate;
|
||||
struct PthreadSemPrivate;
|
||||
|
||||
using KernelSema = KernelSemaPrivate*;
|
||||
using KernelEventFlag = KernelEventFlagPrivate*;
|
||||
@@ -170,6 +171,7 @@ using PthreadRwlock = PthreadRwlockPrivate*;
|
||||
using PthreadRwlockattr = PthreadRwlockattrPrivate*;
|
||||
using PthreadCond = PthreadCondPrivate*;
|
||||
using PthreadCondattr = PthreadCondattrPrivate*;
|
||||
using PthreadSem = PthreadSemPrivate*;
|
||||
using PthreadKey = int;
|
||||
using pthread_entry_func_t = void* (*)(void*);
|
||||
using pthread_key_destructor_func_t = void (*)(void*);
|
||||
|
||||
@@ -51,6 +51,14 @@ struct PthreadCondPrivate {
|
||||
int _clockid = 0;
|
||||
};
|
||||
|
||||
struct PthreadSemPrivate {
|
||||
std::mutex _mutex;
|
||||
TimedWait::Condition _cv;
|
||||
int _count = 0;
|
||||
|
||||
explicit PthreadSemPrivate(unsigned int value) : _count(static_cast<int>(value)) {}
|
||||
};
|
||||
|
||||
static constexpr KernelCpumask DEFAULT_THREAD_AFFINITY = 0x1FFF;
|
||||
static constexpr int DEFAULT_THREAD_PRIORITY = 700;
|
||||
|
||||
|
||||
@@ -1,53 +1,102 @@
|
||||
#include "../include/Pthread.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libkernel/Time/include/TimedWait.hpp"
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int sceBusy = static_cast<int>(0x80020010u);
|
||||
constexpr int sceTimedOut = static_cast<int>(0x8002003cu);
|
||||
std::mutex semInitializationMutex;
|
||||
|
||||
PthreadSem destroyedSem() {
|
||||
return reinterpret_cast<PthreadSem>(std::uintptr_t{2});
|
||||
}
|
||||
|
||||
PthreadSemPrivate* resolveSem(PthreadSem* sem) {
|
||||
if (!sem)
|
||||
throw std::invalid_argument("Semaphore pointer is null");
|
||||
std::atomic_ref<PthreadSem> slot(*sem);
|
||||
const auto current = slot.load(std::memory_order_acquire);
|
||||
if (!current || current == destroyedSem())
|
||||
throw std::runtime_error("Semaphore is not initialized or has been destroyed");
|
||||
return current;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int APS5_VABI scePthreadSemDestroy(void* sem) {
|
||||
(void)sem;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemInit(PthreadSem* sem, int flag, unsigned int value, const char* name) {
|
||||
if (!sem)
|
||||
throw std::invalid_argument("Semaphore pointer is null");
|
||||
(void)flag;
|
||||
(void)name;
|
||||
auto replacement = std::make_unique<PthreadSemPrivate>(value);
|
||||
std::lock_guard lock(semInitializationMutex);
|
||||
std::atomic_ref<PthreadSem>(*sem).store(replacement.release(), std::memory_order_release);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI scePthreadSemGetvalue(void* sem, int* value) {
|
||||
(void)sem;
|
||||
(void)value;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemDestroy(PthreadSem* sem) {
|
||||
if (!sem)
|
||||
throw std::invalid_argument("Semaphore pointer is null");
|
||||
std::lock_guard lock(semInitializationMutex);
|
||||
auto current = *sem;
|
||||
if (!current || current == destroyedSem())
|
||||
throw std::runtime_error("Semaphore is not initialized or has been destroyed");
|
||||
*sem = destroyedSem();
|
||||
delete current;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI scePthreadSemInit(void* sem, int flag, unsigned int value, const char* name) {
|
||||
(void)sem;
|
||||
(void)flag;
|
||||
(void)value;
|
||||
(void)name;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemPost(PthreadSem* sem) {
|
||||
auto* current = resolveSem(sem);
|
||||
{
|
||||
std::lock_guard lock(current->_mutex);
|
||||
++current->_count;
|
||||
}
|
||||
current->_cv.NotifyOne();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI scePthreadSemPost(void* sem) {
|
||||
(void)sem;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemWait(PthreadSem* sem) {
|
||||
auto* current = resolveSem(sem);
|
||||
std::unique_lock lock(current->_mutex);
|
||||
current->_cv.Wait(lock, [&] { return current->_count > 0; });
|
||||
--current->_count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI scePthreadSemTimedwait(void* sem, KernelUseconds usec) {
|
||||
(void)sem;
|
||||
(void)usec;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemTrywait(PthreadSem* sem) {
|
||||
auto* current = resolveSem(sem);
|
||||
std::lock_guard lock(current->_mutex);
|
||||
if (current->_count <= 0)
|
||||
return sceBusy;
|
||||
--current->_count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI scePthreadSemTrywait(void* sem) {
|
||||
(void)sem;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemTimedwait(PthreadSem* sem, KernelUseconds usec) {
|
||||
auto* current = resolveSem(sem);
|
||||
std::unique_lock lock(current->_mutex);
|
||||
const auto acquired = current->_cv.WaitUntil(lock, TimedWait::DeadlineNanos(usec), [&] { return current->_count > 0; });
|
||||
if (!acquired)
|
||||
return sceTimedOut;
|
||||
--current->_count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI scePthreadSemWait(void* sem) {
|
||||
(void)sem;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return 0;
|
||||
int APS5_VABI scePthreadSemGetvalue(PthreadSem* sem, int* value) {
|
||||
auto* current = resolveSem(sem);
|
||||
if (!value)
|
||||
throw std::invalid_argument("Semaphore value pointer is null");
|
||||
std::lock_guard lock(current->_mutex);
|
||||
*value = current->_count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name);
|
||||
int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
|
||||
int APS5_VABI scePthreadSemInit(PthreadSem* sem, int flag, unsigned int value, const char* name);
|
||||
int APS5_VABI scePthreadSemDestroy(PthreadSem* sem);
|
||||
int APS5_VABI scePthreadSemPost(PthreadSem* sem);
|
||||
int APS5_VABI scePthreadSemWait(PthreadSem* sem);
|
||||
int APS5_VABI scePthreadSemTrywait(PthreadSem* sem);
|
||||
int APS5_VABI scePthreadSemTimedwait(PthreadSem* sem, unsigned int usec);
|
||||
int APS5_VABI scePthreadSemGetvalue(PthreadSem* sem, int* value);
|
||||
}
|
||||
|
||||
static constexpr int SCE_OK = 0;
|
||||
static constexpr int SCE_KERNEL_ERROR_EBUSY = static_cast<int>(0x80020010);
|
||||
static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = static_cast<int>(0x8002003C);
|
||||
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
|
||||
struct Context {
|
||||
PthreadSem sem = nullptr;
|
||||
};
|
||||
|
||||
static void* APS5_VABI Poster(void* arg) {
|
||||
auto& context = *static_cast<Context*>(arg);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
Require(scePthreadSemPost(&context.sem) == SCE_OK);
|
||||
Require(scePthreadSemPost(&context.sem) == SCE_OK);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int main() {
|
||||
PthreadSem sem = nullptr;
|
||||
Require(scePthreadSemInit(&sem, 0, 2, nullptr) == SCE_OK);
|
||||
int value = -1;
|
||||
Require(scePthreadSemGetvalue(&sem, &value) == SCE_OK && value == 2);
|
||||
Require(scePthreadSemTrywait(&sem) == SCE_OK);
|
||||
Require(scePthreadSemTrywait(&sem) == SCE_OK);
|
||||
Require(scePthreadSemTrywait(&sem) == SCE_KERNEL_ERROR_EBUSY);
|
||||
Require(scePthreadSemGetvalue(&sem, &value) == SCE_OK && value == 0);
|
||||
Require(scePthreadSemTimedwait(&sem, 1000) == SCE_KERNEL_ERROR_ETIMEDOUT);
|
||||
Require(scePthreadSemPost(&sem) == SCE_OK);
|
||||
Require(scePthreadSemGetvalue(&sem, &value) == SCE_OK && value == 1);
|
||||
|
||||
Context context;
|
||||
Require(scePthreadSemInit(&context.sem, 0, 0, nullptr) == SCE_OK);
|
||||
Pthread thread = nullptr;
|
||||
Require(scePthreadCreate(&thread, nullptr, Poster, &context, nullptr) == SCE_OK);
|
||||
Require(scePthreadSemTimedwait(&context.sem, 5000000) == SCE_OK);
|
||||
Require(scePthreadSemWait(&context.sem) == SCE_OK);
|
||||
Require(scePthreadJoin(thread, nullptr) == SCE_OK);
|
||||
Require(scePthreadSemDestroy(&context.sem) == SCE_OK);
|
||||
|
||||
bool rejected = false;
|
||||
try { scePthreadSemWait(&context.sem); }
|
||||
catch (const std::runtime_error&) { rejected = true; }
|
||||
Require(rejected);
|
||||
Require(scePthreadSemDestroy(&sem) == SCE_OK);
|
||||
}
|
||||
Reference in New Issue
Block a user