From 3856564727e3efcf85fb0c1e5431cab22d818370 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 24 Jan 2021 22:54:37 -0800 Subject: [PATCH] hle: kernel: process: Add state lock. --- src/core/hle/kernel/process.cpp | 4 ++-- src/core/hle/kernel/process.h | 6 ++++++ src/core/hle/kernel/svc.cpp | 11 +++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 9f4583b496..0edbfc4cca 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -138,7 +138,7 @@ std::shared_ptr Process::GetResourceLimit() const { void Process::IncrementThreadCount() { ASSERT(num_threads >= 0); - ++num_created_threads; + num_created_threads++; if (const auto count = ++num_threads; count > peak_num_threads) { peak_num_threads = count; @@ -443,7 +443,7 @@ bool Process::IsSignaled() const { Process::Process(Core::System& system) : KSynchronizationObject{system.Kernel()}, page_table{std::make_unique(system)}, handle_table{system.Kernel()}, - address_arbiter{system}, condition_var{system}, system{system} {} + address_arbiter{system}, condition_var{system}, state_lock{system.Kernel()}, system{system} {} Process::~Process() = default; diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 11d78f3a8d..26e647743e 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -348,6 +348,10 @@ public: void PinCurrentThread(); void UnpinCurrentThread(); + KLightLock& GetStateLock() { + return state_lock; + } + /////////////////////////////////////////////////////////////////////////////////////////////// // Thread-local storage management @@ -472,6 +476,8 @@ private: KThread* exception_thread{}; + KLightLock state_lock; + /// System context Core::System& system; }; diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index dbef854f8f..7fd514e9d3 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -1450,11 +1450,14 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e Svc::ResultInvalidPriority); R_UNLESS(process.CheckThreadPriority(priority), Svc::ResultInvalidPriority); - ASSERT(kernel.CurrentProcess()->GetResourceLimit()->Reserve(ResourceType::Threads, 1)); + ASSERT(process.GetResourceLimit()->Reserve(ResourceType::Threads, 1)); - CASCADE_RESULT(std::shared_ptr thread, - KThread::Create(system, ThreadType::User, "", entry_point, priority, arg, - core_id, stack_bottom, &process)); + std::shared_ptr thread; + { + KScopedLightLock lk{process.GetStateLock()}; + CASCADE_RESULT(thread, KThread::Create(system, ThreadType::User, "", entry_point, priority, + arg, core_id, stack_bottom, &process)); + } const auto new_thread_handle = process.GetHandleTable().Create(thread); if (new_thread_handle.Failed()) {