From c8d0d5637a35690ba1b4faf7cc12c027799fb9e7 Mon Sep 17 00:00:00 2001 From: TheTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Sun, 15 Sep 2024 19:08:37 +0300 Subject: [PATCH] semaphore: Fix semaphore wait list leaking (#928) Co-authored-by: Daniel R. <47796739+polybiusproxy@users.noreply.github.com> --- src/core/libraries/kernel/threads/semaphore.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/core/libraries/kernel/threads/semaphore.cpp b/src/core/libraries/kernel/threads/semaphore.cpp index 63ca25338..312f82b69 100644 --- a/src/core/libraries/kernel/threads/semaphore.cpp +++ b/src/core/libraries/kernel/threads/semaphore.cpp @@ -34,10 +34,14 @@ public: // Create waiting thread object and add it into the list of waiters. WaitingThread waiter{need_count, is_fifo}; - AddWaiter(&waiter); + const auto it = AddWaiter(&waiter); // Perform the wait. - return waiter.Wait(lk, timeout); + const s32 result = waiter.Wait(lk, timeout); + if (result == SCE_KERNEL_ERROR_ETIMEDOUT) { + wait_list.erase(it); + } + return result; } bool Signal(s32 signal_count) { @@ -129,11 +133,13 @@ public: } }; - void AddWaiter(WaitingThread* waiter) { + using WaitList = std::list; + + WaitList::iterator AddWaiter(WaitingThread* waiter) { // Insert at the end of the list for FIFO order. if (is_fifo) { wait_list.push_back(waiter); - return; + return --wait_list.end(); } // Find the first with priority less then us and insert right before it. auto it = wait_list.begin(); @@ -141,9 +147,10 @@ public: it++; } wait_list.insert(it, waiter); + return it; } - std::list wait_list; + WaitList wait_list; std::string name; std::atomic token_count; std::mutex mutex;