rpcsx/rpcs3/Emu/CPU/CPUThread.cpp

226 lines
3.4 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)
2015-07-01 19:09:26 +02:00
: m_state({ CPU_STATE_STOPPED })
2015-07-01 00:25:52 +02:00
, m_id(Emu.GetIdManager().get_current_id())
, m_type(type)
2015-07-01 00:25:52 +02:00
, m_name(name)
{
2015-07-04 21:23:10 +02:00
start(std::move(thread_name), [this]
{
2015-07-01 19:09:26 +02:00
SendDbgCommand(DID_CREATE_THREAD, 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)
{
2015-07-01 19:09:26 +02:00
m_state |= CPU_STATE_STOPPED;
2015-07-01 00:25:52 +02:00
}
catch (CPUThreadExit)
{
m_state |= CPU_STATE_DEAD;
break;
}
2015-07-01 19:09:26 +02:00
m_state &= ~CPU_STATE_RETURN;
2015-07-01 00:25:52 +02:00
cv.notify_one();
continue;
}
2015-07-01 00:25:52 +02:00
if (!lock) lock.lock();
cv.wait(lock);
}
2015-07-01 00:25:52 +02:00
cv.notify_all();
});
}
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 19:09:26 +02:00
return (m_state.load() & CPU_STATE_PAUSED) != 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 19:09:26 +02:00
m_state &= ~CPU_STATE_PAUSED;
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 19:09:26 +02:00
m_state |= CPU_STATE_PAUSED;
2015-07-01 00:25:52 +02:00
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
{
2015-07-01 19:09:26 +02:00
m_state |= CPU_STATE_STOPPED;
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 19:09:26 +02:00
m_state &= ~CPU_STATE_STOPPED;
2015-07-01 00:25:52 +02:00
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;
2015-07-01 19:09:26 +02:00
state &= ~CPU_STATE_PAUSED;
2015-07-01 00:25:52 +02:00
});
cv.notify_one();
}
2015-07-01 00:25:52 +02:00
void CPUThread::Sleep()
{
2015-07-03 18:07:36 +02:00
m_state += CPU_STATE_MAX;
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()
{
2015-07-03 18:07:36 +02:00
// must be called after the corresponding Sleep() call
m_state.atomic_op([](u64& state)
{
if (state < CPU_STATE_MAX)
{
throw EXCEPTION("Sleep()/Awake() inconsistency");
}
if ((state -= CPU_STATE_MAX) < CPU_STATE_MAX)
{
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 19:09:26 +02:00
if (m_state.load() & CPU_STATE_RETURN || 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
2015-07-01 19:09:26 +02:00
m_state |= CPU_STATE_PAUSED;
2015-07-01 00:25:52 +02:00
m_state &= ~CPU_STATE_STEP;
2014-10-07 23:37:04 +02:00
}
2015-07-01 00:25:52 +02:00
return false;
}