1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-23 20:32:10 +00:00
Atmosphere/libraries/libmesosphere/include/mesosphere/kern_k_thread_queue.hpp

85 lines
2.9 KiB
C++
Raw Normal View History

2020-02-06 13:34:38 +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>
namespace ams::kern {
class KThreadQueue {
private:
KThread::WaiterList wait_list;
2020-02-06 13:34:38 +00:00
public:
constexpr ALWAYS_INLINE KThreadQueue() : wait_list() { /* ... */ }
2020-02-06 13:34:38 +00:00
bool IsEmpty() const { return this->wait_list.empty(); }
2020-02-06 13:34:38 +00:00
KThread::WaiterList::iterator begin() { return this->wait_list.begin(); }
KThread::WaiterList::iterator end() { return this->wait_list.end(); }
2020-02-06 13:34:38 +00:00
bool SleepThread(KThread *t) {
KScopedSchedulerLock sl;
2020-02-06 13:34:38 +00:00
/* If the thread needs terminating, don't enqueue it. */
if (t->IsTerminationRequested()) {
return false;
2020-02-06 13:34:38 +00:00
}
/* Set the thread's queue and mark it as waiting. */
t->SetSleepingQueue(this);
t->SetState(KThread::ThreadState_Waiting);
/* Add the thread to the queue. */
this->wait_list.push_back(*t);
2020-02-06 13:34:38 +00:00
return true;
}
void WakeupThread(KThread *t) {
KScopedSchedulerLock sl;
2020-02-06 13:34:38 +00:00
/* Remove the thread from the queue. */
this->wait_list.erase(this->wait_list.iterator_to(*t));
2020-02-06 13:34:38 +00:00
/* Mark the thread as no longer sleeping. */
t->SetState(KThread::ThreadState_Runnable);
t->SetSleepingQueue(nullptr);
}
KThread *WakeupFrontThread() {
KScopedSchedulerLock sl;
2020-02-06 13:34:38 +00:00
if (this->wait_list.empty()) {
return nullptr;
} else {
2020-02-06 13:34:38 +00:00
/* Remove the thread from the queue. */
auto it = this->wait_list.begin();
KThread *thread = std::addressof(*it);
this->wait_list.erase(it);
MESOSPHERE_ASSERT(thread->GetState() == KThread::ThreadState_Waiting);
2020-02-06 13:34:38 +00:00
/* Mark the thread as no longer sleeping. */
thread->SetState(KThread::ThreadState_Runnable);
thread->SetSleepingQueue(nullptr);
return thread;
2020-02-06 13:34:38 +00:00
}
}
};
}