cpu_manager.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/thread.h"
  11. #include "core/hardware_properties.h"
  12. namespace Common {
  13. class Event;
  14. class Fiber;
  15. } // namespace Common
  16. namespace Core {
  17. class System;
  18. class CpuManager {
  19. public:
  20. explicit CpuManager(System& system_);
  21. CpuManager(const CpuManager&) = delete;
  22. CpuManager(CpuManager&&) = delete;
  23. ~CpuManager();
  24. CpuManager& operator=(const CpuManager&) = delete;
  25. CpuManager& operator=(CpuManager&&) = delete;
  26. /// Sets if emulation is multicore or single core, must be set before Initialize
  27. void SetMulticore(bool is_multi) {
  28. is_multicore = is_multi;
  29. }
  30. /// Sets if emulation is using an asynchronous GPU.
  31. void SetAsyncGpu(bool is_async) {
  32. is_async_gpu = is_async;
  33. }
  34. void OnGpuReady() {
  35. gpu_barrier->Sync();
  36. }
  37. void Initialize();
  38. void Shutdown();
  39. std::function<void()> GetGuestActivateFunc() {
  40. return [this] { GuestActivate(); };
  41. }
  42. std::function<void()> GetGuestThreadFunc() {
  43. return [this] { GuestThreadFunction(); };
  44. }
  45. std::function<void()> GetIdleThreadStartFunc() {
  46. return [this] { IdleThreadFunction(); };
  47. }
  48. std::function<void()> GetShutdownThreadStartFunc() {
  49. return [this] { ShutdownThreadFunction(); };
  50. }
  51. void PreemptSingleCore(bool from_running_enviroment = true);
  52. std::size_t CurrentCore() const {
  53. return current_core.load();
  54. }
  55. private:
  56. void GuestThreadFunction();
  57. void IdleThreadFunction();
  58. void ShutdownThreadFunction();
  59. void MultiCoreRunGuestThread();
  60. void MultiCoreRunIdleThread();
  61. void SingleCoreRunGuestThread();
  62. void SingleCoreRunIdleThread();
  63. static void ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager, std::size_t core);
  64. void GuestActivate();
  65. void HandleInterrupt();
  66. void ShutdownThread();
  67. void RunThread(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