rpcsx/rpcs3/Emu/CPU/CPUThread.cpp

165 lines
2.5 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Emu/System.h"
#include "CPUThread.h"
2016-05-13 16:01:48 +02:00
#include <mutex>
template<>
void fmt_class_string<cpu_type>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](auto arg)
{
switch (arg)
{
STR_CASE(cpu_type::ppu);
STR_CASE(cpu_type::spu);
STR_CASE(cpu_type::arm);
}
return unknown;
});
}
template<>
2016-08-07 21:01:27 +02:00
void fmt_class_string<cpu_state>::format(std::string& out, u64 arg)
{
2016-08-07 21:01:27 +02:00
format_enum(out, arg, [](cpu_state f)
{
switch (f)
{
STR_CASE(cpu_state::stop);
STR_CASE(cpu_state::exit);
STR_CASE(cpu_state::suspend);
STR_CASE(cpu_state::ret);
STR_CASE(cpu_state::signal);
STR_CASE(cpu_state::dbg_global_pause);
STR_CASE(cpu_state::dbg_global_stop);
STR_CASE(cpu_state::dbg_pause);
STR_CASE(cpu_state::dbg_step);
case cpu_state::__bitset_enum_max: break;
}
return unknown;
});
}
template<>
void fmt_class_string<bs_t<cpu_state>>::format(std::string& out, u64 arg)
{
format_bitset(out, arg, "[", "|", "]", &fmt_class_string<cpu_state>::format);
}
2016-04-14 00:59:00 +02:00
thread_local cpu_thread* g_tls_current_cpu_thread = nullptr;
2016-04-14 00:59:00 +02:00
void cpu_thread::on_task()
{
state -= cpu_state::exit;
2015-11-26 09:06:29 +01:00
g_tls_current_cpu_thread = this;
Emu.SendDbgCommand(DID_CREATE_THREAD, this);
std::unique_lock<named_thread> lock(*this);
2015-07-01 19:09:26 +02:00
2016-04-14 00:59:00 +02:00
// Check thread status
2016-08-07 21:01:27 +02:00
while (!test(state & cpu_state::exit))
2015-11-26 09:06:29 +01:00
{
CHECK_EMU_STATUS;
2015-07-01 00:25:52 +02:00
2015-11-26 09:06:29 +01:00
// check stop status
2016-08-07 21:01:27 +02:00
if (!test(state & cpu_state::stop))
{
2015-11-26 09:06:29 +01:00
if (lock) lock.unlock();
2015-07-01 00:25:52 +02:00
2015-11-26 09:06:29 +01:00
try
{
2015-11-26 09:06:29 +01:00
cpu_task();
}
2016-04-14 00:59:00 +02:00
catch (cpu_state _s)
2015-11-26 09:06:29 +01:00
{
2016-04-14 00:59:00 +02:00
state += _s;
2015-11-26 09:06:29 +01:00
}
2016-04-14 00:59:00 +02:00
catch (const std::exception&)
2015-07-08 17:01:59 +02:00
{
2016-04-14 00:59:00 +02:00
LOG_NOTICE(GENERAL, "\n%s", dump());
2015-11-26 09:06:29 +01:00
throw;
2015-07-08 17:01:59 +02:00
}
2016-04-14 00:59:00 +02:00
state -= cpu_state::ret;
2015-11-26 09:06:29 +01:00
continue;
}
2015-11-26 09:06:29 +01:00
if (!lock)
{
lock.lock();
continue;
}
thread_ctrl::wait();
2015-11-26 09:06:29 +01:00
}
}
void cpu_thread::on_stop()
{
state += cpu_state::exit;
lock_notify();
}
cpu_thread::~cpu_thread()
{
}
2016-06-25 15:54:08 +02:00
cpu_thread::cpu_thread(cpu_type type)
: type(type)
{
}
bool cpu_thread::check_state()
2015-07-01 00:25:52 +02:00
{
std::unique_lock<named_thread> lock(*this, 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
2016-08-07 21:01:27 +02:00
if (test(state & cpu_state::exit))
2015-11-26 09:06:29 +01:00
{
return true;
}
2016-08-07 21:01:27 +02:00
if (!test(state & cpu_state_pause))
2015-08-10 21:39:52 +02:00
{
break;
}
if (!lock)
{
lock.lock();
continue;
}
thread_ctrl::wait();
}
2016-04-14 00:59:00 +02:00
const auto state_ = state.load();
2016-08-07 21:01:27 +02:00
if (test(state_, cpu_state::ret + cpu_state::stop))
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
2016-08-07 21:01:27 +02:00
if (test(state_, cpu_state::dbg_step))
2015-07-01 00:25:52 +02:00
{
2016-04-14 00:59:00 +02:00
state += cpu_state::dbg_pause;
state -= cpu_state::dbg_step;
2014-10-07 23:37:04 +02:00
}
2015-07-01 00:25:52 +02:00
return false;
}
void cpu_thread::run()
{
state -= cpu_state::stop;
lock_notify();
}