2020-01-30 07:06:25 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-01-30 07:06:25 +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/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ThreadQueueImplForKLightLock final : public KThreadQueue {
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKLightLock() : KThreadQueue() { /* ... */ }
|
|
|
|
|
|
|
|
virtual void CancelWait(KThread *waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
|
|
|
/* Do nothing, waiting to acquire a light lock cannot be canceled. */
|
|
|
|
MESOSPHERE_UNUSED(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:45:29 +02:00
|
|
|
bool KLightLock::LockSlowPath(uintptr_t _owner, uintptr_t _cur_thread) {
|
2020-02-15 00:27:34 +01:00
|
|
|
KThread *cur_thread = reinterpret_cast<KThread *>(_cur_thread);
|
2021-09-19 19:11:56 +02:00
|
|
|
ThreadQueueImplForKLightLock wait_queue;
|
2020-02-15 00:27:34 +01:00
|
|
|
|
|
|
|
/* Pend the current thread waiting on the owner thread. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Ensure we actually have locking to do. */
|
2021-10-20 00:24:15 +02:00
|
|
|
if (m_tag.Load<std::memory_order_relaxed>() != _owner) {
|
2021-10-19 11:45:29 +02:00
|
|
|
return false;
|
2020-02-15 00:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the current thread as a waiter on the owner. */
|
|
|
|
KThread *owner_thread = reinterpret_cast<KThread *>(_owner & ~1ul);
|
2020-12-18 02:18:47 +01:00
|
|
|
cur_thread->SetAddressKey(reinterpret_cast<uintptr_t>(std::addressof(m_tag)));
|
2020-02-15 00:27:34 +01:00
|
|
|
owner_thread->AddWaiter(cur_thread);
|
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
/* Begin waiting to hold the lock. */
|
|
|
|
cur_thread->BeginWait(std::addressof(wait_queue));
|
2020-07-25 13:28:44 +02:00
|
|
|
|
2020-02-15 00:27:34 +01:00
|
|
|
if (owner_thread->IsSuspended()) {
|
|
|
|
owner_thread->ContinueIfHasKernelWaiters();
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 11:45:29 +02:00
|
|
|
|
|
|
|
return true;
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 00:27:34 +01:00
|
|
|
void KLightLock::UnlockSlowPath(uintptr_t _cur_thread) {
|
|
|
|
KThread *owner_thread = reinterpret_cast<KThread *>(_cur_thread);
|
|
|
|
|
|
|
|
/* Unlock. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Get the next owner. */
|
2023-02-21 21:15:01 +01:00
|
|
|
bool has_waiters;
|
|
|
|
KThread *next_owner = owner_thread->RemoveWaiterByKey(std::addressof(has_waiters), reinterpret_cast<uintptr_t>(std::addressof(m_tag)));
|
2020-02-15 00:27:34 +01:00
|
|
|
|
|
|
|
/* Pass the lock to the next owner. */
|
|
|
|
uintptr_t next_tag = 0;
|
2021-04-07 10:44:27 +02:00
|
|
|
if (next_owner != nullptr) {
|
2023-02-21 21:15:01 +01:00
|
|
|
next_tag = reinterpret_cast<uintptr_t>(next_owner) | static_cast<uintptr_t>(has_waiters);
|
2020-02-15 00:27:34 +01:00
|
|
|
|
2021-09-19 19:11:56 +02:00
|
|
|
next_owner->EndWait(ResultSuccess());
|
2020-07-25 13:28:44 +02:00
|
|
|
|
|
|
|
if (next_owner->IsSuspended()) {
|
|
|
|
next_owner->ContinueIfHasKernelWaiters();
|
|
|
|
}
|
2020-02-15 00:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We may have unsuspended in the process of acquiring the lock, so we'll re-suspend now if so. */
|
|
|
|
if (owner_thread->IsSuspended()) {
|
|
|
|
owner_thread->TrySuspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the new tag value. */
|
2021-10-20 00:24:15 +02:00
|
|
|
m_tag.Store<std::memory_order_release>(next_tag);
|
2020-02-15 00:27:34 +01:00
|
|
|
}
|
2020-01-30 07:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|