diff options
author | bunnei <bunneidev@gmail.com> | 2020-12-21 05:57:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-21 05:57:54 +0100 |
commit | 1279c7ce7afd3d1bf2b4e33aa922158acf2cd060 (patch) | |
tree | 6db8088caed2bd957187e4730f51424325038fa5 /src/core/hle/kernel/k_scheduler_lock.h | |
parent | Merge pull request #5201 from ameerj/bufferq-refactor (diff) | |
parent | hle: kernel: Process: Various style fixes based on code review feedback. (diff) | |
download | yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.tar yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.tar.gz yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.tar.bz2 yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.tar.lz yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.tar.xz yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.tar.zst yuzu-1279c7ce7afd3d1bf2b4e33aa922158acf2cd060.zip |
Diffstat (limited to 'src/core/hle/kernel/k_scheduler_lock.h')
-rw-r--r-- | src/core/hle/kernel/k_scheduler_lock.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/core/hle/kernel/k_scheduler_lock.h b/src/core/hle/kernel/k_scheduler_lock.h new file mode 100644 index 000000000..2d675b39e --- /dev/null +++ b/src/core/hle/kernel/k_scheduler_lock.h @@ -0,0 +1,74 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +// This file references various implementation details from Atmosphere, an open-source firmware for +// the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX. + +#pragma once + +#include "common/assert.h" +#include "common/spin_lock.h" +#include "core/hardware_properties.h" + +namespace Kernel { + +class KernelCore; + +template <typename SchedulerType> +class KAbstractSchedulerLock { +public: + explicit KAbstractSchedulerLock(KernelCore& kernel) : kernel{kernel} {} + + bool IsLockedByCurrentThread() const { + return this->owner_thread == kernel.GetCurrentEmuThreadID(); + } + + void Lock() { + if (this->IsLockedByCurrentThread()) { + // If we already own the lock, we can just increment the count. + ASSERT(this->lock_count > 0); + this->lock_count++; + } else { + // Otherwise, we want to disable scheduling and acquire the spinlock. + SchedulerType::DisableScheduling(kernel); + this->spin_lock.lock(); + + // For debug, ensure that our state is valid. + ASSERT(this->lock_count == 0); + ASSERT(this->owner_thread == Core::EmuThreadHandle::InvalidHandle()); + + // Increment count, take ownership. + this->lock_count = 1; + this->owner_thread = kernel.GetCurrentEmuThreadID(); + } + } + + void Unlock() { + ASSERT(this->IsLockedByCurrentThread()); + ASSERT(this->lock_count > 0); + + // Release an instance of the lock. + if ((--this->lock_count) == 0) { + // We're no longer going to hold the lock. Take note of what cores need scheduling. + const u64 cores_needing_scheduling = + SchedulerType::UpdateHighestPriorityThreads(kernel); + Core::EmuThreadHandle leaving_thread = owner_thread; + + // Note that we no longer hold the lock, and unlock the spinlock. + this->owner_thread = Core::EmuThreadHandle::InvalidHandle(); + this->spin_lock.unlock(); + + // Enable scheduling, and perform a rescheduling operation. + SchedulerType::EnableScheduling(kernel, cores_needing_scheduling, leaving_thread); + } + } + +private: + KernelCore& kernel; + Common::SpinLock spin_lock{}; + s32 lock_count{}; + Core::EmuThreadHandle owner_thread{Core::EmuThreadHandle::InvalidHandle()}; +}; + +} // namespace Kernel |