summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/k_thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core/hle/kernel/k_thread.cpp127
1 files changed, 92 insertions, 35 deletions
diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp
index 71e029a3f..f42abb8a1 100644
--- a/src/core/hle/kernel/k_thread.cpp
+++ b/src/core/hle/kernel/k_thread.cpp
@@ -30,6 +30,7 @@
#include "core/hle/kernel/k_system_control.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/k_thread_queue.h"
+#include "core/hle/kernel/k_worker_task_manager.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/svc_results.h"
#include "core/hle/kernel/time_manager.h"
@@ -105,7 +106,7 @@ KThread::~KThread() = default;
ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio,
s32 virt_core, KProcess* owner, ThreadType type) {
// Assert parameters are valid.
- ASSERT((type == ThreadType::Main) ||
+ ASSERT((type == ThreadType::Main) || (type == ThreadType::Dummy) ||
(Svc::HighestThreadPriority <= prio && prio <= Svc::LowestThreadPriority));
ASSERT((owner != nullptr) || (type != ThreadType::User));
ASSERT(0 <= virt_core && virt_core < static_cast<s32>(Common::BitSize<u64>()));
@@ -139,7 +140,7 @@ ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_s
UNREACHABLE_MSG("KThread::Initialize: Unknown ThreadType {}", static_cast<u32>(type));
break;
}
- thread_type_for_debugging = type;
+ thread_type = type;
// Set the ideal core ID and affinity mask.
virtual_ideal_core_id = virt_core;
@@ -261,7 +262,7 @@ ResultCode KThread::InitializeThread(KThread* thread, KThreadFunction func, uint
}
ResultCode KThread::InitializeDummyThread(KThread* thread) {
- return thread->Initialize({}, {}, {}, DefaultThreadPriority, 3, {}, ThreadType::Dummy);
+ return thread->Initialize({}, {}, {}, DummyThreadPriority, 3, {}, ThreadType::Dummy);
}
ResultCode KThread::InitializeIdleThread(Core::System& system, KThread* thread, s32 virt_core) {
@@ -332,7 +333,7 @@ void KThread::Finalize() {
}
// Perform inherited finalization.
- KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>::Finalize();
+ KSynchronizationObject::Finalize();
}
bool KThread::IsSignaled() const {
@@ -376,11 +377,28 @@ void KThread::StartTermination() {
// Register terminated dpc flag.
RegisterDpc(DpcFlag::Terminated);
+}
+
+void KThread::FinishTermination() {
+ // Ensure that the thread is not executing on any core.
+ if (parent != nullptr) {
+ for (std::size_t i = 0; i < static_cast<std::size_t>(Core::Hardware::NUM_CPU_CORES); ++i) {
+ KThread* core_thread{};
+ do {
+ core_thread = kernel.Scheduler(i).GetCurrentThread();
+ } while (core_thread == this);
+ }
+ }
// Close the thread.
this->Close();
}
+void KThread::DoWorkerTaskImpl() {
+ // Finish the termination that was begun by Exit().
+ this->FinishTermination();
+}
+
void KThread::Pin(s32 current_core) {
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
@@ -417,12 +435,7 @@ void KThread::Pin(s32 current_core) {
static_cast<u32>(ThreadState::SuspendShift)));
// Update our state.
- const ThreadState old_state = thread_state;
- thread_state = static_cast<ThreadState>(GetSuspendFlags() |
- static_cast<u32>(old_state & ThreadState::Mask));
- if (thread_state != old_state) {
- KScheduler::OnThreadStateChanged(kernel, this, old_state);
- }
+ UpdateState();
}
// TODO(bunnei): Update our SVC access permissions.
@@ -463,20 +476,13 @@ void KThread::Unpin() {
}
// Allow performing thread suspension (if termination hasn't been requested).
- {
+ if (!IsTerminationRequested()) {
// Update our allow flags.
- if (!IsTerminationRequested()) {
- suspend_allowed_flags |= (1 << (static_cast<u32>(SuspendType::Thread) +
- static_cast<u32>(ThreadState::SuspendShift)));
- }
+ suspend_allowed_flags |= (1 << (static_cast<u32>(SuspendType::Thread) +
+ static_cast<u32>(ThreadState::SuspendShift)));
// Update our state.
- const ThreadState old_state = thread_state;
- thread_state = static_cast<ThreadState>(GetSuspendFlags() |
- static_cast<u32>(old_state & ThreadState::Mask));
- if (thread_state != old_state) {
- KScheduler::OnThreadStateChanged(kernel, this, old_state);
- }
+ UpdateState();
}
// TODO(bunnei): Update our SVC access permissions.
@@ -689,12 +695,7 @@ void KThread::Resume(SuspendType type) {
~(1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)));
// Update our state.
- const ThreadState old_state = thread_state;
- thread_state = static_cast<ThreadState>(GetSuspendFlags() |
- static_cast<u32>(old_state & ThreadState::Mask));
- if (thread_state != old_state) {
- KScheduler::OnThreadStateChanged(kernel, this, old_state);
- }
+ this->UpdateState();
}
void KThread::WaitCancel() {
@@ -721,19 +722,22 @@ void KThread::TrySuspend() {
ASSERT(GetNumKernelWaiters() == 0);
// Perform the suspend.
- Suspend();
+ this->UpdateState();
}
-void KThread::Suspend() {
+void KThread::UpdateState() {
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
- ASSERT(IsSuspendRequested());
// Set our suspend flags in state.
const auto old_state = thread_state;
- thread_state = static_cast<ThreadState>(GetSuspendFlags()) | (old_state & ThreadState::Mask);
+ const auto new_state =
+ static_cast<ThreadState>(this->GetSuspendFlags()) | (old_state & ThreadState::Mask);
+ thread_state = new_state;
// Note the state change in scheduler.
- KScheduler::OnThreadStateChanged(kernel, this, old_state);
+ if (new_state != old_state) {
+ KScheduler::OnThreadStateChanged(kernel, this, old_state);
+ }
}
void KThread::Continue() {
@@ -998,13 +1002,16 @@ ResultCode KThread::Run() {
// If the current thread has been asked to suspend, suspend it and retry.
if (GetCurrentThread(kernel).IsSuspended()) {
- GetCurrentThread(kernel).Suspend();
+ GetCurrentThread(kernel).UpdateState();
continue;
}
// If we're not a kernel thread and we've been asked to suspend, suspend ourselves.
- if (IsUserThread() && IsSuspended()) {
- Suspend();
+ if (KProcess* owner = this->GetOwnerProcess(); owner != nullptr) {
+ if (IsUserThread() && IsSuspended()) {
+ this->UpdateState();
+ }
+ owner->IncrementThreadCount();
}
// Set our state and finish.
@@ -1031,9 +1038,16 @@ void KThread::Exit() {
// Disallow all suspension.
suspend_allowed_flags = 0;
+ this->UpdateState();
+
+ // Disallow all suspension.
+ suspend_allowed_flags = 0;
// Start termination.
StartTermination();
+
+ // Register the thread as a work task.
+ KWorkerTaskManager::AddTask(kernel, KWorkerTaskManager::WorkerType::Exit, this);
}
}
@@ -1061,12 +1075,46 @@ ResultCode KThread::Sleep(s64 timeout) {
return ResultSuccess;
}
+void KThread::IfDummyThreadTryWait() {
+ if (!IsDummyThread()) {
+ return;
+ }
+
+ if (GetState() != ThreadState::Waiting) {
+ return;
+ }
+
+ // Block until we can grab the lock.
+ KScopedSpinLock lk{dummy_wait_lock};
+}
+
+void KThread::IfDummyThreadBeginWait() {
+ if (!IsDummyThread()) {
+ return;
+ }
+
+ // Ensure the thread will block when IfDummyThreadTryWait is called.
+ dummy_wait_lock.Lock();
+}
+
+void KThread::IfDummyThreadEndWait() {
+ if (!IsDummyThread()) {
+ return;
+ }
+
+ // Ensure the thread will no longer block.
+ dummy_wait_lock.Unlock();
+}
+
void KThread::BeginWait(KThreadQueue* queue) {
// Set our state as waiting.
SetState(ThreadState::Waiting);
// Set our wait queue.
wait_queue = queue;
+
+ // Special case for dummy threads to ensure they block.
+ IfDummyThreadBeginWait();
}
void KThread::NotifyAvailable(KSynchronizationObject* signaled_object, ResultCode wait_result_) {
@@ -1085,7 +1133,16 @@ void KThread::EndWait(ResultCode wait_result_) {
// If we're waiting, notify our queue that we're available.
if (GetState() == ThreadState::Waiting) {
+ if (wait_queue == nullptr) {
+ // This should never happen, but avoid a hard crash below to get this logged.
+ ASSERT_MSG(false, "wait_queue is nullptr!");
+ return;
+ }
+
wait_queue->EndWait(this, wait_result_);
+
+ // Special case for dummy threads to wakeup if necessary.
+ IfDummyThreadEndWait();
}
}