CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/HLE/HLEHelperThread.cpp
Views: 1401
// Copyright (c) 2014- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "Common/Serialize/Serializer.h"18#include "Common/Serialize/SerializeFuncs.h"19#include "Core/CoreTiming.h"20#include "Core/MemMapHelpers.h"21#include "Core/HLE/HLE.h"22#include "Core/HLE/HLEHelperThread.h"23#include "Core/HLE/KernelWaitHelpers.h"24#include "Core/HLE/sceKernelThread.h"25#include "Core/HLE/sceKernelMemory.h"26#include "Core/MIPS/MIPSCodeUtils.h"2728HLEHelperThread::HLEHelperThread() : id_(0), entry_(0) {29}3031HLEHelperThread::HLEHelperThread(const char *threadName, const u32 instructions[], u32 instrCount, u32 prio, int stacksize) {32u32 instrBytes = instrCount * sizeof(u32);33u32 totalBytes = instrBytes + sizeof(u32) * 2;34AllocEntry(totalBytes);35Memory::Memcpy(entry_, instructions, instrBytes, "HelperMIPS");3637// Just to simplify things, we add the return here.38Memory::Write_U32(MIPS_MAKE_JR_RA(), entry_ + instrBytes + 0);39Memory::Write_U32(MIPS_MAKE_NOP(), entry_ + instrBytes + 4);4041Create(threadName, prio, stacksize);42}4344HLEHelperThread::HLEHelperThread(const char *threadName, const char *module, const char *func, u32 prio, int stacksize) {45const u32 bytes = sizeof(u32) * 2;46AllocEntry(bytes);47Memory::Write_U32(MIPS_MAKE_JR_RA(), entry_ + 0);48Memory::Write_U32(MIPS_MAKE_SYSCALL(module, func), entry_ + 4);4950Create(threadName, prio, stacksize);51}5253HLEHelperThread::~HLEHelperThread() {54if (id_ > 0)55__KernelDeleteThread(id_, SCE_KERNEL_ERROR_THREAD_TERMINATED, "helper deleted");56if (entry_)57kernelMemory.Free(entry_);58}5960void HLEHelperThread::AllocEntry(u32 size) {61entry_ = kernelMemory.Alloc(size, false, "HLEHelper");62Memory::Memset(entry_, 0, size, "HLEHelperClear");63currentMIPS->InvalidateICache(entry_, size);64}6566void HLEHelperThread::Create(const char *threadName, u32 prio, int stacksize) {67id_ = __KernelCreateThreadInternal(threadName, __KernelGetCurThreadModuleId(), entry_, prio, stacksize, 0x00001000);68}6970void HLEHelperThread::DoState(PointerWrap &p) {71auto s = p.Section("HLEHelperThread", 1);72if (!s) {73return;74}7576Do(p, id_);77Do(p, entry_);78}7980void HLEHelperThread::Start(u32 a0, u32 a1) {81__KernelStartThread(id_, a0, a1, true);82}8384void HLEHelperThread::Terminate() {85__KernelStopThread(id_, SCE_KERNEL_ERROR_THREAD_TERMINATED, "helper terminated");86}8788bool HLEHelperThread::Stopped() {89return KernelIsThreadDormant(id_);90}9192void HLEHelperThread::ChangePriority(u32 prio) {93KernelChangeThreadPriority(id_, prio);94}9596void HLEHelperThread::Resume(WaitType waitType, SceUID uid, int result) {97bool res = HLEKernel::ResumeFromWait(id_, waitType, uid, result);98if (!res) {99ERROR_LOG(Log::HLE, "Failed to wake helper thread from wait");100}101}102103void HLEHelperThread::Forget() {104id_ = 0;105entry_ = 0;106}107108109