ThreadBase rewritten (wip)

This commit is contained in:
Nekotekina 2015-07-01 01:25:52 +03:00
parent b7a320fbbd
commit 3aefa2b4e1
85 changed files with 1960 additions and 2183 deletions

View file

@ -2,13 +2,13 @@
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
#include "Emu/DbgCommand.h"
#include "Emu/IdManager.h"
#include "CPUThreadManager.h"
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/SPUThread.h"
#include "Emu/Cell/RawSPUThread.h"
#include "Emu/ARMv7/ARMv7Thread.h"
#include "CPUThreadManager.h"
CPUThreadManager::CPUThreadManager()
{
@ -16,128 +16,84 @@ CPUThreadManager::CPUThreadManager()
CPUThreadManager::~CPUThreadManager()
{
Close();
}
void CPUThreadManager::Close()
{
while(m_threads.size()) RemoveThread(m_threads[0]->GetId());
std::lock_guard<std::mutex> lock(m_mutex);
for (auto& x : m_raw_spu)
{
x.reset();
}
}
std::shared_ptr<CPUThread> CPUThreadManager::AddThread(CPUThreadType type)
std::vector<std::shared_ptr<CPUThread>> CPUThreadManager::GetAllThreads() const
{
std::vector<std::shared_ptr<CPUThread>> result;
for (auto& v : Emu.GetIdManager().get_data<PPUThread>())
{
result.emplace_back(std::static_pointer_cast<CPUThread>(v.data));
}
for (auto& v : Emu.GetIdManager().get_data<SPUThread>())
{
result.emplace_back(std::static_pointer_cast<CPUThread>(v.data));
}
for (auto& v : Emu.GetIdManager().get_data<RawSPUThread>())
{
result.emplace_back(std::static_pointer_cast<CPUThread>(v.data));
}
for (auto& v : Emu.GetIdManager().get_data<ARMv7Thread>())
{
result.emplace_back(std::static_pointer_cast<CPUThread>(v.data));
}
return result;
}
void CPUThreadManager::Exec() const
{
for (auto& v : Emu.GetIdManager().get_data<PPUThread>())
{
static_cast<CPUThread*>(v.data.get())->Exec();
}
for (auto& v : Emu.GetIdManager().get_data<ARMv7Thread>())
{
static_cast<CPUThread*>(v.data.get())->Exec();
}
}
std::shared_ptr<RawSPUThread> CPUThreadManager::NewRawSPUThread()
{
std::lock_guard<std::mutex> lock(m_mutex);
std::shared_ptr<CPUThread> new_thread;
std::shared_ptr<RawSPUThread> result;
switch(type)
for (u32 i = 0; i < m_raw_spu.size(); i++)
{
case CPU_THREAD_PPU:
{
new_thread = std::make_shared<PPUThread>();
break;
}
case CPU_THREAD_SPU:
{
new_thread = std::make_shared<SPUThread>();
break;
}
case CPU_THREAD_RAW_SPU:
{
for (u32 i = 0; i < m_raw_spu.size(); i++)
if (m_raw_spu[i].expired())
{
if (!m_raw_spu[i])
{
new_thread = std::make_shared<RawSPUThread>();
new_thread->index = i;
m_raw_spu[i] = new_thread;
break;
}
}
break;
}
case CPU_THREAD_ARMv7:
{
new_thread.reset(new ARMv7Thread());
break;
}
default: assert(0);
}
if (new_thread)
{
new_thread->SetId(Emu.GetIdManager().add(new_thread));
m_threads.push_back(new_thread);
SendDbgCommand(DID_CREATE_THREAD, new_thread.get());
}
return new_thread;
}
void CPUThreadManager::RemoveThread(u32 id)
{
std::lock_guard<std::mutex> lock(m_mutex);
std::shared_ptr<CPUThread> thr;
u32 thread_index = 0;
for (u32 i = 0; i < m_threads.size(); ++i)
{
if (m_threads[i]->GetId() != id) continue;
thr = m_threads[i];
thread_index = i;
}
if (thr)
{
SendDbgCommand(DID_REMOVE_THREAD, thr.get());
thr->Close();
m_threads.erase(m_threads.begin() + thread_index);
if (thr->GetType() == CPU_THREAD_RAW_SPU)
{
assert(thr->index < m_raw_spu.size());
m_raw_spu[thr->index] = nullptr;
m_raw_spu[i] = result = Emu.GetIdManager().make_ptr<RawSPUThread>("RawSPU " + std::to_string(i), i);
break;
}
}
// Removing the ID should trigger the actual deletion of the thread
Emu.GetIdManager().remove<CPUThread>(id);
Emu.CheckStatus();
return result;
}
std::shared_ptr<CPUThread> CPUThreadManager::GetThread(u32 id)
{
return Emu.GetIdManager().get<CPUThread>(id);
}
std::shared_ptr<CPUThread> CPUThreadManager::GetThread(u32 id, CPUThreadType type)
{
const auto res = GetThread(id);
return res && res->GetType() == type ? res : nullptr;
}
std::shared_ptr<CPUThread> CPUThreadManager::GetRawSPUThread(u32 index)
std::shared_ptr<RawSPUThread> CPUThreadManager::GetRawSPUThread(u32 index)
{
if (index >= m_raw_spu.size())
{
return nullptr;
}
return m_raw_spu[index];
}
void CPUThreadManager::Exec()
{
std::lock_guard<std::mutex> lock(m_mutex);
for(u32 i = 0; i < m_threads.size(); ++i)
{
m_threads[i]->Exec();
}
return m_raw_spu[index].lock();
}