// SPDX-License-Identifier: MPL-2.0 // Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/) #include #include #include "KThread.h" #include "KProcess.h" namespace skyline::kernel::type { KThread::KThread(const DeviceState &state, KHandle handle, pid_t selfTid, u64 entryPoint, u64 entryArg, u64 stackTop, u64 tls, u8 priority, KProcess *parent, std::shared_ptr &tlsMemory) : handle(handle), tid(selfTid), entryPoint(entryPoint), entryArg(entryArg), stackTop(stackTop), tls(tls), priority(priority), parent(parent), ctxMemory(tlsMemory), KSyncObject(state, KType::KThread) { UpdatePriority(priority); } KThread::~KThread() { Kill(); } void KThread::Start() { if (status == Status::Created) { if (tid == parent->pid) parent->status = KProcess::Status::Started; status = Status::Running; state.nce->StartThread(entryArg, handle, parent->threads.at(tid)); } } void KThread::Kill() { if (status != Status::Dead) { status = Status::Dead; Signal(); tgkill(parent->pid, tid, SIGKILL); } } void KThread::UpdatePriority(u8 priority) { this->priority = priority; auto linuxPriority = static_cast(constant::AndroidPriority.first + ((static_cast(constant::AndroidPriority.second - constant::AndroidPriority.first) / static_cast(constant::SwitchPriority.second - constant::SwitchPriority.first)) * (static_cast(priority) - constant::SwitchPriority.first))); // Resize range SwitchPriority (Nintendo Priority) to AndroidPriority (Android Priority) if (setpriority(PRIO_PROCESS, static_cast(tid), linuxPriority) == -1) throw exception("Couldn't set process priority to {} for PID: {}", linuxPriority, tid); } }