2020-01-30 06:06:25 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-01-30 06:06:25 +00: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
|
2020-01-30 23:29:51 +00:00
|
|
|
#include <mesosphere/kern_common.hpp>
|
2020-01-30 06:06:25 +00:00
|
|
|
#include <mesosphere/kern_select_cpu.hpp>
|
|
|
|
#include <mesosphere/kern_k_current_context.hpp>
|
2020-02-01 00:37:58 +00:00
|
|
|
#include <mesosphere/kern_k_scoped_lock.hpp>
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KLightLock {
|
|
|
|
private:
|
2021-10-19 22:24:15 +00:00
|
|
|
util::Atomic<uintptr_t> m_tag;
|
2020-01-30 06:06:25 +00:00
|
|
|
public:
|
2021-10-23 22:25:20 +00:00
|
|
|
constexpr ALWAYS_INLINE KLightLock() : m_tag(0) { /* ... */ }
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
void Lock() {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2020-01-30 06:06:25 +00:00
|
|
|
const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer());
|
|
|
|
|
|
|
|
while (true) {
|
2021-10-19 22:24:15 +00:00
|
|
|
uintptr_t old_tag = m_tag.Load<std::memory_order_relaxed>();
|
2020-01-30 06:06:25 +00:00
|
|
|
|
2021-10-19 22:24:15 +00:00
|
|
|
while (!m_tag.CompareExchangeWeak<std::memory_order_acquire>(old_tag, (old_tag == 0) ? cur_thread : (old_tag | 1))) {
|
2021-10-19 09:45:29 +00:00
|
|
|
/* ... */
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 09:45:29 +00:00
|
|
|
if (old_tag == 0 || this->LockSlowPath(old_tag | 1, cur_thread)) {
|
2020-01-30 06:06:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
ALWAYS_INLINE void Unlock() {
|
2020-01-30 23:29:51 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2020-01-30 06:06:25 +00:00
|
|
|
const uintptr_t cur_thread = reinterpret_cast<uintptr_t>(GetCurrentThreadPointer());
|
2021-10-19 09:45:29 +00:00
|
|
|
|
2020-01-30 06:06:25 +00:00
|
|
|
uintptr_t expected = cur_thread;
|
2021-10-19 22:24:15 +00:00
|
|
|
if (!m_tag.CompareExchangeStrong<std::memory_order_release>(expected, 0)) {
|
2021-10-19 09:45:29 +00:00
|
|
|
this->UnlockSlowPath(cur_thread);
|
|
|
|
}
|
2020-01-30 06:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 09:45:29 +00:00
|
|
|
bool LockSlowPath(uintptr_t owner, uintptr_t cur_thread);
|
2020-01-30 06:06:25 +00:00
|
|
|
void UnlockSlowPath(uintptr_t cur_thread);
|
2020-02-14 01:38:56 +00:00
|
|
|
|
2021-10-19 22:24:15 +00:00
|
|
|
ALWAYS_INLINE bool IsLocked() const { return m_tag.Load() != 0; }
|
|
|
|
ALWAYS_INLINE bool IsLockedByCurrentThread() const { return (m_tag.Load() | 0x1ul) == (reinterpret_cast<uintptr_t>(GetCurrentThreadPointer()) | 0x1ul); }
|
2020-01-30 06:06:25 +00:00
|
|
|
};
|
|
|
|
|
2020-02-01 00:37:58 +00:00
|
|
|
using KScopedLightLock = KScopedLock<KLightLock>;
|
2020-01-30 06:06:25 +00:00
|
|
|
|
|
|
|
}
|