2020-02-01 01:25:17 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-01 01:25:17 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <mesosphere/kern_k_spin_lock.hpp>
|
|
|
|
#include <mesosphere/kern_k_current_context.hpp>
|
2020-02-01 01:37:58 +01:00
|
|
|
#include <mesosphere/kern_k_scoped_lock.hpp>
|
2020-02-01 01:25:17 +01:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KThread;
|
|
|
|
|
2020-05-12 00:02:10 +02:00
|
|
|
template<typename T>
|
|
|
|
concept KSchedulerLockable = !std::is_reference<T>::value && requires(T) {
|
|
|
|
{ T::DisableScheduling() } -> std::same_as<void>;
|
|
|
|
{ T::EnableScheduling(std::declval<u64>()) } -> std::same_as<void>;
|
|
|
|
{ T::UpdateHighestPriorityThreads() } -> std::convertible_to<u64>;
|
|
|
|
};
|
2020-02-01 01:25:17 +01:00
|
|
|
|
2020-05-12 00:02:10 +02:00
|
|
|
template<typename SchedulerType> requires KSchedulerLockable<SchedulerType>
|
2020-02-01 01:25:17 +01:00
|
|
|
class KAbstractSchedulerLock {
|
|
|
|
private:
|
2020-12-18 02:18:47 +01:00
|
|
|
KAlignedSpinLock m_spin_lock;
|
|
|
|
s32 m_lock_count;
|
|
|
|
KThread *m_owner_thread;
|
2020-02-01 01:25:17 +01:00
|
|
|
public:
|
2020-12-18 02:18:47 +01:00
|
|
|
constexpr ALWAYS_INLINE KAbstractSchedulerLock() : m_spin_lock(), m_lock_count(0), m_owner_thread(nullptr) { MESOSPHERE_ASSERT_THIS(); }
|
2020-02-01 01:25:17 +01:00
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsLockedByCurrentThread() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
return m_owner_thread == GetCurrentThreadPointer();
|
2020-02-01 01:25:17 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:58:39 +01:00
|
|
|
MESOSPHERE_ALWAYS_INLINE_IF_RELEASE void Lock() {
|
2020-02-01 01:25:17 +01:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
if (this->IsLockedByCurrentThread()) {
|
2023-02-21 16:08:08 +01:00
|
|
|
/* If we already own the lock, the lock count should be > 0. */
|
|
|
|
/* For debug, ensure this is true. */
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(m_lock_count > 0);
|
2020-02-01 01:25:17 +01:00
|
|
|
} else {
|
|
|
|
/* Otherwise, we want to disable scheduling and acquire the spinlock. */
|
|
|
|
SchedulerType::DisableScheduling();
|
2020-12-18 02:18:47 +01:00
|
|
|
m_spin_lock.Lock();
|
2020-02-01 01:25:17 +01:00
|
|
|
|
|
|
|
/* For debug, ensure that our state is valid. */
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(m_lock_count == 0);
|
|
|
|
MESOSPHERE_ASSERT(m_owner_thread == nullptr);
|
2020-02-01 01:25:17 +01:00
|
|
|
|
2023-02-21 16:08:08 +01:00
|
|
|
/* Take ownership of the lock. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_owner_thread = GetCurrentThreadPointer();
|
2020-02-01 01:25:17 +01:00
|
|
|
}
|
2023-02-21 16:08:08 +01:00
|
|
|
|
|
|
|
/* Increment the lock count. */
|
|
|
|
m_lock_count++;
|
2020-02-01 01:25:17 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:58:39 +01:00
|
|
|
MESOSPHERE_ALWAYS_INLINE_IF_RELEASE void Unlock() {
|
2020-02-01 01:25:17 +01:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
MESOSPHERE_ASSERT(this->IsLockedByCurrentThread());
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(m_lock_count > 0);
|
2020-02-01 01:25:17 +01:00
|
|
|
|
|
|
|
/* Release an instance of the lock. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if ((--m_lock_count) == 0) {
|
2021-10-26 02:34:47 +02:00
|
|
|
/* Perform a memory barrier here. */
|
|
|
|
cpu::DataMemoryBarrierInnerShareable();
|
|
|
|
|
2020-02-01 01:25:17 +01:00
|
|
|
/* We're no longer going to hold the lock. Take note of what cores need scheduling. */
|
|
|
|
const u64 cores_needing_scheduling = SchedulerType::UpdateHighestPriorityThreads();
|
|
|
|
|
|
|
|
/* Note that we no longer hold the lock, and unlock the spinlock. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_owner_thread = nullptr;
|
|
|
|
m_spin_lock.Unlock();
|
2020-02-01 01:25:17 +01:00
|
|
|
|
|
|
|
/* Enable scheduling, and perform a rescheduling operation. */
|
2020-02-05 22:02:35 +01:00
|
|
|
SchedulerType::EnableScheduling(cores_needing_scheduling);
|
2020-02-01 01:25:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|