Thread: Move ResumeThreadFromWait into member function

This commit is contained in:
Yuri Kunde Schlesner 2014-12-22 11:58:13 -02:00
parent eae3d8e6d8
commit ce6b967f4e
5 changed files with 28 additions and 22 deletions

View File

@ -88,7 +88,9 @@ ResultCode SignalEvent(const Handle handle) {
// Resume threads waiting for event to signal // Resume threads waiting for event to signal
bool event_caught = false; bool event_caught = false;
for (size_t i = 0; i < evt->waiting_threads.size(); ++i) { for (size_t i = 0; i < evt->waiting_threads.size(); ++i) {
ResumeThreadFromWait( evt->waiting_threads[i]); Thread* thread = Kernel::g_handle_table.Get<Thread>(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 // 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, // the event. This will result in the next thread waiting on the event to block. Otherwise,

View File

@ -45,9 +45,12 @@ void MutexAcquireLock(Mutex* mutex, Handle thread = GetCurrentThread()->GetHandl
mutex->lock_thread = thread; mutex->lock_thread = thread;
} }
bool ReleaseMutexForThread(Mutex* mutex, Handle thread) { bool ReleaseMutexForThread(Mutex* mutex, Handle thread_handle) {
MutexAcquireLock(mutex, thread); MutexAcquireLock(mutex, thread_handle);
Kernel::ResumeThreadFromWait(thread);
Thread* thread = Kernel::g_handle_table.Get<Thread>(thread_handle);
if (thread != nullptr)
thread->ResumeFromWait();
return true; return true;
} }

View File

@ -84,7 +84,9 @@ ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
// Notify some of the threads that the semaphore has been released // 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 // stop once the semaphore is full again or there are no more waiting threads
while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) { while (!semaphore->waiting_threads.empty() && semaphore->IsAvailable()) {
Kernel::ResumeThreadFromWait(semaphore->waiting_threads.front()); Thread* thread = Kernel::g_handle_table.Get<Thread>(semaphore->waiting_threads.front());
if (thread != nullptr)
thread->ResumeFromWait();
semaphore->waiting_threads.pop(); semaphore->waiting_threads.pop();
--semaphore->available_count; --semaphore->available_count;
} }

View File

@ -131,7 +131,7 @@ void Thread::Stop(const char* reason) {
Thread* waiting_thread = g_handle_table.Get<Thread>(waiting_handle); Thread* waiting_thread = g_handle_table.Get<Thread>(waiting_handle);
if (CheckWaitType(waiting_thread, WAITTYPE_THREADEND, GetHandle())) if (CheckWaitType(waiting_thread, WAITTYPE_THREADEND, GetHandle()))
ResumeThreadFromWait(waiting_handle); waiting_thread->ResumeFromWait();
} }
waiting_threads.clear(); waiting_threads.clear();
@ -177,8 +177,11 @@ Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) {
} }
} }
// If a thread was arbitrated, resume it // If a thread was arbitrated, resume it
if (0 != highest_priority_thread) if (0 != highest_priority_thread) {
ResumeThreadFromWait(highest_priority_thread); Thread* thread = Kernel::g_handle_table.Get<Thread>(highest_priority_thread);
if (thread != nullptr)
thread->ResumeFromWait();
}
return highest_priority_thread; return highest_priority_thread;
} }
@ -191,7 +194,7 @@ void ArbitrateAllThreads(u32 arbiter, u32 address) {
Thread* thread = g_handle_table.Get<Thread>(handle); Thread* thread = g_handle_table.Get<Thread>(handle);
if (CheckWaitType(thread, WAITTYPE_ARB, arbiter, address)) 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" /// Resumes a thread from waiting by marking it as "ready"
void ResumeThreadFromWait(Handle handle) { void Thread::ResumeFromWait() {
Thread* thread = Kernel::g_handle_table.Get<Thread>(handle); status &= ~THREADSTATUS_WAIT;
if (thread) { wait_handle = 0;
thread->status &= ~THREADSTATUS_WAIT; wait_type = WAITTYPE_NONE;
thread->wait_handle = 0; if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
thread->wait_type = WAITTYPE_NONE; ChangeReadyState(this, true);
if (!(thread->status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
ChangeReadyState(thread, true);
}
} }
} }
@ -416,7 +416,7 @@ void Reschedule() {
// will immediately be placed back in the queue for execution. // will immediately be placed back in the queue for execution.
if (CheckWaitType(prev, WAITTYPE_SLEEP)) if (CheckWaitType(prev, WAITTYPE_SLEEP))
ResumeThreadFromWait(prev->GetHandle()); prev->ResumeFromWait();
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -76,6 +76,8 @@ public:
u32 GetThreadId() const { return thread_id; } u32 GetThreadId() const { return thread_id; }
void Stop(const char* reason); void Stop(const char* reason);
/// Resumes a thread from waiting by marking it as "ready".
void ResumeFromWait();
Core::ThreadContext context; 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) /// Reschedules to the next available thread (call after current thread is suspended)
void Reschedule(); void Reschedule();
/// Resumes a thread from waiting by marking it as "ready"
void ResumeThreadFromWait(Handle handle);
/// Arbitrate the highest priority thread that is waiting /// Arbitrate the highest priority thread that is waiting
Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address); Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address);