2022-04-23 11:59:50 +03:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-01-26 21:07:22 +03:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
2020-03-10 18:50:33 +03:00
|
|
|
#include <atomic>
|
2020-02-25 05:04:12 +03:00
|
|
|
#include <functional>
|
2020-01-26 21:07:22 +03:00
|
|
|
#include <memory>
|
2020-02-25 05:04:12 +03:00
|
|
|
#include <thread>
|
2020-07-16 20:22:55 +03:00
|
|
|
|
|
|
|
#include "common/fiber.h"
|
|
|
|
#include "common/thread.h"
|
2020-02-12 02:56:24 +03:00
|
|
|
#include "core/hardware_properties.h"
|
2020-01-26 21:07:22 +03:00
|
|
|
|
2020-02-25 05:04:12 +03:00
|
|
|
namespace Common {
|
|
|
|
class Event;
|
|
|
|
class Fiber;
|
|
|
|
} // namespace Common
|
|
|
|
|
2020-01-26 21:07:22 +03:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
class System;
|
|
|
|
|
|
|
|
class CpuManager {
|
|
|
|
public:
|
2021-05-16 08:46:30 +03:00
|
|
|
explicit CpuManager(System& system_);
|
2020-01-26 21:07:22 +03:00
|
|
|
CpuManager(const CpuManager&) = delete;
|
|
|
|
CpuManager(CpuManager&&) = delete;
|
|
|
|
|
|
|
|
~CpuManager();
|
|
|
|
|
|
|
|
CpuManager& operator=(const CpuManager&) = delete;
|
|
|
|
CpuManager& operator=(CpuManager&&) = delete;
|
|
|
|
|
2020-03-09 05:39:41 +03:00
|
|
|
/// Sets if emulation is multicore or single core, must be set before Initialize
|
2021-05-03 05:14:15 +03:00
|
|
|
void SetMulticore(bool is_multi) {
|
|
|
|
is_multicore = is_multi;
|
2020-03-09 05:39:41 +03:00
|
|
|
}
|
2020-03-16 04:34:22 +03:00
|
|
|
|
|
|
|
/// Sets if emulation is using an asynchronous GPU.
|
2021-05-03 05:14:15 +03:00
|
|
|
void SetAsyncGpu(bool is_async) {
|
|
|
|
is_async_gpu = is_async;
|
2020-03-16 04:34:22 +03:00
|
|
|
}
|
|
|
|
|
2020-01-26 21:07:22 +03:00
|
|
|
void Initialize();
|
|
|
|
void Shutdown();
|
|
|
|
|
2020-02-25 05:04:12 +03:00
|
|
|
void Pause(bool paused);
|
|
|
|
|
2020-07-16 20:28:10 +03:00
|
|
|
static std::function<void(void*)> GetGuestThreadStartFunc();
|
|
|
|
static std::function<void(void*)> GetIdleThreadStartFunc();
|
|
|
|
static std::function<void(void*)> GetSuspendThreadStartFunc();
|
2020-02-25 05:04:12 +03:00
|
|
|
void* GetStartFuncParamater();
|
2020-01-26 21:07:22 +03:00
|
|
|
|
2020-03-28 22:23:28 +03:00
|
|
|
void PreemptSingleCore(bool from_running_enviroment = true);
|
2020-03-10 20:13:39 +03:00
|
|
|
|
2020-03-09 05:39:41 +03:00
|
|
|
std::size_t CurrentCore() const {
|
2020-03-10 18:50:33 +03:00
|
|
|
return current_core.load();
|
2020-03-09 05:39:41 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 05:04:12 +03:00
|
|
|
private:
|
|
|
|
static void GuestThreadFunction(void* cpu_manager);
|
2020-02-28 02:12:41 +03:00
|
|
|
static void GuestRewindFunction(void* cpu_manager);
|
2020-02-25 05:04:12 +03:00
|
|
|
static void IdleThreadFunction(void* cpu_manager);
|
|
|
|
static void SuspendThreadFunction(void* cpu_manager);
|
2020-01-26 21:07:22 +03:00
|
|
|
|
2020-03-09 05:39:41 +03:00
|
|
|
void MultiCoreRunGuestThread();
|
|
|
|
void MultiCoreRunGuestLoop();
|
|
|
|
void MultiCoreRunIdleThread();
|
|
|
|
void MultiCoreRunSuspendThread();
|
|
|
|
|
|
|
|
void SingleCoreRunGuestThread();
|
|
|
|
void SingleCoreRunGuestLoop();
|
|
|
|
void SingleCoreRunIdleThread();
|
|
|
|
void SingleCoreRunSuspendThread();
|
2020-01-26 21:07:22 +03:00
|
|
|
|
2021-08-07 08:32:48 +03:00
|
|
|
static void ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager, std::size_t core);
|
2020-01-26 21:07:22 +03:00
|
|
|
|
2021-08-07 08:32:48 +03:00
|
|
|
void RunThread(std::stop_token stop_token, std::size_t core);
|
2020-02-25 05:04:12 +03:00
|
|
|
|
|
|
|
struct CoreData {
|
2021-03-06 04:08:17 +03:00
|
|
|
std::shared_ptr<Common::Fiber> host_context;
|
2021-08-07 08:32:48 +03:00
|
|
|
std::jthread host_thread;
|
2020-02-25 05:04:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
std::atomic<bool> running_mode{};
|
2022-05-28 03:44:45 +03:00
|
|
|
std::atomic<bool> pause_state{};
|
|
|
|
std::unique_ptr<Common::Barrier> pause_barrier{};
|
|
|
|
std::mutex pause_lock{};
|
2020-02-25 05:04:12 +03:00
|
|
|
|
|
|
|
std::array<CoreData, Core::Hardware::NUM_CPU_CORES> core_data{};
|
2020-01-26 21:07:22 +03:00
|
|
|
|
2020-03-16 04:34:22 +03:00
|
|
|
bool is_async_gpu{};
|
2020-03-09 05:39:41 +03:00
|
|
|
bool is_multicore{};
|
2020-03-10 18:50:33 +03:00
|
|
|
std::atomic<std::size_t> current_core{};
|
2020-03-19 20:09:32 +03:00
|
|
|
std::size_t idle_count{};
|
2020-03-09 05:39:41 +03:00
|
|
|
static constexpr std::size_t max_cycle_runs = 5;
|
|
|
|
|
2020-01-26 21:07:22 +03:00
|
|
|
System& system;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Core
|