2020-11-28 22:34:07 +01:00
|
|
|
// 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"
|
2020-12-29 20:22:35 +01:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2020-11-28 22:34:07 +01:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class KernelCore;
|
|
|
|
|
|
|
|
template <typename SchedulerType>
|
|
|
|
class KAbstractSchedulerLock {
|
|
|
|
public:
|
2020-12-31 08:05:30 +01:00
|
|
|
explicit KAbstractSchedulerLock(KernelCore& kernel_) : kernel{kernel_} {}
|
2020-11-28 22:34:07 +01:00
|
|
|
|
|
|
|
bool IsLockedByCurrentThread() const {
|
|
|
|
return this->owner_thread == kernel.GetCurrentEmuThreadID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Lock() {
|
|
|
|
if (this->IsLockedByCurrentThread()) {
|
2020-12-05 08:37:35 +01:00
|
|
|
// If we already own the lock, we can just increment the count.
|
2020-11-28 22:34:07 +01:00
|
|
|
ASSERT(this->lock_count > 0);
|
|
|
|
this->lock_count++;
|
|
|
|
} else {
|
2020-12-05 08:37:35 +01:00
|
|
|
// Otherwise, we want to disable scheduling and acquire the spinlock.
|
2020-11-28 22:34:07 +01:00
|
|
|
SchedulerType::DisableScheduling(kernel);
|
|
|
|
this->spin_lock.lock();
|
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// For debug, ensure that our state is valid.
|
2020-11-28 22:34:07 +01:00
|
|
|
ASSERT(this->lock_count == 0);
|
2021-01-20 06:07:07 +01:00
|
|
|
ASSERT(this->owner_thread == EmuThreadHandleInvalid);
|
2020-11-28 22:34:07 +01:00
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Increment count, take ownership.
|
2020-11-28 22:34:07 +01:00
|
|
|
this->lock_count = 1;
|
|
|
|
this->owner_thread = kernel.GetCurrentEmuThreadID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
|
|
|
ASSERT(this->IsLockedByCurrentThread());
|
|
|
|
ASSERT(this->lock_count > 0);
|
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Release an instance of the lock.
|
2020-11-28 22:34:07 +01:00
|
|
|
if ((--this->lock_count) == 0) {
|
2020-12-05 08:37:35 +01:00
|
|
|
// We're no longer going to hold the lock. Take note of what cores need scheduling.
|
2020-11-28 22:34:07 +01:00
|
|
|
const u64 cores_needing_scheduling =
|
|
|
|
SchedulerType::UpdateHighestPriorityThreads(kernel);
|
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Note that we no longer hold the lock, and unlock the spinlock.
|
2021-01-20 06:07:07 +01:00
|
|
|
this->owner_thread = EmuThreadHandleInvalid;
|
2020-11-28 22:34:07 +01:00
|
|
|
this->spin_lock.unlock();
|
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Enable scheduling, and perform a rescheduling operation.
|
2021-01-20 06:07:07 +01:00
|
|
|
SchedulerType::EnableScheduling(kernel, cores_needing_scheduling);
|
2020-11-28 22:34:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-05 09:04:24 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
KernelCore& kernel;
|
|
|
|
Common::SpinLock spin_lock{};
|
|
|
|
s32 lock_count{};
|
2021-01-20 06:07:07 +01:00
|
|
|
EmuThreadHandle owner_thread{EmuThreadHandleInvalid};
|
2020-11-28 22:34:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|