rpcsx/rpcs3/Emu/CPU/CPUThread.h

87 lines
1.9 KiB
C
Raw Normal View History

#pragma once
2015-07-01 00:25:52 +02:00
#include "Utilities/Thread.h"
2016-04-14 00:59:00 +02:00
// CPU Thread Type
enum class cpu_type : u32
{
2016-04-14 00:59:00 +02:00
ppu, // PPU Thread
spu, // SPU Thread
arm, // ARMv7 Thread
};
2016-04-14 00:59:00 +02:00
// CPU Thread State flags
enum struct cpu_state : u32
2015-03-16 22:38:21 +01:00
{
2016-04-14 00:59:00 +02:00
stop, // Thread not running (HLE, initial state)
exit, // Irreversible exit
suspend, // Thread paused
ret, // Callback return requested
signal, // Thread received a signal (HLE)
interrupt, // Thread interrupted
dbg_global_pause, // Emulation paused
dbg_global_stop, // Emulation stopped
dbg_pause, // Thread paused
dbg_step, // Thread forced to pause after one step (one instruction, etc)
2015-03-16 22:38:21 +01:00
};
2016-04-14 00:59:00 +02:00
// CPU Thread State flags: pause state union
constexpr mset<cpu_state> cpu_state_pause = to_mset(cpu_state::suspend, cpu_state::dbg_global_pause, cpu_state::dbg_pause);
2015-07-01 00:25:52 +02:00
2016-04-14 00:59:00 +02:00
class cpu_thread : public named_thread
{
2015-11-26 09:06:29 +01:00
void on_task() override;
2015-07-01 00:25:52 +02:00
public:
virtual void on_stop() override;
virtual ~cpu_thread() override;
2016-04-14 00:59:00 +02:00
const std::string name;
const u32 id = -1;
2016-04-14 00:59:00 +02:00
const cpu_type type;
cpu_thread(cpu_type type, const std::string& name);
2016-04-14 00:59:00 +02:00
// Public thread state
atomic_t<mset<cpu_state>> state{ cpu_state::stop };
// Recursively enter sleep state
void sleep()
{
2016-04-14 00:59:00 +02:00
if (!++m_sleep) xsleep();
2015-07-01 00:25:52 +02:00
}
2016-04-14 00:59:00 +02:00
// Leave sleep state
void awake()
{
2016-04-14 00:59:00 +02:00
if (!m_sleep--) xsleep();
}
2016-04-14 00:59:00 +02:00
// Process thread state, return true if the checker must return
bool check_status();
virtual std::string dump() const = 0; // Print CPU state
virtual void cpu_init() {}
virtual void cpu_task() = 0;
virtual bool handle_interrupt() { return false; }
private:
[[noreturn]] void xsleep();
2016-04-14 00:59:00 +02:00
// Sleep/Awake counter
atomic_t<u32> m_sleep{};
};
extern std::mutex& get_current_thread_mutex();
extern std::condition_variable& get_current_thread_cv();
2016-04-14 00:59:00 +02:00
inline cpu_thread* get_current_cpu_thread() noexcept
{
2016-04-14 00:59:00 +02:00
extern thread_local cpu_thread* g_tls_current_cpu_thread;
return g_tls_current_cpu_thread;
}
2016-04-14 00:59:00 +02:00
extern std::vector<std::shared_ptr<cpu_thread>> get_all_cpu_threads();