cpu_manager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "common/fiber.h"
  11. #include "common/thread.h"
  12. #include "core/hardware_properties.h"
  13. namespace Common {
  14. class Event;
  15. class Fiber;
  16. } // namespace Common
  17. namespace Core {
  18. class System;
  19. class CpuManager {
  20. public:
  21. explicit CpuManager(System& system);
  22. CpuManager(const CpuManager&) = delete;
  23. CpuManager(CpuManager&&) = delete;
  24. ~CpuManager();
  25. CpuManager& operator=(const CpuManager&) = delete;
  26. CpuManager& operator=(CpuManager&&) = delete;
  27. /// Sets if emulation is multicore or single core, must be set before Initialize
  28. void SetMulticore(bool is_multicore) {
  29. this->is_multicore = is_multicore;
  30. }
  31. /// Sets if emulation is using an asynchronous GPU.
  32. void SetAsyncGpu(bool is_async_gpu) {
  33. this->is_async_gpu = is_async_gpu;
  34. }
  35. void Initialize();
  36. void Shutdown();
  37. void Pause(bool paused);
  38. static std::function<void(void*)> GetGuestThreadStartFunc();
  39. static std::function<void(void*)> GetIdleThreadStartFunc();
  40. static std::function<void(void*)> GetSuspendThreadStartFunc();
  41. void* GetStartFuncParamater();
  42. void PreemptSingleCore(bool from_running_enviroment = true);
  43. std::size_t CurrentCore() const {
  44. return current_core.load();
  45. }
  46. private:
  47. static void GuestThreadFunction(void* cpu_manager);
  48. static void GuestRewindFunction(void* cpu_manager);
  49. static void IdleThreadFunction(void* cpu_manager);
  50. static void SuspendThreadFunction(void* cpu_manager);
  51. void MultiCoreRunGuestThread();
  52. void MultiCoreRunGuestLoop();
  53. void MultiCoreRunIdleThread();
  54. void MultiCoreRunSuspendThread();
  55. void MultiCorePause(bool paused);
  56. void SingleCoreRunGuestThread();
  57. void SingleCoreRunGuestLoop();
  58. void SingleCoreRunIdleThread();
  59. void SingleCoreRunSuspendThread();
  60. void SingleCorePause(bool paused);
  61. static void ThreadStart(CpuManager& cpu_manager, std::size_t core);
  62. void RunThread(std::size_t core);
  63. struct CoreData {
  64. std::shared_ptr<Common::Fiber> host_context;
  65. std::unique_ptr<Common::Event> enter_barrier;
  66. std::unique_ptr<Common::Event> exit_barrier;
  67. std::atomic<bool> is_running;
  68. std::atomic<bool> is_paused;
  69. std::atomic<bool> initialized;
  70. std::unique_ptr<std::thread> host_thread;
  71. };
  72. std::atomic<bool> running_mode{};
  73. std::atomic<bool> paused_state{};
  74. std::array<CoreData, Core::Hardware::NUM_CPU_CORES> core_data{};
  75. bool is_async_gpu{};
  76. bool is_multicore{};
  77. std::atomic<std::size_t> current_core{};
  78. std::size_t idle_count{};
  79. static constexpr std::size_t max_cycle_runs = 5;
  80. System& system;
  81. };
  82. } // namespace Core