1
0
Fork 1
forked from suyu/suyu

hle: kernel: Port KAbstractSchedulerLock from Mesosphere.

This commit is contained in:
bunnei 2020-11-28 13:34:07 -08:00
parent 39d356782e
commit 7e5d0f1fe3
2 changed files with 77 additions and 0 deletions

View file

@ -154,6 +154,7 @@ add_library(core STATIC
hle/kernel/hle_ipc.h
hle/kernel/k_affinity_mask.h
hle/kernel/k_priority_queue.h
hle/kernel/k_scheduler_lock.h
hle/kernel/kernel.cpp
hle/kernel/kernel.h
hle/kernel/memory/address_space_info.cpp

View file

@ -0,0 +1,76 @@
// 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 {
private:
KernelCore& kernel;
Common::SpinLock spin_lock;
s32 lock_count;
Core::EmuThreadHandle owner_thread;
public:
KAbstractSchedulerLock(KernelCore& kernel)
: kernel{kernel}, spin_lock(), lock_count(0),
owner_thread(Core::EmuThreadHandle::InvalidHandle()) {}
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);
}
}
};
} // namespace Kernel