rpcsx/rpcs3/Emu/CPU/CPUThread.cpp

142 lines
2.2 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_flag>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](cpu_flag f)
2016-08-07 21:01:27 +02:00
{
switch (f)
{
STR_CASE(cpu_flag::stop);
STR_CASE(cpu_flag::exit);
STR_CASE(cpu_flag::suspend);
STR_CASE(cpu_flag::ret);
STR_CASE(cpu_flag::signal);
STR_CASE(cpu_flag::dbg_global_pause);
STR_CASE(cpu_flag::dbg_global_stop);
STR_CASE(cpu_flag::dbg_pause);
STR_CASE(cpu_flag::dbg_step);
case cpu_flag::__bitset_enum_max: break;
2016-08-07 21:01:27 +02:00
}
return unknown;
});
}
template<>
void fmt_class_string<bs_t<cpu_flag>>::format(std::string& out, u64 arg)
2016-08-07 21:01:27 +02:00
{
format_bitset(out, arg, "[", "|", "]", &fmt_class_string<cpu_flag>::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_flag::exit;
2015-11-26 09:06:29 +01:00
g_tls_current_cpu_thread = this;
Emu.SendDbgCommand(DID_CREATE_THREAD, this);
2016-04-14 00:59:00 +02:00
// Check thread status
while (!test(state & cpu_flag::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
if (!test(state & cpu_flag::stop))
{
2015-11-26 09:06:29 +01:00
try
{
2015-11-26 09:06:29 +01:00
cpu_task();
}
catch (cpu_flag _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
}
state -= cpu_flag::ret;
2015-11-26 09:06:29 +01:00
continue;
}
2015-11-26 09:06:29 +01:00
thread_ctrl::wait();
2015-11-26 09:06:29 +01:00
}
}
void cpu_thread::on_stop()
{
state += cpu_flag::exit;
notify();
}
cpu_thread::~cpu_thread()
{
}
2017-01-25 18:50:30 +01:00
cpu_thread::cpu_thread(u32 id)
: id(id)
{
}
bool cpu_thread::check_state()
2015-07-01 00:25:52 +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
if (test(state & cpu_flag::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;
}
thread_ctrl::wait();
}
2016-04-14 00:59:00 +02:00
const auto state_ = state.load();
if (test(state_, cpu_flag::ret + cpu_flag::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
if (test(state_, cpu_flag::dbg_step))
2015-07-01 00:25:52 +02:00
{
state += cpu_flag::dbg_pause;
state -= cpu_flag::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_flag::stop;
notify();
}
2016-08-13 16:58:19 +02:00
2016-08-15 02:11:49 +02:00
void cpu_thread::set_signal()
{
verify("cpu_flag::signal" HERE), !state.test_and_set(cpu_flag::signal);
2017-01-25 00:22:19 +01:00
notify();
2016-08-15 02:11:49 +02:00
}
2016-08-13 16:58:19 +02:00
std::string cpu_thread::dump() const
{
return fmt::format("Type: %s\n" "State: %s\n", typeid(*this).name(), state.load());
}