rpcsx/rpcs3/Emu/CPU/CPUThread.cpp

291 lines
4.6 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"
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 })
, m_id(idm::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
2015-07-19 13:36:32 +02:00
while (joinable() && is_alive())
{
2015-07-01 00:25:52 +02:00
CHECK_EMU_STATUS;
// check stop status
2015-07-19 13:36:32 +02:00
if (!is_stopped())
{
2015-07-01 00:25:52 +02:00
if (lock) lock.unlock();
try
{
2015-07-19 13:36:32 +02:00
task();
}
2015-07-01 00:25:52 +02:00
catch (CPUThreadReturn)
{
2015-07-08 17:01:59 +02:00
;
}
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-08 17:01:59 +02:00
catch (const fmt::exception&)
{
2015-07-19 13:36:32 +02:00
dump_info();
2015-07-08 17:01:59 +02:00
throw;
}
2015-07-01 19:09:26 +02:00
m_state &= ~CPU_STATE_RETURN;
2015-07-01 00:25:52 +02:00
continue;
}
2015-07-08 17:01:59 +02:00
if (!lock)
{
lock.lock();
continue;
}
cv.wait(lock);
}
2015-07-01 00:25:52 +02:00
});
}
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-19 13:36:32 +02:00
bool CPUThread::is_paused() const
{
2015-07-01 19:09:26 +02:00
return (m_state.load() & CPU_STATE_PAUSED) != 0 || Emu.IsPaused();
}
2015-07-19 13:36:32 +02:00
void CPUThread::dump_info() const
{
if (!Emu.IsStopped())
{
LOG_NOTICE(GENERAL, RegsToString());
}
}
2015-07-19 13:36:32 +02:00
void CPUThread::run()
{
SendDbgCommand(DID_START_THREAD, this);
2015-07-19 13:36:32 +02:00
init_stack();
init_regs();
do_run();
SendDbgCommand(DID_STARTED_THREAD, this);
}
2015-07-19 13:36:32 +02:00
void CPUThread::pause()
{
2015-07-06 21:35:34 +02:00
SendDbgCommand(DID_PAUSE_THREAD, this);
2015-07-06 21:35:34 +02:00
m_state |= CPU_STATE_PAUSED;
2015-07-06 21:35:34 +02:00
SendDbgCommand(DID_PAUSED_THREAD, this);
}
2015-07-19 13:36:32 +02:00
void CPUThread::resume()
{
2015-07-06 21:35:34 +02:00
SendDbgCommand(DID_RESUME_THREAD, this);
2015-07-06 21:35:34 +02:00
{
// lock for reliable notification
std::lock_guard<std::mutex> lock(mutex);
2015-07-01 00:25:52 +02:00
2015-07-06 21:35:34 +02:00
m_state &= ~CPU_STATE_PAUSED;
2015-07-06 21:35:34 +02:00
cv.notify_one();
}
SendDbgCommand(DID_RESUMED_THREAD, this);
}
2015-07-19 13:36:32 +02:00
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-06 21:35:34 +02:00
// lock for reliable notification
std::lock_guard<std::mutex> lock(mutex);
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);
}
2015-07-19 13:36:32 +02:00
void CPUThread::exec()
{
SendDbgCommand(DID_EXEC_THREAD, this);
2015-07-06 21:35:34 +02:00
{
// lock for reliable notification
std::lock_guard<std::mutex> lock(mutex);
2015-07-01 00:25:52 +02:00
2015-07-06 21:35:34 +02:00
m_state &= ~CPU_STATE_STOPPED;
cv.notify_one();
}
}
2015-07-19 13:36:32 +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-19 13:36:32 +02:00
void CPUThread::step()
2015-07-01 00:25:52 +02:00
{
if (m_state.atomic_op([](u64& state) -> bool
2015-07-01 00:25:52 +02:00
{
const bool was_paused = (state & CPU_STATE_PAUSED) != 0;
2015-07-01 00:25:52 +02:00
state |= CPU_STATE_STEP;
2015-07-01 19:09:26 +02:00
state &= ~CPU_STATE_PAUSED;
return was_paused;
}))
{
if (is_current()) return;
// lock for reliable notification (only if PAUSE was removed)
std::lock_guard<std::mutex> lock(mutex);
cv.notify_one();
}
}
2015-07-19 13:36:32 +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
}
2015-07-19 13:36:32 +02:00
void CPUThread::awake()
2015-07-01 00:25:52 +02:00
{
2015-07-06 21:35:34 +02:00
// must be called after the balanced Sleep() call
2015-07-03 18:07:36 +02:00
if (m_state.atomic_op([](u64& state) -> bool
2015-07-03 18:07:36 +02:00
{
if (state < CPU_STATE_MAX)
{
throw EXCEPTION("Sleep()/Awake() inconsistency");
}
if ((state -= CPU_STATE_MAX) < CPU_STATE_MAX)
{
state &= ~CPU_STATE_SLEEP;
// notify the condition variable as well
return true;
2015-07-03 18:07:36 +02:00
}
2014-07-07 19:22:36 +02:00
return false;
}))
{
if (is_current()) return;
// lock for reliable notification; the condition being checked is probably externally set
std::lock_guard<std::mutex> lock(mutex);
2015-07-06 21:35:34 +02:00
cv.notify_one();
}
}
2015-07-19 13:36:32 +02:00
bool CPUThread::signal()
{
// try to set SIGNAL
if (m_state._or(CPU_STATE_SIGNAL) & CPU_STATE_SIGNAL)
{
return false;
}
else
{
// not truly responsible for signal delivery, requires additional measures like LV2_LOCK
cv.notify_one();
return true;
}
}
2015-07-19 13:36:32 +02:00
bool CPUThread::unsignal()
{
// remove SIGNAL and return its old value
return (m_state._and_not(CPU_STATE_SIGNAL) & CPU_STATE_SIGNAL) != 0;
2015-07-01 00:25:52 +02:00
}
2015-07-19 13:36:32 +02:00
bool CPUThread::check_status()
2015-07-01 00:25:52 +02:00
{
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-19 13:36:32 +02:00
if (!is_paused()) break;
if (!lock)
{
lock.lock();
continue;
}
2015-07-06 21:35:34 +02:00
cv.wait(lock);
}
2015-07-19 13:36:32 +02:00
if (m_state.load() & CPU_STATE_RETURN || is_stopped())
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;
}