rpcsx/rpcs3/Emu/CPU/CPUThread.cpp

120 lines
1.6 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>
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
while (!(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-04-14 00:59:00 +02:00
if (!(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-04-14 00:59:00 +02:00
if (state & cpu_state::exit)
2015-11-26 09:06:29 +01:00
{
return true;
}
if (!state.test(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();
if (state_ & make_bitset(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-04-14 00:59:00 +02:00
if (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();
}