2013-11-03 20:23:16 +01:00
|
|
|
#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"
|
2014-08-25 16:56:13 +02:00
|
|
|
|
2016-08-13 16:58:19 +02:00
|
|
|
// Thread state flags
|
2016-08-09 16:14:41 +02:00
|
|
|
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
|
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
|
|
|
};
|
|
|
|
|
|
2016-08-13 16:58:19 +02:00
|
|
|
// Flag set for pause state
|
2016-08-09 16:14:41 +02:00
|
|
|
constexpr bs_t<cpu_flag> cpu_state_pause = cpu_flag::suspend + cpu_flag::dbg_global_pause + cpu_flag::dbg_pause;
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
class cpu_thread : public named_thread
|
2013-11-03 20:23:16 +01:00
|
|
|
{
|
2016-08-13 16:58:19 +02:00
|
|
|
void on_task() override final;
|
2013-11-05 19:12:18 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
public:
|
2016-04-25 12:49:12 +02:00
|
|
|
virtual void on_stop() override;
|
|
|
|
|
virtual ~cpu_thread() override;
|
2013-11-03 20:23:16 +01:00
|
|
|
|
2017-01-25 18:50:30 +01:00
|
|
|
const u32 id;
|
2016-06-25 15:54:08 +02:00
|
|
|
|
2017-01-25 18:50:30 +01:00
|
|
|
cpu_thread(u32 id);
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
// Public thread state
|
2017-03-11 00:14:48 +01:00
|
|
|
atomic_t<bs_t<cpu_flag>> state{+cpu_flag::stop};
|
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
|
2016-07-27 23:43:22 +02:00
|
|
|
bool check_state();
|
|
|
|
|
|
2017-02-22 11:10:55 +01:00
|
|
|
// Process thread state
|
|
|
|
|
void test_state();
|
|
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
// Run thread
|
|
|
|
|
void run();
|
|
|
|
|
|
2017-01-25 18:50:30 +01:00
|
|
|
// Check thread type
|
|
|
|
|
u32 id_type()
|
|
|
|
|
{
|
|
|
|
|
return id >> 24;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-09 23:51:29 +01:00
|
|
|
// Thread stats for external observation
|
|
|
|
|
static atomic_t<u64> g_threads_created, g_threads_deleted;
|
|
|
|
|
|
2016-08-13 16:58:19 +02:00
|
|
|
// Print CPU state
|
|
|
|
|
virtual std::string dump() const;
|
|
|
|
|
|
|
|
|
|
// 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() {}
|
2013-11-03 20:23:16 +01:00
|
|
|
};
|
|
|
|
|
|
2016-04-14 00:59:00 +02:00
|
|
|
inline cpu_thread* get_current_cpu_thread() noexcept
|
2015-09-26 22:46:04 +02:00
|
|
|
{
|
2016-04-14 00:59:00 +02:00
|
|
|
extern thread_local cpu_thread* g_tls_current_cpu_thread;
|
2015-09-26 22:46:04 +02:00
|
|
|
|
|
|
|
|
return g_tls_current_cpu_thread;
|
|
|
|
|
}
|