feat(kernel): implement sceKernelDeleteSema

Waiters are woken with SCE_KERNEL_ERROR_EACCES and the semaphore is freed
once they have left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
QDarkRex
2026-09-27 12:38:59 +07:00
parent 15b749c2ba
commit be127fd0f1
2 changed files with 34 additions and 9 deletions
@@ -12,6 +12,7 @@
constexpr int KERNEL_SEMA_OK = 0;
constexpr int KERNEL_SEMA_ERROR_EINVAL = static_cast<int>(0x80020016);
constexpr int KERNEL_SEMA_ERROR_EACCES = static_cast<int>(0x8002000D);
constexpr int KERNEL_SEMA_ERROR_EBUSY = static_cast<int>(0x80020010);
constexpr int KERNEL_SEMA_ERROR_ETIMEDOUT = static_cast<int>(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);
@@ -53,14 +53,29 @@ int APS5_VABI sceKernelWaitSema(KernelSema sem, int need, KernelUseconds* time)
}
std::unique_lock<std::mutex> 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<std::mutex> 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;
}
}