diff --git a/src/core/hle/kernel/event.cpp b/src/core/hle/kernel/event.cpp index cecc2fb58..6f21134ac 100644 --- a/src/core/hle/kernel/event.cpp +++ b/src/core/hle/kernel/event.cpp @@ -88,7 +88,9 @@ ResultCode SignalEvent(const Handle handle) { // Resume threads waiting for event to signal bool event_caught = false; for (size_t i = 0; i < evt->waiting_threads.size(); ++i) { - ResumeThreadFromWait( evt->waiting_threads[i]); + Thread* thread = Kernel::g_handle_table.Get(evt->waiting_threads[i]); + if (thread != nullptr) + thread->ResumeFromWait(); // If any thread is signalled awake by this event, assume the event was "caught" and reset // the event. This will result in the next thread waiting on the event to block. Otherwise, diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index d3468c19c..d3d07437b 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp @@ -45,9 +45,12 @@ void MutexAcquireLock(Mutex* mutex, Handle thread = GetCurrentThread()->GetHandl mutex->lock_thread = thread; } -bool ReleaseMutexForThread(Mutex* mutex, Handle thread) { - MutexAcquireLock(mutex, thread); - Kernel::ResumeThreadFromWait(thread); +bool ReleaseMutexForThread(Mutex* mutex, Handle thread_handle) { + MutexAcquireLock(mutex, thread_handle); + + Thread* thread = Kernel::g_handle_table.Get(thread_handle); + if (thread != nullptr) + thread->ResumeFromWait(); return true; } diff --git a/src/core/hle/kernel/semaphore.cpp b/src/core/hle/kernel/semaphore.cpp index fbf6de0e7..9215e6e0b 100644 --- a/src/core/hle/kernel/semaphore.cpp +++ b/src/core/hle/kernel/semaphore.cpp @@ -84,7 +84,9 @@ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) { // Notify some of the threads that the semaphore has been released // stop once the semaphore is full again or there are no more waiting threads while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) { - Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front()); + Thread* thread = Kernel::g_handle_table.Get(semaphore->waiting_threads.front()); + if (thread != nullptr) + thread->ResumeFromWait(); semaphore->waiting_threads.pop(); --semaphore->available_count; } diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index d96dd8eab..dfb80760d 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -131,7 +131,7 @@ void Thread::Stop(const char* reason) { Thread* waiting_thread = g_handle_table.Get(waiting_handle); if (CheckWaitType(waiting_thread, WAITTYPE_THREADEND, GetHandle())) - ResumeThreadFromWait(waiting_handle); + waiting_thread->ResumeFromWait(); } waiting_threads.clear(); @@ -177,8 +177,11 @@ Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) { } } // If a thread was arbitrated, resume it - if (0 != highest_priority_thread) - ResumeThreadFromWait(highest_priority_thread); + if (0 != highest_priority_thread) { + Thread* thread = Kernel::g_handle_table.Get(highest_priority_thread); + if (thread != nullptr) + thread->ResumeFromWait(); + } return highest_priority_thread; } @@ -191,7 +194,7 @@ void ArbitrateAllThreads(u32 arbiter, u32 address) { Thread* thread = g_handle_table.Get(handle); if (CheckWaitType(thread, WAITTYPE_ARB, arbiter, address)) - ResumeThreadFromWait(handle); + thread->ResumeFromWait(); } } @@ -257,15 +260,12 @@ void WaitCurrentThread(WaitType wait_type, Handle wait_handle, VAddr wait_addres } /// Resumes a thread from waiting by marking it as "ready" -void ResumeThreadFromWait(Handle handle) { - Thread* thread = Kernel::g_handle_table.Get(handle); - if (thread) { - thread->status &= ~THREADSTATUS_WAIT; - thread->wait_handle = 0; - thread->wait_type = WAITTYPE_NONE; - if (!(thread->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { - ChangeReadyState(thread, true); - } +void Thread::ResumeFromWait() { + status &= ~THREADSTATUS_WAIT; + wait_handle = 0; + wait_type = WAITTYPE_NONE; + if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { + ChangeReadyState(this, true); } } @@ -416,7 +416,7 @@ void Reschedule() { // will immediately be placed back in the queue for execution. if (CheckWaitType(prev, WAITTYPE_SLEEP)) - ResumeThreadFromWait(prev->GetHandle()); + prev->ResumeFromWait(); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 08481ab15..b589e7ba4 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -76,6 +76,8 @@ public: u32 GetThreadId() const { return thread_id; } void Stop(const char* reason); + /// Resumes a thread from waiting by marking it as "ready". + void ResumeFromWait(); Core::ThreadContext context; @@ -109,9 +111,6 @@ Thread* SetupMainThread(s32 priority, int stack_size = Kernel::DEFAULT_STACK_SIZ /// Reschedules to the next available thread (call after current thread is suspended) void Reschedule(); -/// Resumes a thread from waiting by marking it as "ready" -void ResumeThreadFromWait(Handle handle); - /// Arbitrate the highest priority thread that is waiting Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address);