2013-11-03 20:23:16 +01:00
|
|
|
#include "stdafx.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#include "Emu/System.h"
|
2015-07-01 00:25:52 +02:00
|
|
|
#include "Emu/IdManager.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
#include "Emu/Cell/PPUThread.h"
|
|
|
|
|
#include "Emu/Cell/SPUThread.h"
|
|
|
|
|
#include "Emu/Cell/RawSPUThread.h"
|
|
|
|
|
#include "Emu/ARMv7/ARMv7Thread.h"
|
2013-11-03 20:23:16 +01:00
|
|
|
#include "CPUThread.h"
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
thread_local cpu_thread* g_tls_current_cpu_thread = nullptr;
|
2015-09-26 22:46:04 +02:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
void cpu_thread::on_task()
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
state -= cpu_state::exit;
|
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
g_tls_current_cpu_thread = this;
|
|
|
|
|
|
|
|
|
|
Emu.SendDbgCommand(DID_CREATE_THREAD, this);
|
2015-09-26 22:46:04 +02:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
std::unique_lock<std::mutex> lock(get_current_thread_mutex());
|
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-02-18 17:22:06 +01:00
|
|
|
{
|
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-02-18 17:22:06 +01:00
|
|
|
{
|
2015-11-26 09:06:29 +01:00
|
|
|
cpu_task();
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
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
|
|
|
}
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
state -= cpu_state::ret;
|
2015-11-26 09:06:29 +01:00
|
|
|
continue;
|
2015-02-18 17:22:06 +01:00
|
|
|
}
|
2015-11-26 09:06:29 +01:00
|
|
|
|
|
|
|
|
if (!lock)
|
|
|
|
|
{
|
|
|
|
|
lock.lock();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait(lock);
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
void cpu_thread::on_stop()
|
|
|
|
|
{
|
|
|
|
|
state += cpu_state::exit;
|
|
|
|
|
lock_notify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cpu_thread::~cpu_thread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cpu_thread::cpu_thread(cpu_type type, const std::string& name)
|
|
|
|
|
: type(type)
|
|
|
|
|
, name(name)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
bool cpu_thread::check_status()
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
std::unique_lock<std::mutex> lock(get_current_thread_mutex(), std::defer_lock);
|
2014-07-13 20:55:14 +02:00
|
|
|
|
2015-02-18 17:22:06 +01: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-02-18 17:22:06 +01:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
if (state & cpu_state::exit)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
if (!state.test(cpu_state_pause) && !state.test(cpu_state::interrupt))
|
2015-08-10 21:39:52 +02:00
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2015-07-08 00:33:24 +02:00
|
|
|
if (!lock)
|
|
|
|
|
{
|
|
|
|
|
lock.lock();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
if (!state.test(cpu_state_pause) && state & cpu_state::interrupt && handle_interrupt())
|
2015-08-10 21:39:52 +02:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
get_current_thread_cv().wait(lock);
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
const auto state_ = state.load();
|
|
|
|
|
|
|
|
|
|
if (state_ & to_mset(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;
|
2013-11-03 20:23:16 +01:00
|
|
|
}
|
2016-04-14 00:59:00 +02:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
[[noreturn]] void cpu_thread::xsleep()
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("cpu_thread: sleep()/awake() inconsistency");
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
std::vector<std::shared_ptr<cpu_thread>> get_all_cpu_threads()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::shared_ptr<cpu_thread>> result;
|
|
|
|
|
|
|
|
|
|
for (auto& t : idm::get_all<PPUThread>())
|
|
|
|
|
{
|
|
|
|
|
result.emplace_back(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& t : idm::get_all<SPUThread>())
|
|
|
|
|
{
|
|
|
|
|
result.emplace_back(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& t : idm::get_all<RawSPUThread>())
|
|
|
|
|
{
|
|
|
|
|
result.emplace_back(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& t : idm::get_all<ARMv7Thread>())
|
|
|
|
|
{
|
|
|
|
|
result.emplace_back(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|