kern: add abstract scoped lock template

This commit is contained in:
Michael Scire 2020-01-31 16:37:58 -08:00
parent 57222e8301
commit 2355047715
5 changed files with 53 additions and 57 deletions

View file

@ -17,6 +17,7 @@
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_select_cpu.hpp>
#include <mesosphere/kern_k_current_context.hpp>
#include <mesosphere/kern_k_scoped_lock.hpp>
namespace ams::kern {
@ -60,18 +61,6 @@ namespace ams::kern {
void UnlockSlowPath(uintptr_t cur_thread);
};
class KScopedLightLock {
private:
KLightLock *lock;
public:
explicit ALWAYS_INLINE KScopedLightLock(KLightLock *l) : lock(l) {
this->lock->Lock();
}
ALWAYS_INLINE ~KScopedLightLock() {
this->lock->Unlock();
}
explicit ALWAYS_INLINE KScopedLightLock(KLightLock &l) : KScopedLightLock(std::addressof(l)) { /* ... */ }
};
using KScopedLightLock = KScopedLock<KLightLock>;
}

View file

@ -63,10 +63,8 @@ namespace ams::kern {
static void EnableSchedulingAndSchedule(u64 cores_needing_scheduling);
};
class KScopedSchedulerLock {
public:
ALWAYS_INLINE KScopedSchedulerLock() { KScheduler::s_scheduler_lock.Lock(); }
ALWAYS_INLINE ~KScopedSchedulerLock() { KScheduler::s_scheduler_lock.Unlock(); }
class KScopedSchedulerLock : KScopedLock<KScheduler::LockType> {
explicit ALWAYS_INLINE KScopedSchedulerLock() : KScopedLock(KScheduler::s_scheduler_lock) { /* ... */ }
};
}

View file

@ -17,6 +17,7 @@
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_spin_lock.hpp>
#include <mesosphere/kern_k_current_context.hpp>
#include <mesosphere/kern_k_scoped_lock.hpp>
namespace ams::kern {

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
namespace ams::kern {
/*
TODO: C++20
template<typename T>
concept KLockable = !std::is_reference<T>::value && requires (T &t) {
{ t.Lock() } -> std::same_as<void>;
{ t.Unlock() } -> std::same_as<void>;
};
*/
template<typename T> /* TODO C++20: requires KLockable<T> */
class KScopedLock {
NON_COPYABLE(KScopedLock);
NON_MOVEABLE(KScopedLock);
private:
T *lock_ptr;
public:
explicit ALWAYS_INLINE KScopedLock(T *l) : lock_ptr(l) { this->lock_ptr->Lock(); }
explicit ALWAYS_INLINE KScopedLock(T &l) : KScopedLock(std::addressof(l)) { /* ... */}
ALWAYS_INLINE ~KScopedLock() { this->lock_ptr->Unlock(); }
};
}

View file

@ -15,6 +15,7 @@
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_scoped_lock.hpp>
#if defined(ATMOSPHERE_ARCH_ARM64)
@ -34,45 +35,8 @@
namespace ams::kern {
class KScopedSpinLock {
private:
KSpinLock *lock_ptr;
public:
explicit ALWAYS_INLINE KScopedSpinLock(KSpinLock *l) : lock_ptr(l) {
this->lock_ptr->Lock();
}
ALWAYS_INLINE ~KScopedSpinLock() {
this->lock_ptr->Unlock();
}
explicit ALWAYS_INLINE KScopedSpinLock(KSpinLock &l) : KScopedSpinLock(std::addressof(l)) { /* ... */ }
};
class KScopedAlignedSpinLock {
private:
KAlignedSpinLock *lock_ptr;
public:
explicit ALWAYS_INLINE KScopedAlignedSpinLock(KAlignedSpinLock *l) : lock_ptr(l) {
this->lock_ptr->Lock();
}
ALWAYS_INLINE ~KScopedAlignedSpinLock() {
this->lock_ptr->Unlock();
}
explicit ALWAYS_INLINE KScopedAlignedSpinLock(KAlignedSpinLock &l) : KScopedAlignedSpinLock(std::addressof(l)) { /* ... */ }
};
class KScopedNotAlignedSpinLock {
private:
KNotAlignedSpinLock *lock_ptr;
public:
explicit ALWAYS_INLINE KScopedNotAlignedSpinLock(KNotAlignedSpinLock *l) : lock_ptr(l) {
this->lock_ptr->Lock();
}
ALWAYS_INLINE ~KScopedNotAlignedSpinLock() {
this->lock_ptr->Unlock();
}
explicit ALWAYS_INLINE KScopedNotAlignedSpinLock(KNotAlignedSpinLock &l) : KScopedNotAlignedSpinLock(std::addressof(l)) { /* ... */ }
};
using KScopedSpinLock = KScopedLock<KSpinLock>;
using KScopedAlignedSpinLock = KScopedLock<KAlignedSpinLock>;
using KScopedNotAlignedSpinLock = KScopedLock<KNotAlignedSpinLock>;
}