2020-02-18 13:04:49 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
#include <mesosphere/kern_k_thread.hpp>
|
|
|
|
#include <mesosphere/kern_k_scheduler.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KConditionVariable {
|
|
|
|
public:
|
2020-07-15 16:15:49 +00:00
|
|
|
using ThreadTree = typename KThread::ConditionVariableThreadTreeType;
|
2020-02-18 13:04:49 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
ThreadTree m_tree;
|
2020-02-18 13:04:49 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KConditionVariable() : m_tree() { /* ... */ }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
|
|
|
/* Arbitration. */
|
2021-09-17 22:11:58 +00:00
|
|
|
static Result SignalToAddress(KProcessAddress addr);
|
|
|
|
static Result WaitForAddress(ams::svc::Handle handle, KProcessAddress addr, u32 value);
|
2020-02-18 13:04:49 +00:00
|
|
|
|
|
|
|
/* Condition variable. */
|
|
|
|
void Signal(uintptr_t cv_key, s32 count);
|
|
|
|
Result Wait(KProcessAddress addr, uintptr_t key, u32 value, s64 timeout);
|
2020-07-15 16:15:49 +00:00
|
|
|
private:
|
2021-04-07 06:33:33 +00:00
|
|
|
void SignalImpl(KThread *thread);
|
2020-07-15 16:15:49 +00:00
|
|
|
};
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-07-15 16:15:49 +00:00
|
|
|
ALWAYS_INLINE void BeforeUpdatePriority(KConditionVariable::ThreadTree *tree, KThread *thread) {
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-07-15 16:15:49 +00:00
|
|
|
tree->erase(tree->iterator_to(*thread));
|
|
|
|
}
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-07-15 16:15:49 +00:00
|
|
|
ALWAYS_INLINE void AfterUpdatePriority(KConditionVariable::ThreadTree *tree, KThread *thread) {
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-07-15 16:15:49 +00:00
|
|
|
tree->insert(*thread);
|
|
|
|
}
|
2020-02-18 13:04:49 +00:00
|
|
|
|
|
|
|
}
|