cpu_manager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <atomic>
  6. #include <csetjmp>
  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_multi) {
  29. is_multicore = is_multi;
  30. }
  31. /// Sets if emulation is using an asynchronous GPU.
  32. void SetAsyncGpu(bool is_async) {
  33. is_async_gpu = is_async;
  34. }
  35. void OnGpuReady() {
  36. gpu_barrier->Sync();
  37. }
  38. void WaitForAndHandleInterrupt();
  39. void Initialize();
  40. void Shutdown();
  41. std::function<void()> GetGuestActivateFunc() {
  42. return [this] { GuestActivateFunction(); };
  43. }
  44. std::function<void()> GetGuestThreadFunc() {
  45. return [this] { GuestThreadFunction(); };
  46. }
  47. std::function<void()> GetIdleThreadStartFunc() {
  48. return [this] { IdleThreadFunction(); };
  49. }
  50. std::function<void()> GetShutdownThreadStartFunc() {
  51. return [this] { ShutdownThreadFunction(); };
  52. }
  53. void PreemptSingleCore(bool from_running_enviroment = true);
  54. std::size_t CurrentCore() const {
  55. return current_core.load();
  56. }
  57. private:
  58. void GuestActivateFunction();
  59. void GuestThreadFunction();
  60. void IdleThreadFunction();
  61. void ShutdownThreadFunction();
  62. void MultiCoreGuestActivate();
  63. void MultiCoreRunGuestThread();
  64. void MultiCoreRunGuestLoop();
  65. void SingleCoreGuestActivate();
  66. void SingleCoreRunGuestThread();
  67. void SingleCoreRunGuestLoop();
  68. static void ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager, std::size_t core);
  69. void HandleInterrupt();
  70. void ShutdownThread();
  71. void RunThread(std::size_t core);
  72. struct CoreData {
  73. std::shared_ptr<Common::Fiber> host_context;
  74. std::jthread host_thread;
  75. };
  76. std::unique_ptr<Common::Barrier> gpu_barrier{};
  77. std::array<CoreData, Core::Hardware::NUM_CPU_CORES> core_data{};
  78. bool is_async_gpu{};
  79. bool is_multicore{};
  80. std::atomic<std::size_t> current_core{};
  81. std::size_t idle_count{};
  82. std::size_t num_cores{};
  83. static constexpr std::size_t max_cycle_runs = 5;
  84. System& system;
  85. };
  86. } // namespace Core