|
|
@ -16,31 +16,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
|
|
CpuManager::CpuManager(System& system_)
|
|
|
|
CpuManager::CpuManager(System& system_) : system{system_} {}
|
|
|
|
: pause_barrier{std::make_unique<Common::Barrier>(1)}, system{system_} {}
|
|
|
|
|
|
|
|
CpuManager::~CpuManager() = default;
|
|
|
|
CpuManager::~CpuManager() = default;
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager,
|
|
|
|
void CpuManager::ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager,
|
|
|
|
std::size_t core) {
|
|
|
|
std::size_t core) {
|
|
|
|
cpu_manager.RunThread(stop_token, core);
|
|
|
|
cpu_manager.RunThread(core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::Initialize() {
|
|
|
|
void CpuManager::Initialize() {
|
|
|
|
running_mode = true;
|
|
|
|
num_cores = is_multicore ? Core::Hardware::NUM_CPU_CORES : 1;
|
|
|
|
if (is_multicore) {
|
|
|
|
|
|
|
|
for (std::size_t core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) {
|
|
|
|
for (std::size_t core = 0; core < num_cores; core++) {
|
|
|
|
core_data[core].host_thread = std::jthread(ThreadStart, std::ref(*this), core);
|
|
|
|
core_data[core].host_thread = std::jthread(ThreadStart, std::ref(*this), core);
|
|
|
|
}
|
|
|
|
|
|
|
|
pause_barrier = std::make_unique<Common::Barrier>(Core::Hardware::NUM_CPU_CORES + 1);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
core_data[0].host_thread = std::jthread(ThreadStart, std::ref(*this), 0);
|
|
|
|
|
|
|
|
pause_barrier = std::make_unique<Common::Barrier>(2);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::Shutdown() {
|
|
|
|
void CpuManager::Shutdown() {
|
|
|
|
running_mode = false;
|
|
|
|
for (std::size_t core = 0; core < num_cores; core++) {
|
|
|
|
Pause(false);
|
|
|
|
if (core_data[core].host_thread.joinable()) {
|
|
|
|
|
|
|
|
core_data[core].host_thread.join();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::function<void(void*)> CpuManager::GetGuestThreadStartFunc() {
|
|
|
|
std::function<void(void*)> CpuManager::GetGuestThreadStartFunc() {
|
|
|
@ -51,8 +48,8 @@ std::function<void(void*)> CpuManager::GetIdleThreadStartFunc() {
|
|
|
|
return IdleThreadFunction;
|
|
|
|
return IdleThreadFunction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::function<void(void*)> CpuManager::GetSuspendThreadStartFunc() {
|
|
|
|
std::function<void(void*)> CpuManager::GetShutdownThreadStartFunc() {
|
|
|
|
return SuspendThreadFunction;
|
|
|
|
return ShutdownThreadFunction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::GuestThreadFunction(void* cpu_manager_) {
|
|
|
|
void CpuManager::GuestThreadFunction(void* cpu_manager_) {
|
|
|
@ -82,17 +79,12 @@ void CpuManager::IdleThreadFunction(void* cpu_manager_) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::SuspendThreadFunction(void* cpu_manager_) {
|
|
|
|
void CpuManager::ShutdownThreadFunction(void* cpu_manager) {
|
|
|
|
CpuManager* cpu_manager = static_cast<CpuManager*>(cpu_manager_);
|
|
|
|
static_cast<CpuManager*>(cpu_manager)->ShutdownThread();
|
|
|
|
if (cpu_manager->is_multicore) {
|
|
|
|
|
|
|
|
cpu_manager->MultiCoreRunSuspendThread();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
cpu_manager->SingleCoreRunSuspendThread();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* CpuManager::GetStartFuncParamater() {
|
|
|
|
void* CpuManager::GetStartFuncParameter() {
|
|
|
|
return static_cast<void*>(this);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
@ -134,21 +126,6 @@ void CpuManager::MultiCoreRunIdleThread() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::MultiCoreRunSuspendThread() {
|
|
|
|
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
|
|
|
|
kernel.CurrentScheduler()->OnThreadStart();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
auto core = kernel.CurrentPhysicalCoreIndex();
|
|
|
|
|
|
|
|
auto& scheduler = *kernel.CurrentScheduler();
|
|
|
|
|
|
|
|
Kernel::KThread* current_thread = scheduler.GetCurrentThread();
|
|
|
|
|
|
|
|
current_thread->DisableDispatch();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Common::Fiber::YieldTo(current_thread->GetHostContext(), *core_data[core].host_context);
|
|
|
|
|
|
|
|
ASSERT(core == kernel.CurrentPhysicalCoreIndex());
|
|
|
|
|
|
|
|
scheduler.RescheduleCurrentCore();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// SingleCore ///
|
|
|
|
/// SingleCore ///
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
@ -194,21 +171,6 @@ void CpuManager::SingleCoreRunIdleThread() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::SingleCoreRunSuspendThread() {
|
|
|
|
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
|
|
|
|
kernel.CurrentScheduler()->OnThreadStart();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
auto core = kernel.GetCurrentHostThreadID();
|
|
|
|
|
|
|
|
auto& scheduler = *kernel.CurrentScheduler();
|
|
|
|
|
|
|
|
Kernel::KThread* current_thread = scheduler.GetCurrentThread();
|
|
|
|
|
|
|
|
current_thread->DisableDispatch();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Common::Fiber::YieldTo(current_thread->GetHostContext(), *core_data[0].host_context);
|
|
|
|
|
|
|
|
ASSERT(core == kernel.GetCurrentHostThreadID());
|
|
|
|
|
|
|
|
scheduler.RescheduleCurrentCore();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::PreemptSingleCore(bool from_running_enviroment) {
|
|
|
|
void CpuManager::PreemptSingleCore(bool from_running_enviroment) {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
@ -241,24 +203,16 @@ void CpuManager::PreemptSingleCore(bool from_running_enviroment) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::Pause(bool paused) {
|
|
|
|
void CpuManager::ShutdownThread() {
|
|
|
|
std::scoped_lock lk{pause_lock};
|
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
|
|
|
|
auto core = is_multicore ? kernel.CurrentPhysicalCoreIndex() : 0;
|
|
|
|
|
|
|
|
auto* current_thread = kernel.GetCurrentEmuThread();
|
|
|
|
|
|
|
|
|
|
|
|
if (pause_state == paused) {
|
|
|
|
Common::Fiber::YieldTo(current_thread->GetHostContext(), *core_data[core].host_context);
|
|
|
|
return;
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the new state
|
|
|
|
|
|
|
|
pause_state.store(paused);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wake up any waiting threads
|
|
|
|
|
|
|
|
pause_state.notify_all();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for all threads to successfully change state before returning
|
|
|
|
|
|
|
|
pause_barrier->Sync();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuManager::RunThread(std::stop_token stop_token, std::size_t core) {
|
|
|
|
void CpuManager::RunThread(std::size_t core) {
|
|
|
|
/// Initialization
|
|
|
|
/// Initialization
|
|
|
|
system.RegisterCoreThread(core);
|
|
|
|
system.RegisterCoreThread(core);
|
|
|
|
std::string name;
|
|
|
|
std::string name;
|
|
|
@ -272,8 +226,6 @@ void CpuManager::RunThread(std::stop_token stop_token, std::size_t core) {
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
|
|
|
|
auto& data = core_data[core];
|
|
|
|
auto& data = core_data[core];
|
|
|
|
data.host_context = Common::Fiber::ThreadToFiber();
|
|
|
|
data.host_context = Common::Fiber::ThreadToFiber();
|
|
|
|
const bool sc_sync = !is_async_gpu && !is_multicore;
|
|
|
|
|
|
|
|
bool sc_sync_first_use = sc_sync;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
// Cleanup
|
|
|
|
SCOPE_EXIT({
|
|
|
|
SCOPE_EXIT({
|
|
|
@ -281,32 +233,13 @@ void CpuManager::RunThread(std::stop_token stop_token, std::size_t core) {
|
|
|
|
MicroProfileOnThreadExit();
|
|
|
|
MicroProfileOnThreadExit();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/// Running
|
|
|
|
// Running
|
|
|
|
while (running_mode) {
|
|
|
|
if (!is_async_gpu && !is_multicore) {
|
|
|
|
if (pause_state.load(std::memory_order_relaxed)) {
|
|
|
|
system.GPU().ObtainContext();
|
|
|
|
// Wait for caller to acknowledge pausing
|
|
|
|
|
|
|
|
pause_barrier->Sync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait until unpaused
|
|
|
|
|
|
|
|
pause_state.wait(true, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Wait for caller to acknowledge unpausing
|
|
|
|
|
|
|
|
pause_barrier->Sync();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (sc_sync_first_use) {
|
|
|
|
|
|
|
|
system.GPU().ObtainContext();
|
|
|
|
|
|
|
|
sc_sync_first_use = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Emulation was stopped
|
|
|
|
|
|
|
|
if (stop_token.stop_requested()) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread();
|
|
|
|
|
|
|
|
Common::Fiber::YieldTo(data.host_context, *current_thread->GetHostContext());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread();
|
|
|
|
|
|
|
|
Common::Fiber::YieldTo(data.host_context, *current_thread->GetHostContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Core
|
|
|
|
} // namespace Core
|
|
|
|