rpcsx/rpcs3/Emu/CPU/CPUThread.cpp

211 lines
3.1 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-23 02:16:54 +02:00
#include "rpcs3/Ini.h"
#include "Utilities/Log.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2015-07-01 00:25:52 +02:00
#include "Emu/IdManager.h"
2014-07-12 09:02:39 +02:00
#include "Emu/DbgCommand.h"
2015-07-01 00:25:52 +02:00
#include "CPUThreadManager.h"
2014-08-23 22:40:04 +02:00
#include "CPUDecoder.h"
#include "CPUThread.h"
2015-07-01 00:25:52 +02:00
CPUThread::CPUThread(CPUThreadType type, const std::string& name, std::function<std::string()> thread_name)
: m_state({ CPU_STATE_STOP })
, m_id(Emu.GetIdManager().get_current_id())
, m_type(type)
2015-07-01 00:25:52 +02:00
, m_name(name)
{
2015-07-01 00:25:52 +02:00
start(thread_name, [this]
{
2015-07-01 00:25:52 +02:00
std::unique_lock<std::mutex> lock(mutex);
// check thread status
while (joinable() && IsActive())
{
2015-07-01 00:25:52 +02:00
CHECK_EMU_STATUS;
// check stop status
if (!IsStopped())
{
2015-07-01 00:25:52 +02:00
if (lock) lock.unlock();
try
{
2015-07-01 00:25:52 +02:00
Task();
}
2015-07-01 00:25:52 +02:00
catch (CPUThreadReturn)
{
}
2015-07-01 00:25:52 +02:00
catch (CPUThreadStop)
{
m_state |= CPU_STATE_STOP;
}
catch (CPUThreadExit)
{
m_state |= CPU_STATE_DEAD;
break;
}
2015-07-01 00:25:52 +02:00
cv.notify_one();
continue;
}
2015-07-01 00:25:52 +02:00
if (!lock) lock.lock();
2015-07-01 00:25:52 +02:00
cv.wait_for(lock, std::chrono::milliseconds(1));
}
2015-07-01 00:25:52 +02:00
cv.notify_all();
});
2014-08-22 18:36:27 +02:00
2015-07-01 00:25:52 +02:00
SendDbgCommand(DID_CREATE_THREAD, this);
}
2015-07-01 00:25:52 +02:00
CPUThread::~CPUThread()
{
2015-07-01 00:25:52 +02:00
if (joinable())
{
2015-07-01 00:25:52 +02:00
throw EXCEPTION("Thread not joined");
}
2015-07-01 00:25:52 +02:00
SendDbgCommand(DID_REMOVE_THREAD, this);
}
2015-07-01 00:25:52 +02:00
bool CPUThread::IsPaused() const
{
2015-07-01 00:25:52 +02:00
return (m_state.load() & CPU_STATE_PAUSE) != 0 || Emu.IsPaused();
}
2015-07-01 00:25:52 +02:00
void CPUThread::DumpInformation() const
{
2015-07-01 00:25:52 +02:00
LOG_WARNING(GENERAL, RegsToString());
}
void CPUThread::Run()
{
SendDbgCommand(DID_START_THREAD, this);
InitStack();
InitRegs();
DoRun();
SendDbgCommand(DID_STARTED_THREAD, this);
}
void CPUThread::Resume()
{
SendDbgCommand(DID_RESUME_THREAD, this);
2015-07-01 00:25:52 +02:00
m_state &= ~CPU_STATE_PAUSE;
2015-07-01 00:25:52 +02:00
cv.notify_one();
SendDbgCommand(DID_RESUMED_THREAD, this);
}
void CPUThread::Pause()
{
SendDbgCommand(DID_PAUSE_THREAD, this);
2015-07-01 00:25:52 +02:00
m_state |= CPU_STATE_PAUSE;
cv.notify_one();
SendDbgCommand(DID_PAUSED_THREAD, this);
}
void CPUThread::Stop()
{
SendDbgCommand(DID_STOP_THREAD, this);
2015-07-01 00:25:52 +02:00
if (is_current())
{
2015-07-01 00:25:52 +02:00
throw CPUThreadStop{};
}
2015-07-01 00:25:52 +02:00
else
{
m_state |= CPU_STATE_STOP;
2015-07-01 00:25:52 +02:00
cv.notify_one();
}
SendDbgCommand(DID_STOPED_THREAD, this);
}
void CPUThread::Exec()
{
SendDbgCommand(DID_EXEC_THREAD, this);
2015-07-01 00:25:52 +02:00
m_state &= ~CPU_STATE_STOP;
cv.notify_one();
}
2015-07-01 00:25:52 +02:00
void CPUThread::Exit()
{
2015-07-01 00:25:52 +02:00
if (is_current())
{
throw CPUThreadExit{};
}
else
{
throw EXCEPTION("Unable to exit another thread");
}
}
2015-07-01 00:25:52 +02:00
void CPUThread::Step()
{
m_state.atomic_op([](u64& state)
{
state |= CPU_STATE_STEP;
state &= ~CPU_STATE_PAUSE;
});
cv.notify_one();
}
2015-07-01 00:25:52 +02:00
void CPUThread::Sleep()
{
2015-07-01 00:25:52 +02:00
m_state ^= CPU_STATE_SLEEP;
2015-07-01 00:25:52 +02:00
cv.notify_one();
}
2015-07-01 00:25:52 +02:00
void CPUThread::Awake()
{
m_state ^= CPU_STATE_SLEEP;
2014-07-07 19:22:36 +02:00
2015-07-01 00:25:52 +02:00
cv.notify_one();
}
bool CPUThread::CheckStatus()
{
std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
2014-07-13 20:55:14 +02:00
while (true)
2014-07-07 19:22:36 +02:00
{
2015-07-01 00:25:52 +02:00
CHECK_EMU_STATUS; // check at least once
2015-07-01 00:25:52 +02:00
if (!IsPaused()) break;
2015-07-01 00:25:52 +02:00
if (!lock) lock.lock();
2015-07-01 00:25:52 +02:00
cv.wait_for(lock, std::chrono::milliseconds(1));
}
2015-07-01 00:25:52 +02:00
if (IsStopped())
2014-10-07 23:37:04 +02:00
{
2015-07-01 00:25:52 +02:00
return true;
}
2014-10-07 23:37:04 +02:00
2015-07-01 00:25:52 +02:00
if (m_state.load() & CPU_STATE_STEP)
{
// set PAUSE, but allow to execute once
m_state |= CPU_STATE_PAUSE;
m_state &= ~CPU_STATE_STEP;
2014-10-07 23:37:04 +02:00
}
2015-07-01 00:25:52 +02:00
return false;
}