rpcsx/rpcs3/Emu/CPU/CPUThread.h

183 lines
3.7 KiB
C
Raw Normal View History

#pragma once
2015-07-01 00:25:52 +02:00
2016-06-07 22:24:20 +02:00
#include "../Utilities/Thread.h"
2016-08-07 21:01:27 +02:00
#include "../Utilities/bit_set.h"
#include <vector>
2016-08-13 16:58:19 +02:00
// Thread state flags
enum class cpu_flag : 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
wait, // Indicates waiting state, set by the thread itself
pause, // Thread suspended by suspend_all technique
2017-02-06 19:36:46 +01:00
suspend, // Thread suspended
2016-04-14 00:59:00 +02:00
ret, // Callback return requested
signal, // Thread received a signal (HLE)
2017-03-11 00:14:48 +01:00
memory, // Thread must unlock memory mutex
2016-04-14 00:59:00 +02:00
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)
2016-08-07 21:01:27 +02:00
__bitset_enum_max
2015-03-16 22:38:21 +01:00
};
class cpu_thread
{
public:
u64 block_hash = 0;
protected:
cpu_thread(u32 id);
2015-07-01 00:25:52 +02:00
public:
virtual ~cpu_thread();
void operator()();
// Self identifier
2017-01-25 18:50:30 +01:00
const u32 id;
2016-06-25 15:54:08 +02:00
2016-04-14 00:59:00 +02:00
// Public thread state
atomic_bs_t<cpu_flag> state{cpu_flag::stop + cpu_flag::wait};
2016-04-14 00:59:00 +02:00
2016-05-13 16:01:48 +02:00
// Process thread state, return true if the checker must return
bool check_state() noexcept;
// Process thread state (pause)
[[nodiscard]] bool test_stopped()
{
if (state)
{
if (check_state())
{
return true;
}
}
return false;
}
// Test stopped state
bool is_stopped() const
{
return !!(state & (cpu_flag::stop + cpu_flag::exit + cpu_flag::dbg_global_stop));
}
// Test paused state
bool is_paused() const
{
return !!(state & (cpu_flag::suspend + cpu_flag::dbg_global_pause + cpu_flag::dbg_pause));
}
2017-01-25 18:50:30 +01:00
// Check thread type
u32 id_type() const
2017-01-25 18:50:30 +01:00
{
return id >> 24;
}
void notify();
private:
void abort();
public:
// Thread stats for external observation
static atomic_t<u64> g_threads_created, g_threads_deleted, g_suspend_counter;
// Get thread name (as assigned to named_thread)
std::string get_name() const;
// Get CPU state dump (everything)
virtual std::string dump_all() const = 0;
// Get CPU register dump
virtual std::string dump_regs() const;
// Get CPU call stack dump
virtual std::string dump_callstack() const;
// Get CPU call stack list
virtual std::vector<std::pair<u32, u32>> dump_callstack_list() const;
// Get CPU dump of misc information
virtual std::string dump_misc() const;
2016-08-13 16:58:19 +02:00
// Thread entry point function
2016-04-14 00:59:00 +02:00
virtual void cpu_task() = 0;
2017-02-06 19:36:46 +01:00
// Callback for cpu_flag::suspend
virtual void cpu_sleep() {}
2018-04-03 16:19:07 +02:00
// Callback for cpu_flag::memory
virtual void cpu_mem() {}
// Callback for vm::temporary_unlock
virtual void cpu_unmem() {}
// Callback for cpu_flag::ret
virtual void cpu_return() {}
// For internal use
struct suspend_work
{
void* func_ptr;
void* res_buf;
// Type-erased op executor
void (*exec)(void* func, void* res);
// Next object in the linked list
suspend_work* next;
// Internal method
void push(cpu_thread* _this) noexcept;
};
2019-08-20 18:07:03 +02:00
// Suspend all threads and execute op (may be executed by other thread than caller!)
template <typename F>
static auto suspend_all(cpu_thread* _this, F op)
{
if constexpr (std::is_void_v<std::invoke_result_t<F>>)
{
suspend_work work{&op, nullptr, [](void* func, void*)
{
(*static_cast<F*>(func))();
}};
work.push(_this);
return;
}
else
{
std::invoke_result_t<F> result;
suspend_work work{&op, &result, [](void* func, void* res_buf)
{
*static_cast<std::invoke_result_t<F>*>(res_buf) = (*static_cast<F*>(func))();
}};
work.push(_this);
return result;
}
}
2019-08-20 18:07:03 +02:00
// Stop all threads with cpu_flag::dbg_global_stop
static void stop_all() noexcept;
// Send signal to the profiler(s) to flush results
static void flush_profilers() noexcept;
};
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;
}
class ppu_thread;
class spu_thread;