kern: unconditionally set thread state when appropriate

This commit is contained in:
Michael Scire 2021-04-07 01:44:27 -07:00 committed by SciresM
parent 3356eddcba
commit ec1d9c4c49
3 changed files with 15 additions and 32 deletions

View file

@ -35,11 +35,7 @@ namespace ams::kern {
owner_thread->AddWaiter(cur_thread);
/* Set thread states. */
if (AMS_LIKELY(cur_thread->GetState() == KThread::ThreadState_Runnable)) {
cur_thread->SetState(KThread::ThreadState_Waiting);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
cur_thread->SetState(KThread::ThreadState_Waiting);
if (owner_thread->IsSuspended()) {
owner_thread->ContinueIfHasKernelWaiters();
@ -49,10 +45,9 @@ namespace ams::kern {
/* We're no longer waiting on the lock owner. */
{
KScopedSchedulerLock sl;
KThread *owner_thread = cur_thread->GetLockOwner();
if (AMS_UNLIKELY(owner_thread)) {
if (KThread *owner_thread = cur_thread->GetLockOwner(); AMS_UNLIKELY(owner_thread != nullptr)) {
owner_thread->RemoveWaiter(cur_thread);
KScheduler::SetSchedulerUpdateNeeded();
}
}
}
@ -70,17 +65,13 @@ namespace ams::kern {
/* Pass the lock to the next owner. */
uintptr_t next_tag = 0;
if (next_owner) {
if (next_owner != nullptr) {
next_tag = reinterpret_cast<uintptr_t>(next_owner);
if (num_waiters > 1) {
next_tag |= 0x1;
}
if (AMS_LIKELY(next_owner->GetState() == KThread::ThreadState_Waiting)) {
next_owner->SetState(KThread::ThreadState_Runnable);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
next_owner->SetState(KThread::ThreadState_Runnable);
if (next_owner->IsSuspended()) {
next_owner->ContinueIfHasKernelWaiters();

View file

@ -740,25 +740,22 @@ namespace ams::kern {
/* If we have no exception thread, we succeeded. */
if (m_exception_thread == nullptr) {
m_exception_thread = cur_thread;
KScheduler::SetSchedulerUpdateNeeded();
return true;
}
/* Otherwise, wait for us to not have an exception thread. */
cur_thread->SetAddressKey(address_key | 1);
m_exception_thread->AddWaiter(cur_thread);
if (cur_thread->GetState() == KThread::ThreadState_Runnable) {
cur_thread->SetState(KThread::ThreadState_Waiting);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
cur_thread->SetState(KThread::ThreadState_Waiting);
}
/* Remove the thread as a waiter from the lock owner. */
{
KScopedSchedulerLock sl;
KThread *owner_thread = cur_thread->GetLockOwner();
if (owner_thread != nullptr) {
if (KThread *owner_thread = cur_thread->GetLockOwner(); owner_thread != nullptr) {
owner_thread->RemoveWaiter(cur_thread);
KScheduler::SetSchedulerUpdateNeeded();
}
}
}
@ -779,15 +776,12 @@ namespace ams::kern {
/* Remove waiter thread. */
s32 num_waiters;
KThread *next = thread->RemoveWaiterByKey(std::addressof(num_waiters), reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread)));
if (next != nullptr) {
if (next->GetState() == KThread::ThreadState_Waiting) {
next->SetState(KThread::ThreadState_Runnable);
} else {
KScheduler::SetSchedulerUpdateNeeded();
}
if (KThread *next = thread->RemoveWaiterByKey(std::addressof(num_waiters), reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread))); next != nullptr) {
next->SetState(KThread::ThreadState_Runnable);
}
KScheduler::SetSchedulerUpdateNeeded();
return true;
} else {
return false;

View file

@ -218,9 +218,7 @@ namespace ams::kern {
KThread *task_thread = Kernel::GetInterruptTaskManager().GetThread();
{
KScopedSchedulerLock sl;
if (AMS_LIKELY(task_thread->GetState() == KThread::ThreadState_Waiting)) {
task_thread->SetState(KThread::ThreadState_Runnable);
}
task_thread->SetState(KThread::ThreadState_Runnable);
}
}