cpu_manager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <functional>
  8. #include <memory>
  9. #include <thread>
  10. #include "core/hardware_properties.h"
  11. namespace Common {
  12. class Event;
  13. class Fiber;
  14. } // namespace Common
  15. namespace Core::Frontend {
  16. class EmuWindow;
  17. } // namespace Core::Frontend
  18. namespace Core {
  19. class System;
  20. class CpuManager {
  21. public:
  22. explicit CpuManager(System& system);
  23. CpuManager(const CpuManager&) = delete;
  24. CpuManager(CpuManager&&) = delete;
  25. ~CpuManager();
  26. CpuManager& operator=(const CpuManager&) = delete;
  27. CpuManager& operator=(CpuManager&&) = delete;
  28. /// Sets if emulation is multicore or single core, must be set before Initialize
  29. void SetMulticore(bool is_multicore) {
  30. this->is_multicore = is_multicore;
  31. }
  32. /// Sets if emulation is using an asynchronous GPU.
  33. void SetAsyncGpu(bool is_async_gpu) {
  34. this->is_async_gpu = is_async_gpu;
  35. }
  36. void Initialize();
  37. void Shutdown();
  38. void Pause(bool paused);
  39. std::function<void(void*)> GetGuestThreadStartFunc();
  40. std::function<void(void*)> GetIdleThreadStartFunc();
  41. std::function<void(void*)> GetSuspendThreadStartFunc();
  42. void* GetStartFuncParamater();
  43. void PreemptSingleCore();
  44. std::size_t CurrentCore() const {
  45. return current_core.load();
  46. }
  47. void SetRenderWindow(Core::Frontend::EmuWindow& render_window);
  48. private:
  49. static void GuestThreadFunction(void* cpu_manager);
  50. static void GuestRewindFunction(void* cpu_manager);
  51. static void IdleThreadFunction(void* cpu_manager);
  52. static void SuspendThreadFunction(void* cpu_manager);
  53. void MultiCoreRunGuestThread();
  54. void MultiCoreRunGuestLoop();
  55. void MultiCoreRunIdleThread();
  56. void MultiCoreRunSuspendThread();
  57. void MultiCorePause(bool paused);
  58. void SingleCoreRunGuestThread();
  59. void SingleCoreRunGuestLoop();
  60. void SingleCoreRunIdleThread();
  61. void SingleCoreRunSuspendThread();
  62. void SingleCorePause(bool paused);
  63. static void ThreadStart(CpuManager& cpu_manager, std::size_t core);
  64. void RunThread(std::size_t core);
  65. struct CoreData {
  66. std::shared_ptr<Common::Fiber> host_context;
  67. std::unique_ptr<Common::Event> enter_barrier;
  68. std::unique_ptr<Common::Event> exit_barrier;
  69. std::atomic<bool> is_running;
  70. std::atomic<bool> is_paused;
  71. std::atomic<bool> initialized;
  72. std::unique_ptr<std::thread> host_thread;
  73. };
  74. std::atomic<bool> running_mode{};
  75. std::atomic<bool> paused_state{};
  76. std::array<CoreData, Core::Hardware::NUM_CPU_CORES> core_data{};
  77. bool is_async_gpu{};
  78. bool is_multicore{};
  79. std::atomic<std::size_t> current_core{};
  80. std::size_t preemption_count{};
  81. static constexpr std::size_t max_cycle_runs = 5;
  82. Core::Frontend::EmuWindow* render_window;
  83. System& system;
  84. };
  85. } // namespace Core