VERY experimental Nintendo Switch 2 emulator
C++
Kernel: Add some asserts to enforce the invariants in the scheduler.
Subva0ae8f7c269e
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cppindex 47d4df69c3..f599916f0d 100644--- a/src/core/hle/kernel/kernel.cpp+++ b/src/core/hle/kernel/kernel.cpp@@ -27,6 +27,9 @@ void WaitObject::AddWaitingThread(SharedPtr<Thread> thread) {void WaitObject::RemoveWaitingThread(Thread* thread) {auto itr = std::find(waiting_threads.begin(), waiting_threads.end(), thread);+ // If a thread passed multiple handles to the same object,+ // the kernel might attempt to remove the thread from the object's+ // waiting threads list multiple times.if (itr != waiting_threads.end())waiting_threads.erase(itr);}@@ -36,6 +39,11 @@ SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {s32 candidate_priority = THREADPRIO_LOWEST + 1;for (const auto& thread : waiting_threads) {+ // The list of waiting threads must not contain threads that are not waiting to be awakened.+ ASSERT_MSG(thread->status == THREADSTATUS_WAIT_SYNCH_ANY ||+ thread->status == THREADSTATUS_WAIT_SYNCH_ALL,+ "Inconsistent thread statuses in waiting_threads");+if (thread->current_priority >= candidate_priority)continue;diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cppindex 568cef5b91..9109bd10ba 100644--- a/src/core/hle/kernel/thread.cpp+++ b/src/core/hle/kernel/thread.cpp@@ -200,8 +200,8 @@ static void SwitchContext(Thread* new_thread) {// Load context of new threadif (new_thread) {- DEBUG_ASSERT_MSG(new_thread->status == THREADSTATUS_READY,- "Thread must be ready to become running.");+ ASSERT_MSG(new_thread->status == THREADSTATUS_READY,+ "Thread must be ready to become running.");// Cancel any outstanding wakeup events for this threadCoreTiming::UnscheduleEvent(ThreadWakeupEventType, new_thread->callback_handle);@@ -307,6 +307,8 @@ void Thread::WakeAfterDelay(s64 nanoseconds) {}void Thread::ResumeFromWait() {+ ASSERT_MSG(wait_objects.empty(), "Thread is waking up while waiting for objects");+switch (status) {case THREADSTATUS_WAIT_SYNCH_ALL:case THREADSTATUS_WAIT_SYNCH_ANY:@@ -580,6 +582,7 @@ void Thread::SetWaitSynchronizationOutput(s32 output) {}s32 Thread::GetWaitObjectIndex(WaitObject* object) const {+ ASSERT_MSG(!wait_objects.empty(), "Thread is not waiting for anything");auto match = std::find(wait_objects.rbegin(), wait_objects.rend(), object);return std::distance(match, wait_objects.rend()) - 1;}