1
0
Fork 0
forked from suyu/suyu

hle: kernel: thread: Replace ThreadStatus/ThreadSchedStatus with a single ThreadState.

- This is how the real kernel works, and is more accurate and simpler.
This commit is contained in:
bunnei 2020-12-28 13:16:43 -08:00
parent 7420a717e6
commit c3c43e32fc
12 changed files with 111 additions and 172 deletions

View file

@ -201,7 +201,7 @@ ResultCode AddressArbiter::WaitForAddressIfLessThan(VAddr address, s32 value, s6
current_thread->SetArbiterWaitAddress(address); current_thread->SetArbiterWaitAddress(address);
InsertThread(SharedFrom(current_thread)); InsertThread(SharedFrom(current_thread));
current_thread->SetState(ThreadStatus::WaitArb); current_thread->SetState(ThreadState::Waiting);
current_thread->WaitForArbitration(true); current_thread->WaitForArbitration(true);
} }
@ -256,7 +256,7 @@ ResultCode AddressArbiter::WaitForAddressIfEqual(VAddr address, s32 value, s64 t
current_thread->SetSynchronizationResults(nullptr, RESULT_TIMEOUT); current_thread->SetSynchronizationResults(nullptr, RESULT_TIMEOUT);
current_thread->SetArbiterWaitAddress(address); current_thread->SetArbiterWaitAddress(address);
InsertThread(SharedFrom(current_thread)); InsertThread(SharedFrom(current_thread));
current_thread->SetState(ThreadStatus::WaitArb); current_thread->SetState(ThreadState::Waiting);
current_thread->WaitForArbitration(true); current_thread->WaitForArbitration(true);
} }

View file

@ -180,22 +180,22 @@ u64 KScheduler::UpdateHighestPriorityThreadsImpl(KernelCore& kernel) {
return cores_needing_scheduling; return cores_needing_scheduling;
} }
void KScheduler::OnThreadStateChanged(KernelCore& kernel, Thread* thread, u32 old_state) { void KScheduler::OnThreadStateChanged(KernelCore& kernel, Thread* thread, ThreadState old_state) {
ASSERT(kernel.GlobalSchedulerContext().IsLocked()); ASSERT(kernel.GlobalSchedulerContext().IsLocked());
// Check if the state has changed, because if it hasn't there's nothing to do. // Check if the state has changed, because if it hasn't there's nothing to do.
const auto cur_state = thread->scheduling_state; const auto cur_state = thread->GetRawState();
if (cur_state == old_state) { if (cur_state == old_state) {
return; return;
} }
// Update the priority queues. // Update the priority queues.
if (old_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { if (old_state == ThreadState::Runnable) {
// If we were previously runnable, then we're not runnable now, and we should remove. // If we were previously runnable, then we're not runnable now, and we should remove.
GetPriorityQueue(kernel).Remove(thread); GetPriorityQueue(kernel).Remove(thread);
IncrementScheduledCount(thread); IncrementScheduledCount(thread);
SetSchedulerUpdateNeeded(kernel); SetSchedulerUpdateNeeded(kernel);
} else if (cur_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { } else if (cur_state == ThreadState::Runnable) {
// If we're now runnable, then we weren't previously, and we should add. // If we're now runnable, then we weren't previously, and we should add.
GetPriorityQueue(kernel).PushBack(thread); GetPriorityQueue(kernel).PushBack(thread);
IncrementScheduledCount(thread); IncrementScheduledCount(thread);
@ -204,12 +204,12 @@ void KScheduler::OnThreadStateChanged(KernelCore& kernel, Thread* thread, u32 ol
} }
void KScheduler::OnThreadPriorityChanged(KernelCore& kernel, Thread* thread, Thread* current_thread, void KScheduler::OnThreadPriorityChanged(KernelCore& kernel, Thread* thread, Thread* current_thread,
u32 old_priority) { s32 old_priority) {
ASSERT(kernel.GlobalSchedulerContext().IsLocked()); ASSERT(kernel.GlobalSchedulerContext().IsLocked());
// If the thread is runnable, we want to change its priority in the queue. // If the thread is runnable, we want to change its priority in the queue.
if (thread->scheduling_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { if (thread->GetRawState() == ThreadState::Runnable) {
GetPriorityQueue(kernel).ChangePriority( GetPriorityQueue(kernel).ChangePriority(
old_priority, thread == kernel.CurrentScheduler()->GetCurrentThread(), thread); old_priority, thread == kernel.CurrentScheduler()->GetCurrentThread(), thread);
IncrementScheduledCount(thread); IncrementScheduledCount(thread);
@ -222,7 +222,7 @@ void KScheduler::OnThreadAffinityMaskChanged(KernelCore& kernel, Thread* thread,
ASSERT(kernel.GlobalSchedulerContext().IsLocked()); ASSERT(kernel.GlobalSchedulerContext().IsLocked());
// If the thread is runnable, we want to change its affinity in the queue. // If the thread is runnable, we want to change its affinity in the queue.
if (thread->scheduling_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { if (thread->GetRawState() == ThreadState::Runnable) {
GetPriorityQueue(kernel).ChangeAffinityMask(old_core, old_affinity, thread); GetPriorityQueue(kernel).ChangeAffinityMask(old_core, old_affinity, thread);
IncrementScheduledCount(thread); IncrementScheduledCount(thread);
SetSchedulerUpdateNeeded(kernel); SetSchedulerUpdateNeeded(kernel);
@ -395,8 +395,8 @@ void KScheduler::YieldWithoutCoreMigration() {
{ {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
const auto cur_state = cur_thread.scheduling_state; const auto cur_state = cur_thread.GetRawState();
if (cur_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { if (cur_state == ThreadState::Runnable) {
// Put the current thread at the back of the queue. // Put the current thread at the back of the queue.
Thread* next_thread = priority_queue.MoveToScheduledBack(std::addressof(cur_thread)); Thread* next_thread = priority_queue.MoveToScheduledBack(std::addressof(cur_thread));
IncrementScheduledCount(std::addressof(cur_thread)); IncrementScheduledCount(std::addressof(cur_thread));
@ -436,8 +436,8 @@ void KScheduler::YieldWithCoreMigration() {
{ {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
const auto cur_state = cur_thread.scheduling_state; const auto cur_state = cur_thread.GetRawState();
if (cur_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { if (cur_state == ThreadState::Runnable) {
// Get the current active core. // Get the current active core.
const s32 core_id = cur_thread.GetActiveCore(); const s32 core_id = cur_thread.GetActiveCore();
@ -526,8 +526,8 @@ void KScheduler::YieldToAnyThread() {
{ {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
const auto cur_state = cur_thread.scheduling_state; const auto cur_state = cur_thread.GetRawState();
if (cur_state == static_cast<u32>(ThreadSchedStatus::Runnable)) { if (cur_state == ThreadState::Runnable) {
// Get the current active core. // Get the current active core.
const s32 core_id = cur_thread.GetActiveCore(); const s32 core_id = cur_thread.GetActiveCore();
@ -645,7 +645,7 @@ void KScheduler::Unload(Thread* thread) {
void KScheduler::Reload(Thread* thread) { void KScheduler::Reload(Thread* thread) {
if (thread) { if (thread) {
ASSERT_MSG(thread->GetState() == ThreadSchedStatus::Runnable, "Thread must be runnable."); ASSERT_MSG(thread->GetState() == ThreadState::Runnable, "Thread must be runnable.");
// Cancel any outstanding wakeup events for this thread // Cancel any outstanding wakeup events for this thread
thread->SetIsRunning(true); thread->SetIsRunning(true);
@ -724,7 +724,7 @@ void KScheduler::SwitchToCurrent() {
do { do {
if (current_thread != nullptr && !current_thread->IsHLEThread()) { if (current_thread != nullptr && !current_thread->IsHLEThread()) {
current_thread->context_guard.lock(); current_thread->context_guard.lock();
if (!current_thread->IsRunnable()) { if (current_thread->GetRawState() != ThreadState::Runnable) {
current_thread->context_guard.unlock(); current_thread->context_guard.unlock();
break; break;
} }
@ -771,7 +771,7 @@ void KScheduler::Initialize() {
{ {
KScopedSchedulerLock lock{system.Kernel()}; KScopedSchedulerLock lock{system.Kernel()};
idle_thread->SetState(ThreadStatus::Ready); idle_thread->SetState(ThreadState::Runnable);
} }
} }

View file

@ -100,11 +100,11 @@ public:
void YieldToAnyThread(); void YieldToAnyThread();
/// Notify the scheduler a thread's status has changed. /// Notify the scheduler a thread's status has changed.
static void OnThreadStateChanged(KernelCore& kernel, Thread* thread, u32 old_state); static void OnThreadStateChanged(KernelCore& kernel, Thread* thread, ThreadState old_state);
/// Notify the scheduler a thread's priority has changed. /// Notify the scheduler a thread's priority has changed.
static void OnThreadPriorityChanged(KernelCore& kernel, Thread* thread, Thread* current_thread, static void OnThreadPriorityChanged(KernelCore& kernel, Thread* thread, Thread* current_thread,
u32 old_priority); s32 old_priority);
/// Notify the scheduler a thread's core and/or affinity mask has changed. /// Notify the scheduler a thread's core and/or affinity mask has changed.
static void OnThreadAffinityMaskChanged(KernelCore& kernel, Thread* thread, static void OnThreadAffinityMaskChanged(KernelCore& kernel, Thread* thread,

View file

@ -77,7 +77,7 @@ ResultCode KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index,
// Mark the thread as waiting. // Mark the thread as waiting.
thread->SetCancellable(); thread->SetCancellable();
thread->SetSyncedObject(nullptr, Svc::ResultTimedOut); thread->SetSyncedObject(nullptr, Svc::ResultTimedOut);
thread->SetState(ThreadState::WaitSynch); thread->SetState(ThreadState::Waiting);
} }
// The lock/sleep is done, so we should be able to get our result. // The lock/sleep is done, so we should be able to get our result.
@ -148,9 +148,9 @@ void KSynchronizationObject::NotifyAvailable(ResultCode result) {
// Iterate over each thread. // Iterate over each thread.
for (auto* cur_node = thread_list_head; cur_node != nullptr; cur_node = cur_node->next) { for (auto* cur_node = thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
Thread* thread = cur_node->thread; Thread* thread = cur_node->thread;
if (thread->GetState() == ThreadSchedStatus::Paused) { if (thread->GetState() == ThreadState::Waiting) {
thread->SetSyncedObject(this, result); thread->SetSyncedObject(this, result);
thread->SetState(ThreadStatus::Ready); thread->SetState(ThreadState::Runnable);
} }
} }
} }

View file

@ -602,7 +602,7 @@ void KernelCore::Suspend(bool in_suspention) {
const bool should_suspend = exception_exited || in_suspention; const bool should_suspend = exception_exited || in_suspention;
{ {
KScopedSchedulerLock lock(*this); KScopedSchedulerLock lock(*this);
ThreadStatus status = should_suspend ? ThreadStatus::Ready : ThreadStatus::WaitSleep; const auto status = should_suspend ? ThreadState::Runnable : ThreadState::Waiting;
for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) { for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
impl->suspend_threads[i]->SetState(status); impl->suspend_threads[i]->SetState(status);
} }

View file

@ -107,7 +107,7 @@ ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
current_thread->SetMutexWaitAddress(address); current_thread->SetMutexWaitAddress(address);
current_thread->SetWaitHandle(requesting_thread_handle); current_thread->SetWaitHandle(requesting_thread_handle);
current_thread->SetState(ThreadStatus::WaitMutex); current_thread->SetState(ThreadState::Waiting);
// Update the lock holder thread's priority to prevent priority inversion. // Update the lock holder thread's priority to prevent priority inversion.
holding_thread->AddMutexWaiter(current_thread); holding_thread->AddMutexWaiter(current_thread);

View file

@ -55,7 +55,7 @@ void SetupMainThread(Core::System& system, Process& owner_process, u32 priority,
// Threads by default are dormant, wake up the main thread so it runs when the scheduler fires // Threads by default are dormant, wake up the main thread so it runs when the scheduler fires
{ {
KScopedSchedulerLock lock{kernel}; KScopedSchedulerLock lock{kernel};
thread->SetState(ThreadStatus::Ready); thread->SetState(ThreadState::Runnable);
} }
} }
} // Anonymous namespace } // Anonymous namespace
@ -318,7 +318,7 @@ void Process::PrepareForTermination() {
continue; continue;
// TODO(Subv): When are the other running/ready threads terminated? // TODO(Subv): When are the other running/ready threads terminated?
ASSERT_MSG(thread->GetStatus() == ThreadStatus::WaitSynch, ASSERT_MSG(thread->GetState() == ThreadState::Waiting,
"Exiting processes with non-waiting threads is currently unimplemented"); "Exiting processes with non-waiting threads is currently unimplemented");
thread->Stop(); thread->Stop();

View file

@ -343,7 +343,7 @@ static ResultCode SendSyncRequest(Core::System& system, Handle handle) {
auto thread = kernel.CurrentScheduler()->GetCurrentThread(); auto thread = kernel.CurrentScheduler()->GetCurrentThread();
{ {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
thread->SetState(ThreadStatus::WaitIPC); thread->SetState(ThreadState::Waiting);
session->SendSyncRequest(SharedFrom(thread), system.Memory(), system.CoreTiming()); session->SendSyncRequest(SharedFrom(thread), system.Memory(), system.CoreTiming());
} }
@ -1546,7 +1546,7 @@ static ResultCode StartThread(Core::System& system, Handle thread_handle) {
return ERR_INVALID_HANDLE; return ERR_INVALID_HANDLE;
} }
ASSERT(thread->GetStatus() == ThreadStatus::Dormant); ASSERT(thread->GetState() == ThreadState::Initialized);
return thread->Start(); return thread->Start();
} }
@ -1661,7 +1661,8 @@ static ResultCode WaitProcessWideKeyAtomic(Core::System& system, VAddr mutex_add
current_thread->SetCondVarWaitAddress(condition_variable_addr); current_thread->SetCondVarWaitAddress(condition_variable_addr);
current_thread->SetMutexWaitAddress(mutex_addr); current_thread->SetMutexWaitAddress(mutex_addr);
current_thread->SetWaitHandle(thread_handle); current_thread->SetWaitHandle(thread_handle);
current_thread->SetState(ThreadStatus::WaitCondVar); current_thread->SetState(ThreadState::Waiting);
current_thread->SetWaitingCondVar(true);
current_process->InsertConditionVariableThread(SharedFrom(current_thread)); current_process->InsertConditionVariableThread(SharedFrom(current_thread));
} }
@ -1755,9 +1756,7 @@ static void SignalProcessWideKey(Core::System& system, VAddr condition_variable_
const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
auto owner = handle_table.Get<Thread>(owner_handle); auto owner = handle_table.Get<Thread>(owner_handle);
ASSERT(owner); ASSERT(owner);
if (thread->GetStatus() == ThreadStatus::WaitCondVar) { thread->SetWaitingCondVar(false);
thread->SetState(ThreadStatus::WaitMutex);
}
owner->AddMutexWaiter(thread); owner->AddMutexWaiter(thread);
} }

View file

@ -44,7 +44,7 @@ Thread::~Thread() = default;
void Thread::Stop() { void Thread::Stop() {
{ {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
SetState(ThreadStatus::Dead); SetState(ThreadState::Terminated);
signaled = true; signaled = true;
NotifyAvailable(); NotifyAvailable();
kernel.GlobalHandleTable().Close(global_handle); kernel.GlobalHandleTable().Close(global_handle);
@ -62,54 +62,43 @@ void Thread::Stop() {
void Thread::Wakeup() { void Thread::Wakeup() {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
switch (status) { switch (thread_state) {
case ThreadStatus::Paused: case ThreadState::Runnable:
case ThreadStatus::WaitSynch:
case ThreadStatus::WaitHLEEvent:
case ThreadStatus::WaitSleep:
case ThreadStatus::WaitIPC:
case ThreadStatus::WaitMutex:
case ThreadStatus::WaitCondVar:
case ThreadStatus::WaitArb:
case ThreadStatus::Dormant:
break;
case ThreadStatus::Ready:
// If the thread is waiting on multiple wait objects, it might be awoken more than once // If the thread is waiting on multiple wait objects, it might be awoken more than once
// before actually resuming. We can ignore subsequent wakeups if the thread status has // before actually resuming. We can ignore subsequent wakeups if the thread status has
// already been set to ThreadStatus::Ready. // already been set to ThreadStatus::Ready.
return; return;
case ThreadStatus::Dead: case ThreadState::Terminated:
// This should never happen, as threads must complete before being stopped. // This should never happen, as threads must complete before being stopped.
DEBUG_ASSERT_MSG(false, "Thread with object id {} cannot be resumed because it's DEAD.", DEBUG_ASSERT_MSG(false, "Thread with object id {} cannot be resumed because it's DEAD.",
GetObjectId()); GetObjectId());
return; return;
} }
SetState(ThreadStatus::Ready); SetState(ThreadState::Runnable);
} }
void Thread::OnWakeUp() { void Thread::OnWakeUp() {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
SetState(ThreadStatus::Ready); SetState(ThreadState::Runnable);
} }
ResultCode Thread::Start() { ResultCode Thread::Start() {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
SetState(ThreadStatus::Ready); SetState(ThreadState::Runnable);
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
void Thread::CancelWait() { void Thread::CancelWait() {
KScopedSchedulerLock lock(kernel); KScopedSchedulerLock lock(kernel);
if (GetState() != ThreadSchedStatus::Paused || !is_cancellable) { if (GetState() != ThreadState::Waiting || !is_cancellable) {
is_sync_cancelled = true; is_sync_cancelled = true;
return; return;
} }
// TODO(Blinkhawk): Implement cancel of server session // TODO(Blinkhawk): Implement cancel of server session
is_sync_cancelled = false; is_sync_cancelled = false;
SetSynchronizationResults(nullptr, ERR_SYNCHRONIZATION_CANCELED); SetSynchronizationResults(nullptr, ERR_SYNCHRONIZATION_CANCELED);
SetState(ThreadStatus::Ready); SetState(ThreadState::Runnable);
} }
static void ResetThreadContext32(Core::ARM_Interface::ThreadContext32& context, u32 stack_top, static void ResetThreadContext32(Core::ARM_Interface::ThreadContext32& context, u32 stack_top,
@ -173,7 +162,7 @@ ResultVal<std::shared_ptr<Thread>> Thread::Create(Core::System& system, ThreadTy
std::shared_ptr<Thread> thread = std::make_shared<Thread>(kernel); std::shared_ptr<Thread> thread = std::make_shared<Thread>(kernel);
thread->thread_id = kernel.CreateNewThreadID(); thread->thread_id = kernel.CreateNewThreadID();
thread->status = ThreadStatus::Dormant; thread->thread_state = ThreadState::Initialized;
thread->entry_point = entry_point; thread->entry_point = entry_point;
thread->stack_top = stack_top; thread->stack_top = stack_top;
thread->disable_count = 1; thread->disable_count = 1;
@ -235,27 +224,18 @@ VAddr Thread::GetCommandBufferAddress() const {
return GetTLSAddress() + command_header_offset; return GetTLSAddress() + command_header_offset;
} }
void Thread::SetState(ThreadStatus new_status) { void Thread::SetState(ThreadState new_status) {
if (new_status == status) { if (new_status == thread_state) {
return; return;
} }
switch (new_status) { if (new_status != ThreadState::Waiting) {
case ThreadStatus::Ready: SetWaitingCondVar(false);
SetSchedulingStatus(ThreadSchedStatus::Runnable);
break;
case ThreadStatus::Dormant:
SetSchedulingStatus(ThreadSchedStatus::None);
break;
case ThreadStatus::Dead:
SetSchedulingStatus(ThreadSchedStatus::Exited);
break;
default:
SetSchedulingStatus(ThreadSchedStatus::Paused);
break;
} }
status = new_status; SetSchedulingStatus(new_status);
thread_state = new_status;
} }
void Thread::AddMutexWaiter(std::shared_ptr<Thread> thread) { void Thread::AddMutexWaiter(std::shared_ptr<Thread> thread) {
@ -312,13 +292,13 @@ void Thread::UpdatePriority() {
return; return;
} }
if (GetStatus() == ThreadStatus::WaitCondVar) { if (GetState() == ThreadState::Waiting && is_waiting_on_condvar) {
owner_process->RemoveConditionVariableThread(SharedFrom(this)); owner_process->RemoveConditionVariableThread(SharedFrom(this));
} }
SetCurrentPriority(new_priority); SetCurrentPriority(new_priority);
if (GetStatus() == ThreadStatus::WaitCondVar) { if (GetState() == ThreadState::Waiting && is_waiting_on_condvar) {
owner_process->InsertConditionVariableThread(SharedFrom(this)); owner_process->InsertConditionVariableThread(SharedFrom(this));
} }
@ -340,7 +320,7 @@ ResultCode Thread::SetActivity(ThreadActivity value) {
auto sched_status = GetState(); auto sched_status = GetState();
if (sched_status != ThreadSchedStatus::Runnable && sched_status != ThreadSchedStatus::Paused) { if (sched_status != ThreadState::Runnable && sched_status != ThreadState::Waiting) {
return ERR_INVALID_STATE; return ERR_INVALID_STATE;
} }
@ -366,7 +346,7 @@ ResultCode Thread::Sleep(s64 nanoseconds) {
Handle event_handle{}; Handle event_handle{};
{ {
KScopedSchedulerLockAndSleep lock(kernel, event_handle, this, nanoseconds); KScopedSchedulerLockAndSleep lock(kernel, event_handle, this, nanoseconds);
SetState(ThreadStatus::WaitSleep); SetState(ThreadState::Waiting);
} }
if (event_handle != InvalidHandle) { if (event_handle != InvalidHandle) {
@ -377,25 +357,24 @@ ResultCode Thread::Sleep(s64 nanoseconds) {
} }
void Thread::AddSchedulingFlag(ThreadSchedFlags flag) { void Thread::AddSchedulingFlag(ThreadSchedFlags flag) {
const u32 old_state = scheduling_state; const auto old_state = GetRawState();
pausing_state |= static_cast<u32>(flag); pausing_state |= static_cast<u32>(flag);
const u32 base_scheduling = static_cast<u32>(GetState()); const auto base_scheduling = GetState();
scheduling_state = base_scheduling | pausing_state; thread_state = base_scheduling | static_cast<ThreadState>(pausing_state);
KScheduler::OnThreadStateChanged(kernel, this, old_state); KScheduler::OnThreadStateChanged(kernel, this, old_state);
} }
void Thread::RemoveSchedulingFlag(ThreadSchedFlags flag) { void Thread::RemoveSchedulingFlag(ThreadSchedFlags flag) {
const u32 old_state = scheduling_state; const auto old_state = GetRawState();
pausing_state &= ~static_cast<u32>(flag); pausing_state &= ~static_cast<u32>(flag);
const u32 base_scheduling = static_cast<u32>(GetState()); const auto base_scheduling = GetState();
scheduling_state = base_scheduling | pausing_state; thread_state = base_scheduling | static_cast<ThreadState>(pausing_state);
KScheduler::OnThreadStateChanged(kernel, this, old_state); KScheduler::OnThreadStateChanged(kernel, this, old_state);
} }
void Thread::SetSchedulingStatus(ThreadSchedStatus new_status) { void Thread::SetSchedulingStatus(ThreadState new_status) {
const u32 old_state = scheduling_state; const auto old_state = GetRawState();
scheduling_state = (scheduling_state & static_cast<u32>(ThreadSchedMasks::HighMask)) | thread_state = (thread_state & ThreadState::HighMask) | new_status;
static_cast<u32>(new_status);
KScheduler::OnThreadStateChanged(kernel, this, old_state); KScheduler::OnThreadStateChanged(kernel, this, old_state);
} }

View file

@ -73,19 +73,26 @@ enum ThreadProcessorId : s32 {
(1 << THREADPROCESSORID_2) | (1 << THREADPROCESSORID_3) (1 << THREADPROCESSORID_2) | (1 << THREADPROCESSORID_3)
}; };
enum class ThreadStatus { enum class ThreadState : u16 {
Ready, ///< Ready to run Initialized = 0,
Paused, ///< Paused by SetThreadActivity or debug Waiting = 1,
WaitHLEEvent, ///< Waiting for hle event to finish Runnable = 2,
WaitSleep, ///< Waiting due to a SleepThread SVC Terminated = 3,
WaitIPC, ///< Waiting for the reply from an IPC request
WaitSynch, ///< Waiting due to WaitSynchronization SuspendShift = 4,
WaitMutex, ///< Waiting due to an ArbitrateLock svc Mask = (1 << SuspendShift) - 1,
WaitCondVar, ///< Waiting due to an WaitProcessWideKey svc
WaitArb, ///< Waiting due to a SignalToAddress/WaitForAddress svc ProcessSuspended = (1 << (0 + SuspendShift)),
Dormant, ///< Created but not yet made ready ThreadSuspended = (1 << (1 + SuspendShift)),
Dead ///< Run to completion, or forcefully terminated DebugSuspended = (1 << (2 + SuspendShift)),
BacktraceSuspended = (1 << (3 + SuspendShift)),
InitSuspended = (1 << (4 + SuspendShift)),
SuspendFlagMask = ((1 << 5) - 1) << SuspendShift,
HighMask = 0xfff0,
}; };
DECLARE_ENUM_FLAG_OPERATORS(ThreadState);
enum class ThreadWakeupReason { enum class ThreadWakeupReason {
Signal, // The thread was woken up by WakeupAllWaitingThreads due to an object signal. Signal, // The thread was woken up by WakeupAllWaitingThreads due to an object signal.
@ -97,13 +104,6 @@ enum class ThreadActivity : u32 {
Paused = 1, Paused = 1,
}; };
enum class ThreadSchedStatus : u32 {
None = 0,
Paused = 1,
Runnable = 2,
Exited = 3,
};
enum class ThreadSchedFlags : u32 { enum class ThreadSchedFlags : u32 {
ProcessPauseFlag = 1 << 4, ProcessPauseFlag = 1 << 4,
ThreadPauseFlag = 1 << 5, ThreadPauseFlag = 1 << 5,
@ -111,12 +111,6 @@ enum class ThreadSchedFlags : u32 {
KernelInitPauseFlag = 1 << 8, KernelInitPauseFlag = 1 << 8,
}; };
enum class ThreadSchedMasks : u32 {
LowMask = 0x000f,
HighMask = 0xfff0,
ForcePauseMask = 0x0070,
};
class Thread final : public KSynchronizationObject { class Thread final : public KSynchronizationObject {
public: public:
explicit Thread(KernelCore& kernel); explicit Thread(KernelCore& kernel);
@ -326,11 +320,19 @@ public:
std::shared_ptr<Common::Fiber>& GetHostContext(); std::shared_ptr<Common::Fiber>& GetHostContext();
ThreadStatus GetStatus() const { ThreadState GetState() const {
return status; return thread_state & ThreadState::Mask;
} }
void SetState(ThreadStatus new_status); ThreadState GetRawState() const {
return thread_state;
}
void SetState(ThreadState new_state);
void SetWaitingCondVar(bool value) {
is_waiting_on_condvar = value;
}
s64 GetLastScheduledTick() const { s64 GetLastScheduledTick() const {
return this->last_scheduled_tick; return this->last_scheduled_tick;
@ -447,15 +449,6 @@ public:
this->schedule_count = count; this->schedule_count = count;
} }
ThreadSchedStatus GetState() const {
return static_cast<ThreadSchedStatus>(scheduling_state &
static_cast<u32>(ThreadSchedMasks::LowMask));
}
bool IsRunnable() const {
return scheduling_state == static_cast<u32>(ThreadSchedStatus::Runnable);
}
bool IsRunning() const { bool IsRunning() const {
return is_running; return is_running;
} }
@ -497,7 +490,7 @@ public:
} }
bool IsTerminationRequested() const { bool IsTerminationRequested() const {
return will_be_terminated || GetState() == ThreadSchedStatus::Exited; return will_be_terminated || GetState() == ThreadState::Terminated;
} }
bool IsPaused() const { bool IsPaused() const {
@ -590,7 +583,7 @@ private:
friend class KScheduler; friend class KScheduler;
friend class Process; friend class Process;
void SetSchedulingStatus(ThreadSchedStatus new_status); void SetSchedulingStatus(ThreadState new_status);
void AddSchedulingFlag(ThreadSchedFlags flag); void AddSchedulingFlag(ThreadSchedFlags flag);
void RemoveSchedulingFlag(ThreadSchedFlags flag); void RemoveSchedulingFlag(ThreadSchedFlags flag);
void SetCurrentPriority(u32 new_priority); void SetCurrentPriority(u32 new_priority);
@ -600,8 +593,7 @@ private:
ThreadContext64 context_64{}; ThreadContext64 context_64{};
std::shared_ptr<Common::Fiber> host_context{}; std::shared_ptr<Common::Fiber> host_context{};
ThreadStatus status = ThreadStatus::Dormant; ThreadState thread_state = ThreadState::Initialized;
u32 scheduling_state = 0;
u64 thread_id = 0; u64 thread_id = 0;
@ -647,6 +639,7 @@ private:
/// If waiting on a ConditionVariable, this is the ConditionVariable address /// If waiting on a ConditionVariable, this is the ConditionVariable address
VAddr condvar_wait_address = 0; VAddr condvar_wait_address = 0;
bool is_waiting_on_condvar{};
/// If waiting on a Mutex, this is the mutex address /// If waiting on a Mutex, this is the mutex address
VAddr mutex_wait_address = 0; VAddr mutex_wait_address = 0;
/// The handle used to wait for the mutex. /// The handle used to wait for the mutex.

View file

@ -42,8 +42,7 @@ void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64
event_handle = timetask->GetGlobalHandle(); event_handle = timetask->GetGlobalHandle();
if (nanoseconds > 0) { if (nanoseconds > 0) {
ASSERT(timetask); ASSERT(timetask);
ASSERT(timetask->GetStatus() != ThreadStatus::Ready); ASSERT(timetask->GetState() != ThreadState::Runnable);
ASSERT(timetask->GetStatus() != ThreadStatus::WaitMutex);
system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds}, system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds},
time_manager_event_type, event_handle); time_manager_event_type, event_handle);
} else { } else {

View file

@ -238,8 +238,8 @@ WaitTreeThread::~WaitTreeThread() = default;
QString WaitTreeThread::GetText() const { QString WaitTreeThread::GetText() const {
const auto& thread = static_cast<const Kernel::Thread&>(object); const auto& thread = static_cast<const Kernel::Thread&>(object);
QString status; QString status;
switch (thread.GetStatus()) { switch (thread.GetState()) {
case Kernel::ThreadStatus::Ready: case Kernel::ThreadState::Runnable:
if (!thread.IsPaused()) { if (!thread.IsPaused()) {
if (thread.WasRunning()) { if (thread.WasRunning()) {
status = tr("running"); status = tr("running");
@ -250,35 +250,14 @@ QString WaitTreeThread::GetText() const {
status = tr("paused"); status = tr("paused");
} }
break; break;
case Kernel::ThreadStatus::Paused: case Kernel::ThreadState::Waiting:
status = tr("paused"); status = tr("waiting");
break; break;
case Kernel::ThreadStatus::WaitHLEEvent: case Kernel::ThreadState::Initialized:
status = tr("waiting for HLE return"); status = tr("initialized");
break; break;
case Kernel::ThreadStatus::WaitSleep: case Kernel::ThreadState::Terminated:
status = tr("sleeping"); status = tr("terminated");
break;
case Kernel::ThreadStatus::WaitIPC:
status = tr("waiting for IPC reply");
break;
case Kernel::ThreadStatus::WaitSynch:
status = tr("waiting for objects");
break;
case Kernel::ThreadStatus::WaitMutex:
status = tr("waiting for mutex");
break;
case Kernel::ThreadStatus::WaitCondVar:
status = tr("waiting for condition variable");
break;
case Kernel::ThreadStatus::WaitArb:
status = tr("waiting for address arbiter");
break;
case Kernel::ThreadStatus::Dormant:
status = tr("dormant");
break;
case Kernel::ThreadStatus::Dead:
status = tr("dead");
break; break;
} }
@ -294,8 +273,8 @@ QColor WaitTreeThread::GetColor() const {
const std::size_t color_index = IsDarkTheme() ? 1 : 0; const std::size_t color_index = IsDarkTheme() ? 1 : 0;
const auto& thread = static_cast<const Kernel::Thread&>(object); const auto& thread = static_cast<const Kernel::Thread&>(object);
switch (thread.GetStatus()) { switch (thread.GetState()) {
case Kernel::ThreadStatus::Ready: case Kernel::ThreadState::Runnable:
if (!thread.IsPaused()) { if (!thread.IsPaused()) {
if (thread.WasRunning()) { if (thread.WasRunning()) {
return QColor(WaitTreeColors[0][color_index]); return QColor(WaitTreeColors[0][color_index]);
@ -305,21 +284,11 @@ QColor WaitTreeThread::GetColor() const {
} else { } else {
return QColor(WaitTreeColors[2][color_index]); return QColor(WaitTreeColors[2][color_index]);
} }
case Kernel::ThreadStatus::Paused: case Kernel::ThreadState::Waiting:
return QColor(WaitTreeColors[3][color_index]); return QColor(WaitTreeColors[3][color_index]);
case Kernel::ThreadStatus::WaitHLEEvent: case Kernel::ThreadState::Initialized:
case Kernel::ThreadStatus::WaitIPC:
return QColor(WaitTreeColors[4][color_index]);
case Kernel::ThreadStatus::WaitSleep:
return QColor(WaitTreeColors[5][color_index]);
case Kernel::ThreadStatus::WaitSynch:
case Kernel::ThreadStatus::WaitMutex:
case Kernel::ThreadStatus::WaitCondVar:
case Kernel::ThreadStatus::WaitArb:
return QColor(WaitTreeColors[6][color_index]);
case Kernel::ThreadStatus::Dormant:
return QColor(WaitTreeColors[7][color_index]); return QColor(WaitTreeColors[7][color_index]);
case Kernel::ThreadStatus::Dead: case Kernel::ThreadState::Terminated:
return QColor(WaitTreeColors[8][color_index]); return QColor(WaitTreeColors[8][color_index]);
default: default:
return WaitTreeItem::GetColor(); return WaitTreeItem::GetColor();
@ -367,7 +336,7 @@ std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeThread::GetChildren() const {
list.push_back(std::make_unique<WaitTreeText>(tr("not waiting for mutex"))); list.push_back(std::make_unique<WaitTreeText>(tr("not waiting for mutex")));
} }
if (thread.GetStatus() == Kernel::ThreadStatus::WaitSynch) { if (thread.GetState() == Kernel::ThreadState::Waiting) {
list.push_back(std::make_unique<WaitTreeObjectList>(thread.GetWaitObjectsForDebugging(), list.push_back(std::make_unique<WaitTreeObjectList>(thread.GetWaitObjectsForDebugging(),
thread.IsCancellable())); thread.IsCancellable()));
} }