summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/kernel.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2021-01-21 13:00:16 -0800
committerGravatar bunnei2021-01-28 21:42:26 -0800
commit6e953f7f0294d945ba9d6f08350d5dccb0d76075 (patch)
tree92a498827c4de7dd372731eff83c66aa3f57f060 /src/core/hle/kernel/kernel.cpp
parenthle: kernel: k_scheduler: Use atomics for current_thread, etc. (diff)
downloadyuzu-6e953f7f0294d945ba9d6f08350d5dccb0d76075.tar.gz
yuzu-6e953f7f0294d945ba9d6f08350d5dccb0d76075.tar.xz
yuzu-6e953f7f0294d945ba9d6f08350d5dccb0d76075.zip
hle: kernel: Allocate a dummy KThread for each host thread, and use it for scheduling.
Diffstat (limited to 'src/core/hle/kernel/kernel.cpp')
-rw-r--r--src/core/hle/kernel/kernel.cpp27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 6142496b6..093886b7c 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -57,9 +57,10 @@ struct KernelCore::Impl {
57 } 57 }
58 58
59 void Initialize(KernelCore& kernel) { 59 void Initialize(KernelCore& kernel) {
60 global_scheduler_context = std::make_unique<Kernel::GlobalSchedulerContext>(kernel);
61
60 RegisterHostThread(); 62 RegisterHostThread();
61 63
62 global_scheduler_context = std::make_unique<Kernel::GlobalSchedulerContext>(kernel);
63 service_thread_manager = 64 service_thread_manager =
64 std::make_unique<Common::ThreadWorker>(1, "yuzu:ServiceThreadManager"); 65 std::make_unique<Common::ThreadWorker>(1, "yuzu:ServiceThreadManager");
65 is_phantom_mode_for_singlecore = false; 66 is_phantom_mode_for_singlecore = false;
@@ -206,6 +207,18 @@ struct KernelCore::Impl {
206 return host_thread_id; 207 return host_thread_id;
207 } 208 }
208 209
210 // Gets the dummy KThread for the caller, allocating a new one if this is the first time
211 KThread* GetHostDummyThread() {
212 const thread_local auto thread =
213 KThread::Create(
214 system, ThreadType::Main,
215 std::string{"DummyThread:" + GetHostThreadId()}, 0, KThread::DefaultThreadPriority,
216 0, static_cast<u32>(3), 0, nullptr,
217 []([[maybe_unused]] void* arg) { UNREACHABLE(); }, nullptr)
218 .Unwrap();
219 return thread.get();
220 }
221
209 /// Registers a CPU core thread by allocating a host thread ID for it 222 /// Registers a CPU core thread by allocating a host thread ID for it
210 void RegisterCoreThread(std::size_t core_id) { 223 void RegisterCoreThread(std::size_t core_id) {
211 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); 224 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
@@ -218,6 +231,7 @@ struct KernelCore::Impl {
218 /// Registers a new host thread by allocating a host thread ID for it 231 /// Registers a new host thread by allocating a host thread ID for it
219 void RegisterHostThread() { 232 void RegisterHostThread() {
220 [[maybe_unused]] const auto this_id = GetHostThreadId(); 233 [[maybe_unused]] const auto this_id = GetHostThreadId();
234 [[maybe_unused]] const auto dummy_thread = GetHostDummyThread();
221 } 235 }
222 236
223 [[nodiscard]] u32 GetCurrentHostThreadID() { 237 [[nodiscard]] u32 GetCurrentHostThreadID() {
@@ -237,13 +251,12 @@ struct KernelCore::Impl {
237 is_phantom_mode_for_singlecore = value; 251 is_phantom_mode_for_singlecore = value;
238 } 252 }
239 253
240 [[nodiscard]] EmuThreadHandle GetCurrentEmuThreadID() { 254 KThread* GetCurrentEmuThread() {
241 const auto thread_id = GetCurrentHostThreadID(); 255 const auto thread_id = GetCurrentHostThreadID();
242 if (thread_id >= Core::Hardware::NUM_CPU_CORES) { 256 if (thread_id >= Core::Hardware::NUM_CPU_CORES) {
243 // Reserved value for HLE threads 257 return GetHostDummyThread();
244 return EmuThreadHandleReserved + (static_cast<u64>(thread_id) << 1);
245 } 258 }
246 return reinterpret_cast<uintptr_t>(schedulers[thread_id].get()); 259 return schedulers[thread_id]->GetCurrentThread();
247 } 260 }
248 261
249 void InitializeMemoryLayout() { 262 void InitializeMemoryLayout() {
@@ -548,8 +561,8 @@ u32 KernelCore::GetCurrentHostThreadID() const {
548 return impl->GetCurrentHostThreadID(); 561 return impl->GetCurrentHostThreadID();
549} 562}
550 563
551EmuThreadHandle KernelCore::GetCurrentEmuThreadID() const { 564KThread* KernelCore::GetCurrentEmuThread() const {
552 return impl->GetCurrentEmuThreadID(); 565 return impl->GetCurrentEmuThread();
553} 566}
554 567
555Memory::MemoryManager& KernelCore::MemoryManager() { 568Memory::MemoryManager& KernelCore::MemoryManager() {