summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/kernel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel/kernel.cpp')
-rw-r--r--src/core/hle/kernel/kernel.cpp45
1 files changed, 18 insertions, 27 deletions
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index d9eafe261..ece20a643 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -367,23 +367,21 @@ struct KernelCore::Impl {
367 current_process = process; 367 current_process = process;
368 } 368 }
369 369
370 static inline thread_local u32 host_thread_id = UINT32_MAX; 370 static inline thread_local u8 host_thread_id = UINT8_MAX;
371 371
372 /// Gets the host thread ID for the caller, allocating a new one if this is the first time 372 /// Sets the host thread ID for the caller.
373 u32 GetHostThreadId(std::size_t core_id) { 373 u32 SetHostThreadId(std::size_t core_id) {
374 if (host_thread_id == UINT32_MAX) { 374 // This should only be called during core init.
375 // The first four slots are reserved for CPU core threads 375 ASSERT(host_thread_id == UINT8_MAX);
376 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); 376
377 host_thread_id = static_cast<u32>(core_id); 377 // The first four slots are reserved for CPU core threads
378 } 378 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
379 host_thread_id = static_cast<u8>(core_id);
379 return host_thread_id; 380 return host_thread_id;
380 } 381 }
381 382
382 /// Gets the host thread ID for the caller, allocating a new one if this is the first time 383 /// Gets the host thread ID for the caller
383 u32 GetHostThreadId() { 384 u32 GetHostThreadId() const {
384 if (host_thread_id == UINT32_MAX) {
385 host_thread_id = next_host_thread_id++;
386 }
387 return host_thread_id; 385 return host_thread_id;
388 } 386 }
389 387
@@ -391,23 +389,19 @@ struct KernelCore::Impl {
391 KThread* GetHostDummyThread(KThread* existing_thread) { 389 KThread* GetHostDummyThread(KThread* existing_thread) {
392 auto initialize = [this](KThread* thread) { 390 auto initialize = [this](KThread* thread) {
393 ASSERT(KThread::InitializeDummyThread(thread, nullptr).IsSuccess()); 391 ASSERT(KThread::InitializeDummyThread(thread, nullptr).IsSuccess());
394 thread->SetName(fmt::format("DummyThread:{}", GetHostThreadId())); 392 thread->SetName(fmt::format("DummyThread:{}", next_host_thread_id++));
395 return thread; 393 return thread;
396 }; 394 };
397 395
398 thread_local KThread raw_thread{system.Kernel()}; 396 thread_local KThread raw_thread{system.Kernel()};
399 thread_local KThread* thread = nullptr; 397 thread_local KThread* thread = existing_thread ? existing_thread : initialize(&raw_thread);
400 if (thread == nullptr) {
401 thread = (existing_thread == nullptr) ? initialize(&raw_thread) : existing_thread;
402 }
403
404 return thread; 398 return thread;
405 } 399 }
406 400
407 /// Registers a CPU core thread by allocating a host thread ID for it 401 /// Registers a CPU core thread by allocating a host thread ID for it
408 void RegisterCoreThread(std::size_t core_id) { 402 void RegisterCoreThread(std::size_t core_id) {
409 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); 403 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
410 const auto this_id = GetHostThreadId(core_id); 404 const auto this_id = SetHostThreadId(core_id);
411 if (!is_multicore) { 405 if (!is_multicore) {
412 single_core_thread_id = this_id; 406 single_core_thread_id = this_id;
413 } 407 }
@@ -415,7 +409,6 @@ struct KernelCore::Impl {
415 409
416 /// Registers a new host thread by allocating a host thread ID for it 410 /// Registers a new host thread by allocating a host thread ID for it
417 void RegisterHostThread(KThread* existing_thread) { 411 void RegisterHostThread(KThread* existing_thread) {
418 [[maybe_unused]] const auto this_id = GetHostThreadId();
419 [[maybe_unused]] const auto dummy_thread = GetHostDummyThread(existing_thread); 412 [[maybe_unused]] const auto dummy_thread = GetHostDummyThread(existing_thread);
420 } 413 }
421 414
@@ -445,11 +438,9 @@ struct KernelCore::Impl {
445 static inline thread_local KThread* current_thread{nullptr}; 438 static inline thread_local KThread* current_thread{nullptr};
446 439
447 KThread* GetCurrentEmuThread() { 440 KThread* GetCurrentEmuThread() {
448 const auto thread_id = GetCurrentHostThreadID(); 441 if (!current_thread) {
449 if (thread_id >= Core::Hardware::NUM_CPU_CORES) { 442 current_thread = GetHostDummyThread(nullptr);
450 return GetHostDummyThread(nullptr);
451 } 443 }
452
453 return current_thread; 444 return current_thread;
454 } 445 }
455 446
@@ -1002,7 +993,7 @@ const Kernel::PhysicalCore& KernelCore::CurrentPhysicalCore() const {
1002} 993}
1003 994
1004Kernel::KScheduler* KernelCore::CurrentScheduler() { 995Kernel::KScheduler* KernelCore::CurrentScheduler() {
1005 u32 core_id = impl->GetCurrentHostThreadID(); 996 const u32 core_id = impl->GetCurrentHostThreadID();
1006 if (core_id >= Core::Hardware::NUM_CPU_CORES) { 997 if (core_id >= Core::Hardware::NUM_CPU_CORES) {
1007 // This is expected when called from not a guest thread 998 // This is expected when called from not a guest thread
1008 return {}; 999 return {};