From 275aaeef9c94db25a4a52cfcda1dac0dcc9e6c06 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Mon, 20 Jul 2015 17:15:54 -0500
Subject: [PATCH] Kernel/Scheduling: Clean up a thread's wait_objects when its
 scheduled.

They'll be reset if needed during the next svcWaitSynchronization call (if there's any pending)
---
 src/core/hle/kernel/thread.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 64166ab993..8b49fc7dfe 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -217,6 +217,14 @@ static void SwitchContext(Thread* new_thread) {
             new_thread->context.pc -= thumb_mode ? 2 : 4;
         }
 
+        // Clean up the thread's wait_objects, they'll be restored if needed during
+        // the svcWaitSynchronization call
+        for (int i = 0; i < new_thread->wait_objects.size(); ++i) {
+            SharedPtr<WaitObject> object = new_thread->wait_objects[i];
+            object->RemoveWaitingThread(new_thread);
+        }
+        new_thread->wait_objects.clear();
+
         ready_queue.remove(new_thread->current_priority, new_thread);
         new_thread->status = THREADSTATUS_RUNNING;