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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "core/hardware_properties.h"
|
2021-02-13 10:29:32 +01:00
|
|
|
#include "core/hle/kernel/k_spin_lock.h"
|
2021-01-21 22:00:16 +01:00
|
|
|
#include "core/hle/kernel/k_thread.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 {
|
2021-01-25 07:53:01 +01:00
|
|
|
return owner_thread == GetCurrentThreadPointer(kernel);
|
2020-11-28 22:34:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Lock() {
|
2021-01-25 07:53:01 +01:00
|
|
|
if (IsLockedByCurrentThread()) {
|
2020-12-05 08:37:35 +01:00
|
|
|
// If we already own the lock, we can just increment the count.
|
2021-01-21 22:00:16 +01:00
|
|
|
ASSERT(lock_count > 0);
|
|
|
|
lock_count++;
|
2020-11-28 22:34:07 +01:00
|
|
|
} 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);
|
2021-02-13 10:29:32 +01:00
|
|
|
spin_lock.Lock();
|
2020-11-28 22:34:07 +01:00
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// For debug, ensure that our state is valid.
|
2021-01-21 22:00:16 +01:00
|
|
|
ASSERT(lock_count == 0);
|
|
|
|
ASSERT(owner_thread == nullptr);
|
2020-11-28 22:34:07 +01:00
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Increment count, take ownership.
|
2021-01-21 22:00:16 +01:00
|
|
|
lock_count = 1;
|
|
|
|
owner_thread = GetCurrentThreadPointer(kernel);
|
2020-11-28 22:34:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
2021-01-25 07:53:01 +01:00
|
|
|
ASSERT(IsLockedByCurrentThread());
|
2021-01-21 22:00:16 +01:00
|
|
|
ASSERT(lock_count > 0);
|
2020-11-28 22:34:07 +01:00
|
|
|
|
2020-12-05 08:37:35 +01:00
|
|
|
// Release an instance of the lock.
|
2021-01-21 22:00:16 +01:00
|
|
|
if ((--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-21 22:00:16 +01:00
|
|
|
owner_thread = nullptr;
|
2021-02-13 10:29:32 +01:00
|
|
|
spin_lock.Unlock();
|
2020-11-28 22:34:07 +01:00
|
|
|
|
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;
|
2021-02-13 10:29:32 +01:00
|
|
|
KAlignedSpinLock spin_lock{};
|
2020-12-05 09:04:24 +01:00
|
|
|
s32 lock_count{};
|
2021-01-21 22:00:16 +01:00
|
|
|
KThread* owner_thread{};
|
2020-11-28 22:34:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Kernel
|