cpu_manager.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <functional>
  7. #include <memory>
  8. #include <thread>
  9. #include "common/fiber.h"
  10. #include "common/polyfill_thread.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 Initialize();
  39. void Shutdown();
  40. std::function<void()> GetGuestActivateFunc() {
  41. return [this] { GuestActivate(); };
  42. }
  43. std::function<void()> GetGuestThreadFunc() {
  44. return [this] { GuestThreadFunction(); };
  45. }
  46. std::function<void()> GetIdleThreadStartFunc() {
  47. return [this] { IdleThreadFunction(); };
  48. }
  49. std::function<void()> GetShutdownThreadStartFunc() {
  50. return [this] { ShutdownThreadFunction(); };
  51. }
  52. void PreemptSingleCore(bool from_running_environment = true);
  53. std::size_t CurrentCore() const {
  54. return current_core.load();
  55. }
  56. private:
  57. void GuestThreadFunction();
  58. void IdleThreadFunction();
  59. void ShutdownThreadFunction();
  60. void MultiCoreRunGuestThread();
  61. void MultiCoreRunIdleThread();
  62. void SingleCoreRunGuestThread();
  63. void SingleCoreRunIdleThread();
  64. void GuestActivate();
  65. void HandleInterrupt();
  66. void ShutdownThread();
  67. void RunThread(std::stop_token stop_token, std::size_t core);
  68. struct CoreData {
  69. std::shared_ptr<Common::Fiber> host_context;
  70. std::jthread host_thread;
  71. };
  72. std::unique_ptr<Common::Barrier> gpu_barrier{};
  73. std::array<CoreData, Core::Hardware::NUM_CPU_CORES> core_data{};
  74. bool is_async_gpu{};
  75. bool is_multicore{};
  76. std::atomic<std::size_t> current_core{};
  77. std::size_t idle_count{};
  78. std::size_t num_cores{};
  79. static constexpr std::size_t max_cycle_runs = 5;
  80. System& system;
  81. };
  82. } // namespace Core