2020-02-14 10:56:42 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-14 10:56:42 +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/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2021-09-19 17:11:56 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ThreadQueueImplForKWorkerTaskManager final : public KThreadQueue {
|
|
|
|
private:
|
|
|
|
KThread **m_waiting_thread;
|
|
|
|
public:
|
|
|
|
constexpr ThreadQueueImplForKWorkerTaskManager(KThread **t) : KThreadQueue(), m_waiting_thread(t) { /* ... */ }
|
|
|
|
|
|
|
|
virtual void EndWait(KThread *waiting_thread, Result wait_result) override {
|
|
|
|
/* Clear our waiting thread. */
|
|
|
|
*m_waiting_thread = nullptr;
|
|
|
|
|
|
|
|
/* Invoke the base end wait handler. */
|
|
|
|
KThreadQueue::EndWait(waiting_thread, wait_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CancelWait(KThread *waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
|
|
|
MESOSPHERE_UNUSED(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
MESOSPHERE_PANIC("ThreadQueueImplForKWorkerTaskManager::CancelWait\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-25 03:41:38 +00:00
|
|
|
void KWorkerTask::DoWorkerTask() {
|
|
|
|
if (auto * const thread = this->DynamicCast<KThread *>(); thread != nullptr) {
|
|
|
|
return thread->DoWorkerTaskImpl();
|
|
|
|
} else {
|
|
|
|
auto * const process = this->DynamicCast<KProcess *>();
|
|
|
|
MESOSPHERE_ABORT_UNLESS(process != nullptr);
|
|
|
|
|
|
|
|
return process->DoWorkerTaskImpl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 23:20:55 +00:00
|
|
|
void KWorkerTaskManager::Initialize(s32 priority) {
|
2020-02-14 10:56:42 +00:00
|
|
|
/* Reserve a thread from the system limit. */
|
|
|
|
MESOSPHERE_ABORT_UNLESS(Kernel::GetSystemResourceLimit().Reserve(ams::svc::LimitableResource_ThreadCountMax, 1));
|
|
|
|
|
|
|
|
/* Create a new thread. */
|
2021-09-19 17:11:56 +00:00
|
|
|
KThread *thread = KThread::Create();
|
|
|
|
MESOSPHERE_ABORT_UNLESS(thread != nullptr);
|
2020-02-14 10:56:42 +00:00
|
|
|
|
|
|
|
/* Launch the new thread. */
|
2021-09-19 17:11:56 +00:00
|
|
|
MESOSPHERE_R_ABORT_UNLESS(KThread::InitializeKernelThread(thread, ThreadFunction, reinterpret_cast<uintptr_t>(this), priority, cpu::NumCores - 1));
|
2020-02-14 10:56:42 +00:00
|
|
|
|
|
|
|
/* Register the new thread. */
|
2021-09-19 17:11:56 +00:00
|
|
|
KThread::Register(thread);
|
2020-02-14 10:56:42 +00:00
|
|
|
|
|
|
|
/* Run the thread. */
|
2021-09-19 17:11:56 +00:00
|
|
|
thread->Run();
|
2020-02-14 10:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KWorkerTaskManager::AddTask(WorkerType type, KWorkerTask *task) {
|
|
|
|
MESOSPHERE_ASSERT(type <= WorkerType_Count);
|
|
|
|
Kernel::GetWorkerTaskManager(type).AddTask(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWorkerTaskManager::ThreadFunction(uintptr_t arg) {
|
|
|
|
reinterpret_cast<KWorkerTaskManager *>(arg)->ThreadFunctionImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWorkerTaskManager::ThreadFunctionImpl() {
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Create wait queue. */
|
|
|
|
ThreadQueueImplForKWorkerTaskManager wait_queue(std::addressof(m_waiting_thread));
|
|
|
|
|
2020-02-14 10:56:42 +00:00
|
|
|
while (true) {
|
2021-09-19 17:11:56 +00:00
|
|
|
KWorkerTask *task;
|
2020-02-14 10:56:42 +00:00
|
|
|
|
|
|
|
/* Get a worker task. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
2021-09-19 17:11:56 +00:00
|
|
|
|
2020-02-14 10:56:42 +00:00
|
|
|
task = this->GetTask();
|
|
|
|
|
|
|
|
if (task == nullptr) {
|
2021-09-19 17:11:56 +00:00
|
|
|
/* Wait to have a task. */
|
|
|
|
m_waiting_thread = GetCurrentThreadPointer();
|
|
|
|
GetCurrentThread().BeginWait(std::addressof(wait_queue));
|
2020-02-14 10:56:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the task. */
|
|
|
|
task->DoWorkerTask();
|
2021-04-07 20:38:51 +00:00
|
|
|
|
|
|
|
/* Destroy any objects we may need to close. */
|
2021-09-19 17:11:56 +00:00
|
|
|
GetCurrentThread().DestroyClosedObjects();
|
2020-02-14 10:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KWorkerTask *KWorkerTaskManager::GetTask() {
|
|
|
|
MESOSPHERE_ASSERT(KScheduler::IsSchedulerLockedByCurrentThread());
|
2021-09-19 17:11:56 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
KWorkerTask *next = m_head_task;
|
2021-09-19 17:11:56 +00:00
|
|
|
|
|
|
|
if (next != nullptr) {
|
2020-02-14 10:56:42 +00:00
|
|
|
/* Advance the list. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_head_task == m_tail_task) {
|
|
|
|
m_head_task = nullptr;
|
|
|
|
m_tail_task = nullptr;
|
2020-02-14 10:56:42 +00:00
|
|
|
} else {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_head_task = m_head_task->GetNextTask();
|
2020-02-14 10:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the next task's next. */
|
|
|
|
next->SetNextTask(nullptr);
|
|
|
|
}
|
2021-09-19 17:11:56 +00:00
|
|
|
|
2020-02-14 10:56:42 +00:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWorkerTaskManager::AddTask(KWorkerTask *task) {
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
MESOSPHERE_ASSERT(task->GetNextTask() == nullptr);
|
|
|
|
|
|
|
|
/* Insert the task. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_tail_task) {
|
|
|
|
m_tail_task->SetNextTask(task);
|
|
|
|
m_tail_task = task;
|
2020-02-14 10:56:42 +00:00
|
|
|
} else {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_head_task = task;
|
|
|
|
m_tail_task = task;
|
2020-02-14 10:56:42 +00:00
|
|
|
|
|
|
|
/* Make ourselves active if we need to. */
|
2021-09-19 17:11:56 +00:00
|
|
|
if (m_waiting_thread != nullptr) {
|
|
|
|
m_waiting_thread->EndWait(ResultSuccess());
|
2020-02-14 10:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|