diff --git a/core/libs/prx/libkernel/Semaphore/include/Semaphore.hpp b/core/libs/prx/libkernel/Semaphore/include/Semaphore.hpp index e7c6e49d2..2ec36278e 100644 --- a/core/libs/prx/libkernel/Semaphore/include/Semaphore.hpp +++ b/core/libs/prx/libkernel/Semaphore/include/Semaphore.hpp @@ -12,6 +12,7 @@ constexpr int KERNEL_SEMA_OK = 0; constexpr int KERNEL_SEMA_ERROR_EINVAL = static_cast(0x80020016); +constexpr int KERNEL_SEMA_ERROR_EACCES = static_cast(0x8002000D); constexpr int KERNEL_SEMA_ERROR_EBUSY = static_cast(0x80020010); constexpr int KERNEL_SEMA_ERROR_ETIMEDOUT = static_cast(0x8002003C); @@ -24,11 +25,14 @@ struct KernelSemaPrivate { std::int32_t tokenCount; std::int32_t maxCount; bool isFifo; + bool deleted = false; + std::int32_t waiterCount = 0; }; extern "C" { int APS5_VABI sceKernelCreateSema(KernelSema* sem, const char* name, uint32_t attr, int init, int max, void* opt); +int APS5_VABI sceKernelDeleteSema(KernelSema sem); int APS5_VABI sceKernelPollSema(KernelSema sem, int need); int APS5_VABI sceKernelSignalSema(KernelSema sem, int count); int APS5_VABI sceKernelWaitSema(KernelSema sem, int need, KernelUseconds* time); diff --git a/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp b/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp index ac822495f..de7bb4808 100644 --- a/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp +++ b/core/libs/prx/libkernel/Semaphore/src/Semaphore.cpp @@ -53,14 +53,29 @@ int APS5_VABI sceKernelWaitSema(KernelSema sem, int need, KernelUseconds* time) } std::unique_lock lock(sem->mutex); + ++sem->waiterCount; + struct WaiterGuard { + KernelSemaPrivate* sem; + ~WaiterGuard() { + --sem->waiterCount; + sem->condition.notify_all(); + } + } waiterGuard{sem}; + if (time == nullptr) { - sem->condition.wait(lock, [&] { return sem->tokenCount >= need; }); + sem->condition.wait(lock, [&] { return sem->tokenCount >= need || sem->deleted; }); + if (sem->deleted) { + return KERNEL_SEMA_ERROR_EACCES; + } sem->tokenCount -= need; return KERNEL_SEMA_OK; } auto timeout = std::chrono::microseconds(*time); - bool acquired = sem->condition.wait_for(lock, timeout, [&] { return sem->tokenCount >= need; }); + bool acquired = sem->condition.wait_for(lock, timeout, [&] { return sem->tokenCount >= need || sem->deleted; }); + if (sem->deleted) { + return KERNEL_SEMA_ERROR_EACCES; + } if (!acquired) { return KERNEL_SEMA_ERROR_ETIMEDOUT; } @@ -68,10 +83,6 @@ int APS5_VABI sceKernelWaitSema(KernelSema sem, int need, KernelUseconds* time) return KERNEL_SEMA_OK; } -// --------------------------------------------------------------------------- -// Moved as-is (not yet implemented) from the monolithic libkernel/Export.cpp. -// --------------------------------------------------------------------------- - int APS5_VABI sceKernelCancelSema(KernelSema sem, int count, int* threads) { (void)sem; (void)count; @@ -81,9 +92,19 @@ int APS5_VABI sceKernelCancelSema(KernelSema sem, int count, int* threads) { } int APS5_VABI sceKernelDeleteSema(KernelSema sem) { - (void)sem; - NotImplemented_nid_no_patch(__func__); - return 0; + if (sem == nullptr) { + APS5_INVALID_ARG_EX; + } + + std::unique_lock lock(sem->mutex); + sem->deleted = true; + sem->condition.notify_all(); + while (sem->waiterCount > 0) { + sem->condition.wait(lock); + } + lock.unlock(); + delete sem; + return KERNEL_SEMA_OK; } }