diff options
| author | 2023-02-19 13:12:43 -0500 | |
|---|---|---|
| committer | 2023-02-19 13:12:43 -0500 | |
| commit | 898c5d35a58b555301ad1131e52870fbabe8802c (patch) | |
| tree | 27667f7f91cbc81dcf0970ae09554196b5cb9633 /src/core/hle/kernel/kernel.cpp | |
| parent | Merge pull request #9588 from liamwhite/bylaws-reverts (diff) | |
| parent | kernel: Refactor thread_local variable usage (diff) | |
| download | yuzu-898c5d35a58b555301ad1131e52870fbabe8802c.tar.gz yuzu-898c5d35a58b555301ad1131e52870fbabe8802c.tar.xz yuzu-898c5d35a58b555301ad1131e52870fbabe8802c.zip | |
Merge pull request #9771 from ameerj/host-thread-id
kernel: Refactor thread_local variable usage
Diffstat (limited to 'src/core/hle/kernel/kernel.cpp')
| -rw-r--r-- | src/core/hle/kernel/kernel.cpp | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 3a68a5633..2ff253183 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp | |||
| @@ -375,23 +375,21 @@ struct KernelCore::Impl { | |||
| 375 | application_process = process; | 375 | application_process = process; |
| 376 | } | 376 | } |
| 377 | 377 | ||
| 378 | static inline thread_local u32 host_thread_id = UINT32_MAX; | 378 | static inline thread_local u8 host_thread_id = UINT8_MAX; |
| 379 | 379 | ||
| 380 | /// Gets the host thread ID for the caller, allocating a new one if this is the first time | 380 | /// Sets the host thread ID for the caller. |
| 381 | u32 GetHostThreadId(std::size_t core_id) { | 381 | u32 SetHostThreadId(std::size_t core_id) { |
| 382 | if (host_thread_id == UINT32_MAX) { | 382 | // This should only be called during core init. |
| 383 | // The first four slots are reserved for CPU core threads | 383 | ASSERT(host_thread_id == UINT8_MAX); |
| 384 | ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); | 384 | |
| 385 | host_thread_id = static_cast<u32>(core_id); | 385 | // The first four slots are reserved for CPU core threads |
| 386 | } | 386 | ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); |
| 387 | host_thread_id = static_cast<u8>(core_id); | ||
| 387 | return host_thread_id; | 388 | return host_thread_id; |
| 388 | } | 389 | } |
| 389 | 390 | ||
| 390 | /// Gets the host thread ID for the caller, allocating a new one if this is the first time | 391 | /// Gets the host thread ID for the caller |
| 391 | u32 GetHostThreadId() { | 392 | u32 GetHostThreadId() const { |
| 392 | if (host_thread_id == UINT32_MAX) { | ||
| 393 | host_thread_id = next_host_thread_id++; | ||
| 394 | } | ||
| 395 | return host_thread_id; | 393 | return host_thread_id; |
| 396 | } | 394 | } |
| 397 | 395 | ||
| @@ -399,23 +397,19 @@ struct KernelCore::Impl { | |||
| 399 | KThread* GetHostDummyThread(KThread* existing_thread) { | 397 | KThread* GetHostDummyThread(KThread* existing_thread) { |
| 400 | auto initialize = [this](KThread* thread) { | 398 | auto initialize = [this](KThread* thread) { |
| 401 | ASSERT(KThread::InitializeDummyThread(thread, nullptr).IsSuccess()); | 399 | ASSERT(KThread::InitializeDummyThread(thread, nullptr).IsSuccess()); |
| 402 | thread->SetName(fmt::format("DummyThread:{}", GetHostThreadId())); | 400 | thread->SetName(fmt::format("DummyThread:{}", next_host_thread_id++)); |
| 403 | return thread; | 401 | return thread; |
| 404 | }; | 402 | }; |
| 405 | 403 | ||
| 406 | thread_local KThread raw_thread{system.Kernel()}; | 404 | thread_local KThread raw_thread{system.Kernel()}; |
| 407 | thread_local KThread* thread = nullptr; | 405 | thread_local KThread* thread = existing_thread ? existing_thread : initialize(&raw_thread); |
| 408 | if (thread == nullptr) { | ||
| 409 | thread = (existing_thread == nullptr) ? initialize(&raw_thread) : existing_thread; | ||
| 410 | } | ||
| 411 | |||
| 412 | return thread; | 406 | return thread; |
| 413 | } | 407 | } |
| 414 | 408 | ||
| 415 | /// Registers a CPU core thread by allocating a host thread ID for it | 409 | /// Registers a CPU core thread by allocating a host thread ID for it |
| 416 | void RegisterCoreThread(std::size_t core_id) { | 410 | void RegisterCoreThread(std::size_t core_id) { |
| 417 | ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); | 411 | ASSERT(core_id < Core::Hardware::NUM_CPU_CORES); |
| 418 | const auto this_id = GetHostThreadId(core_id); | 412 | const auto this_id = SetHostThreadId(core_id); |
| 419 | if (!is_multicore) { | 413 | if (!is_multicore) { |
| 420 | single_core_thread_id = this_id; | 414 | single_core_thread_id = this_id; |
| 421 | } | 415 | } |
| @@ -423,7 +417,6 @@ struct KernelCore::Impl { | |||
| 423 | 417 | ||
| 424 | /// Registers a new host thread by allocating a host thread ID for it | 418 | /// Registers a new host thread by allocating a host thread ID for it |
| 425 | void RegisterHostThread(KThread* existing_thread) { | 419 | void RegisterHostThread(KThread* existing_thread) { |
| 426 | [[maybe_unused]] const auto this_id = GetHostThreadId(); | ||
| 427 | [[maybe_unused]] const auto dummy_thread = GetHostDummyThread(existing_thread); | 420 | [[maybe_unused]] const auto dummy_thread = GetHostDummyThread(existing_thread); |
| 428 | } | 421 | } |
| 429 | 422 | ||
| @@ -453,11 +446,9 @@ struct KernelCore::Impl { | |||
| 453 | static inline thread_local KThread* current_thread{nullptr}; | 446 | static inline thread_local KThread* current_thread{nullptr}; |
| 454 | 447 | ||
| 455 | KThread* GetCurrentEmuThread() { | 448 | KThread* GetCurrentEmuThread() { |
| 456 | const auto thread_id = GetCurrentHostThreadID(); | 449 | if (!current_thread) { |
| 457 | if (thread_id >= Core::Hardware::NUM_CPU_CORES) { | 450 | current_thread = GetHostDummyThread(nullptr); |
| 458 | return GetHostDummyThread(nullptr); | ||
| 459 | } | 451 | } |
| 460 | |||
| 461 | return current_thread; | 452 | return current_thread; |
| 462 | } | 453 | } |
| 463 | 454 | ||
| @@ -1012,7 +1003,7 @@ const Kernel::PhysicalCore& KernelCore::CurrentPhysicalCore() const { | |||
| 1012 | } | 1003 | } |
| 1013 | 1004 | ||
| 1014 | Kernel::KScheduler* KernelCore::CurrentScheduler() { | 1005 | Kernel::KScheduler* KernelCore::CurrentScheduler() { |
| 1015 | u32 core_id = impl->GetCurrentHostThreadID(); | 1006 | const u32 core_id = impl->GetCurrentHostThreadID(); |
| 1016 | if (core_id >= Core::Hardware::NUM_CPU_CORES) { | 1007 | if (core_id >= Core::Hardware::NUM_CPU_CORES) { |
| 1017 | // This is expected when called from not a guest thread | 1008 | // This is expected when called from not a guest thread |
| 1018 | return {}; | 1009 | return {}; |