kernel: k_light_lock: Simplify EmuThreadHandle implementation.

This commit is contained in:
bunnei 2021-01-19 21:07:07 -08:00
parent c0f5830323
commit 1f99f5473c
5 changed files with 33 additions and 51 deletions

View file

@ -4,8 +4,10 @@
#pragma once #pragma once
#include <array>
#include <tuple> #include <tuple>
#include "common/bit_util.h"
#include "common/common_types.h" #include "common/common_types.h"
namespace Core { namespace Core {
@ -18,34 +20,12 @@ constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch cpu frequency is 1020MHz u
constexpr u64 CNTFREQ = 19200000; // Switch's hardware clock speed constexpr u64 CNTFREQ = 19200000; // Switch's hardware clock speed
constexpr u32 NUM_CPU_CORES = 4; // Number of CPU Cores constexpr u32 NUM_CPU_CORES = 4; // Number of CPU Cores
} // namespace Hardware // Virtual to Physical core map.
constexpr std::array<s32, Common::BitSize<u64>()> VirtualToPhysicalCoreMap{
constexpr u32 INVALID_HOST_THREAD_ID = 0xFFFFFFFF; 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
struct EmuThreadHandle {
u32 host_handle;
u32 guest_handle;
u64 GetRaw() const {
return (static_cast<u64>(host_handle) << 32) | guest_handle;
}
bool operator==(const EmuThreadHandle& rhs) const {
return std::tie(host_handle, guest_handle) == std::tie(rhs.host_handle, rhs.guest_handle);
}
bool operator!=(const EmuThreadHandle& rhs) const {
return !operator==(rhs);
}
static constexpr EmuThreadHandle InvalidHandle() {
constexpr u32 invalid_handle = 0xFFFFFFFF;
return {invalid_handle, invalid_handle};
}
bool IsInvalid() const {
return (*this) == InvalidHandle();
}
}; };
} // namespace Hardware
} // namespace Core } // namespace Core

View file

@ -9,6 +9,12 @@
namespace Kernel { namespace Kernel {
static KThread* ToThread(uintptr_t thread_) {
ASSERT((thread_ & EmuThreadHandleReserved) == 0);
ASSERT((thread_ & 1) == 0);
return reinterpret_cast<KThread*>(thread_);
}
void KLightLock::Lock() { void KLightLock::Lock() {
const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer(kernel)); const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer(kernel));
const uintptr_t cur_thread_tag = (cur_thread | 1); const uintptr_t cur_thread_tag = (cur_thread | 1);
@ -42,7 +48,7 @@ void KLightLock::Unlock() {
} }
void KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) { void KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) {
KThread* cur_thread = reinterpret_cast<KThread*>(_cur_thread); KThread* cur_thread = ToThread(_cur_thread);
// Pend the current thread waiting on the owner thread. // Pend the current thread waiting on the owner thread.
{ {
@ -54,7 +60,7 @@ void KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) {
} }
// Add the current thread as a waiter on the owner. // Add the current thread as a waiter on the owner.
KThread* owner_thread = reinterpret_cast<KThread*>(_owner & ~1ul); KThread* owner_thread = ToThread(_owner & ~1ul);
cur_thread->SetAddressKey(reinterpret_cast<uintptr_t>(std::addressof(tag))); cur_thread->SetAddressKey(reinterpret_cast<uintptr_t>(std::addressof(tag)));
owner_thread->AddWaiter(cur_thread); owner_thread->AddWaiter(cur_thread);
@ -82,7 +88,7 @@ void KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) {
} }
void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) { void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) {
KThread* owner_thread = reinterpret_cast<KThread*>(_cur_thread); KThread* owner_thread = ToThread(_cur_thread);
// Unlock. // Unlock.
{ {

View file

@ -37,7 +37,7 @@ public:
// For debug, ensure that our state is valid. // For debug, ensure that our state is valid.
ASSERT(this->lock_count == 0); ASSERT(this->lock_count == 0);
ASSERT(this->owner_thread == Core::EmuThreadHandle::InvalidHandle()); ASSERT(this->owner_thread == EmuThreadHandleInvalid);
// Increment count, take ownership. // Increment count, take ownership.
this->lock_count = 1; this->lock_count = 1;
@ -54,14 +54,13 @@ public:
// We're no longer going to hold the lock. Take note of what cores need scheduling. // We're no longer going to hold the lock. Take note of what cores need scheduling.
const u64 cores_needing_scheduling = const u64 cores_needing_scheduling =
SchedulerType::UpdateHighestPriorityThreads(kernel); SchedulerType::UpdateHighestPriorityThreads(kernel);
Core::EmuThreadHandle leaving_thread = owner_thread;
// Note that we no longer hold the lock, and unlock the spinlock. // Note that we no longer hold the lock, and unlock the spinlock.
this->owner_thread = Core::EmuThreadHandle::InvalidHandle(); this->owner_thread = EmuThreadHandleInvalid;
this->spin_lock.unlock(); this->spin_lock.unlock();
// Enable scheduling, and perform a rescheduling operation. // Enable scheduling, and perform a rescheduling operation.
SchedulerType::EnableScheduling(kernel, cores_needing_scheduling, leaving_thread); SchedulerType::EnableScheduling(kernel, cores_needing_scheduling);
} }
} }
@ -69,7 +68,7 @@ private:
KernelCore& kernel; KernelCore& kernel;
Common::SpinLock spin_lock{}; Common::SpinLock spin_lock{};
s32 lock_count{}; s32 lock_count{};
Core::EmuThreadHandle owner_thread{Core::EmuThreadHandle::InvalidHandle()}; EmuThreadHandle owner_thread{EmuThreadHandleInvalid};
}; };
} // namespace Kernel } // namespace Kernel

View file

@ -237,20 +237,13 @@ struct KernelCore::Impl {
is_phantom_mode_for_singlecore = value; is_phantom_mode_for_singlecore = value;
} }
[[nodiscard]] Core::EmuThreadHandle GetCurrentEmuThreadID() { [[nodiscard]] EmuThreadHandle GetCurrentEmuThreadID() {
Core::EmuThreadHandle result = Core::EmuThreadHandle::InvalidHandle(); const auto thread_id = GetCurrentHostThreadID();
result.host_handle = GetCurrentHostThreadID(); if (thread_id >= Core::Hardware::NUM_CPU_CORES) {
if (result.host_handle >= Core::Hardware::NUM_CPU_CORES) { // Reserved value for HLE threads
return result; return EmuThreadHandleReserved + (static_cast<u64>(thread_id) << 1);
} }
const Kernel::KScheduler& sched = cores[result.host_handle].Scheduler(); return reinterpret_cast<uintptr_t>(schedulers[thread_id].get());
const Kernel::KThread* current = sched.GetCurrentThread();
if (current != nullptr && !IsPhantomModeForSingleCore()) {
result.guest_handle = current->GetGlobalHandle();
} else {
result.guest_handle = InvalidHandle;
}
return result;
} }
void InitializeMemoryLayout() { void InitializeMemoryLayout() {
@ -555,7 +548,7 @@ u32 KernelCore::GetCurrentHostThreadID() const {
return impl->GetCurrentHostThreadID(); return impl->GetCurrentHostThreadID();
} }
Core::EmuThreadHandle KernelCore::GetCurrentEmuThreadID() const { EmuThreadHandle KernelCore::GetCurrentEmuThreadID() const {
return impl->GetCurrentEmuThreadID(); return impl->GetCurrentEmuThreadID();
} }

View file

@ -46,6 +46,10 @@ class Synchronization;
class KThread; class KThread;
class TimeManager; class TimeManager;
using EmuThreadHandle = uintptr_t;
constexpr EmuThreadHandle EmuThreadHandleInvalid{};
constexpr EmuThreadHandle EmuThreadHandleReserved{1ULL << 63};
/// Represents a single instance of the kernel. /// Represents a single instance of the kernel.
class KernelCore { class KernelCore {
private: private:
@ -162,7 +166,7 @@ public:
bool IsValidNamedPort(NamedPortTable::const_iterator port) const; bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
/// Gets the current host_thread/guest_thread handle. /// Gets the current host_thread/guest_thread handle.
Core::EmuThreadHandle GetCurrentEmuThreadID() const; EmuThreadHandle GetCurrentEmuThreadID() const;
/// Gets the current host_thread handle. /// Gets the current host_thread handle.
u32 GetCurrentHostThreadID() const; u32 GetCurrentHostThreadID() const;