mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-22 16:43:05 -05:00
Add pthread_cond_reltimedwait_np (K953PF5u6Pc; computed with tools/nid_names.py and matching aerolib.csv) as scePthreadCondTimedwait with POSIX error numbers: ETIMEDOUT when the relative timeout expires, EPERM when the caller does not hold the mutex, and the mutex held again on return in every waiting case. Titles: Porpoise PPSA99764 and RetroArch PPSA99169 import it. Predicted from relink registry order, not observed: Porpoise stops with "FAIL: unresolved ELF import K953PF5u6Pc from libkernel.prx" (registry index 384; RetroArch index 490). No call site is resolved in the registry. The third argument is a relative timeout in microseconds, not a struct timespec. FreeBSD 11.4 libthr (8c639945a44588f6cfdddfb057bb5aad88735da0) has no such function. shadPS4 (3a9f57265d28975658f1ca941daa5c879c7a74bd, src/core/libraries/kernel/threads/condvar.cpp:229-234, 445, 467-468) registers posix_pthread_cond_reltimedwait_np(cond, mutex, u64 usec) for this NID and the same function for scePthreadCondTimedwait (BmMjYxmew1w), whose microsecond argument this repository already implements. The width follows the SCE twin's 32-bit SceKernelUseconds, as sem_reltimedwait_np does, not shadPS4's u64. There is no timespec to validate, so no EINVAL case. Recorded in TechnicalDebt. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> (cherry picked from commit c86ec8417d12f5b12136b0f06eae06493b0e7a62)
132 lines
5.5 KiB
C++
132 lines
5.5 KiB
C++
#include "SceTypes.hpp"
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
|
|
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 scePthreadMutexInit(PthreadMutex* mutex, const PthreadMutexattr* attr, const char* name);
|
|
int APS5_VABI scePthreadMutexDestroy(PthreadMutex* mutex);
|
|
int APS5_VABI scePthreadMutexLock(PthreadMutex* mutex);
|
|
int APS5_VABI scePthreadMutexUnlock(PthreadMutex* mutex);
|
|
int APS5_VABI scePthreadMutexTrylock(PthreadMutex* mutex);
|
|
int APS5_VABI scePthreadCondInit(PthreadCond* cond, const PthreadCondattr* attr, const char* name);
|
|
int APS5_VABI scePthreadCondDestroy(PthreadCond* cond);
|
|
int APS5_VABI scePthreadCondTimedwait(PthreadCond* cond, PthreadMutex* mutex, unsigned int usec);
|
|
int APS5_VABI scePthreadCondSignal(PthreadCond* cond);
|
|
int APS5_VABI pthread_cond_reltimedwait_np_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, unsigned int usec);
|
|
int APS5_VABI __cxa_atexit_nid_postfix(void (APS5_VABI *)(void*), void*, void*);
|
|
void APS5_VABI __pthread_cxa_finalize_nid_postfix(void*);
|
|
unsigned int APS5_VABI sceKernelSleep(unsigned int seconds);
|
|
}
|
|
|
|
static int cleanupAt = -1;
|
|
static void APS5_VABI Cleanup(void*) { cleanupAt = 0; }
|
|
|
|
static constexpr int SCE_OK = 0;
|
|
static constexpr int SCE_KERNEL_ERROR_ETIMEDOUT = static_cast<int>(0x8002003C);
|
|
static constexpr int SCE_KERNEL_ERROR_EBUSY = static_cast<int>(0x80020010);
|
|
static constexpr int POSIX_EPERM = 1;
|
|
static constexpr int POSIX_ETIMEDOUT = 60;
|
|
|
|
static void Require(bool value) { if (!value) std::abort(); }
|
|
|
|
struct Context {
|
|
PthreadMutex mutex = nullptr;
|
|
std::atomic<bool> acquired{false};
|
|
};
|
|
|
|
static void* APS5_VABI Contender(void* arg) {
|
|
auto& context = *static_cast<Context*>(arg);
|
|
Require(scePthreadMutexLock(&context.mutex) == SCE_OK);
|
|
context.acquired.store(true);
|
|
Require(scePthreadMutexUnlock(&context.mutex) == SCE_OK);
|
|
return nullptr;
|
|
}
|
|
|
|
struct Signalled {
|
|
PthreadMutex mutex = nullptr;
|
|
PthreadCond cond = nullptr;
|
|
bool ready = false;
|
|
int trylock = SCE_OK;
|
|
};
|
|
|
|
static void* APS5_VABI Trylocker(void* arg) {
|
|
auto& signalled = *static_cast<Signalled*>(arg);
|
|
signalled.trylock = scePthreadMutexTrylock(&signalled.mutex);
|
|
return nullptr;
|
|
}
|
|
|
|
static void* APS5_VABI Signaller(void* arg) {
|
|
auto& signalled = *static_cast<Signalled*>(arg);
|
|
Require(scePthreadMutexLock(&signalled.mutex) == SCE_OK);
|
|
signalled.ready = true;
|
|
Require(scePthreadCondSignal(&signalled.cond) == SCE_OK);
|
|
Require(scePthreadMutexUnlock(&signalled.mutex) == SCE_OK);
|
|
return nullptr;
|
|
}
|
|
|
|
static void RequireHeldByCaller(Signalled& signalled) {
|
|
Pthread thread = nullptr;
|
|
Require(scePthreadCreate(&thread, nullptr, Trylocker, &signalled, nullptr) == SCE_OK);
|
|
Require(scePthreadJoin(thread, nullptr) == SCE_OK);
|
|
Require(signalled.trylock == SCE_KERNEL_ERROR_EBUSY);
|
|
}
|
|
|
|
static void RelativeWait() {
|
|
Signalled signalled;
|
|
Require(scePthreadMutexInit(&signalled.mutex, nullptr, nullptr) == SCE_OK);
|
|
Require(scePthreadCondInit(&signalled.cond, nullptr, nullptr) == SCE_OK);
|
|
|
|
Require(pthread_cond_reltimedwait_np_nid_postfix(&signalled.cond, &signalled.mutex, 1000) == POSIX_EPERM);
|
|
|
|
Require(scePthreadMutexLock(&signalled.mutex) == SCE_OK);
|
|
const auto start = std::chrono::steady_clock::now();
|
|
Require(pthread_cond_reltimedwait_np_nid_postfix(&signalled.cond, &signalled.mutex, 50000) == POSIX_ETIMEDOUT);
|
|
Require(std::chrono::steady_clock::now() - start >= std::chrono::milliseconds(40));
|
|
RequireHeldByCaller(signalled);
|
|
Require(pthread_cond_reltimedwait_np_nid_postfix(&signalled.cond, &signalled.mutex, 0) == POSIX_ETIMEDOUT);
|
|
RequireHeldByCaller(signalled);
|
|
|
|
Pthread thread = nullptr;
|
|
Require(scePthreadCreate(&thread, nullptr, Signaller, &signalled, nullptr) == SCE_OK);
|
|
const auto waitStart = std::chrono::steady_clock::now();
|
|
while (!signalled.ready)
|
|
Require(pthread_cond_reltimedwait_np_nid_postfix(&signalled.cond, &signalled.mutex, 10000000) == 0);
|
|
Require(std::chrono::steady_clock::now() - waitStart < std::chrono::seconds(5));
|
|
RequireHeldByCaller(signalled);
|
|
Require(scePthreadMutexUnlock(&signalled.mutex) == SCE_OK);
|
|
Require(scePthreadJoin(thread, nullptr) == SCE_OK);
|
|
|
|
Require(scePthreadCondDestroy(&signalled.cond) == SCE_OK);
|
|
Require(scePthreadMutexDestroy(&signalled.mutex) == SCE_OK);
|
|
}
|
|
|
|
int main() {
|
|
RelativeWait();
|
|
|
|
Context context;
|
|
PthreadCond cond = nullptr;
|
|
Require(scePthreadMutexInit(&context.mutex, nullptr, nullptr) == SCE_OK);
|
|
Require(scePthreadCondInit(&cond, nullptr, nullptr) == SCE_OK);
|
|
|
|
Require(scePthreadMutexLock(&context.mutex) == SCE_OK);
|
|
Pthread thread = nullptr;
|
|
Require(scePthreadCreate(&thread, nullptr, Contender, &context, nullptr) == SCE_OK);
|
|
while (!context.acquired.load())
|
|
Require(scePthreadCondTimedwait(&cond, &context.mutex, 1000) == SCE_KERNEL_ERROR_ETIMEDOUT);
|
|
Require(scePthreadMutexUnlock(&context.mutex) == SCE_OK);
|
|
Require(scePthreadJoin(thread, nullptr) == SCE_OK);
|
|
|
|
Require(scePthreadCondDestroy(&cond) == SCE_OK);
|
|
Require(scePthreadMutexDestroy(&context.mutex) == SCE_OK);
|
|
|
|
Require(sceKernelSleep(0) == SCE_OK);
|
|
void* finalizeHandle = reinterpret_cast<void*>(7);
|
|
Require(__cxa_atexit_nid_postfix(Cleanup, nullptr, finalizeHandle) == SCE_OK);
|
|
__pthread_cxa_finalize_nid_postfix(finalizeHandle);
|
|
Require(cleanupAt == 0);
|
|
}
|