core_cpu.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <atomic>
  6. #include <condition_variable>
  7. #include <cstddef>
  8. #include <memory>
  9. #include <mutex>
  10. #include "common/common_types.h"
  11. namespace Kernel {
  12. class GlobalScheduler;
  13. class Scheduler;
  14. } // namespace Kernel
  15. namespace Core {
  16. class System;
  17. }
  18. namespace Core::Timing {
  19. class CoreTiming;
  20. }
  21. namespace Core {
  22. class ARM_Interface;
  23. class ExclusiveMonitor;
  24. constexpr unsigned NUM_CPU_CORES{4};
  25. class CpuBarrier {
  26. public:
  27. bool IsAlive() const {
  28. return !end;
  29. }
  30. void NotifyEnd();
  31. bool Rendezvous();
  32. private:
  33. unsigned cores_waiting{NUM_CPU_CORES};
  34. std::mutex mutex;
  35. std::condition_variable condition;
  36. std::atomic<bool> end{};
  37. };
  38. class Cpu {
  39. public:
  40. Cpu(System& system, ExclusiveMonitor& exclusive_monitor, CpuBarrier& cpu_barrier,
  41. std::size_t core_index);
  42. ~Cpu();
  43. void RunLoop(bool tight_loop = true);
  44. void SingleStep();
  45. void PrepareReschedule();
  46. ARM_Interface& ArmInterface() {
  47. return *arm_interface;
  48. }
  49. const ARM_Interface& ArmInterface() const {
  50. return *arm_interface;
  51. }
  52. Kernel::Scheduler& Scheduler() {
  53. return *scheduler;
  54. }
  55. const Kernel::Scheduler& Scheduler() const {
  56. return *scheduler;
  57. }
  58. bool IsMainCore() const {
  59. return core_index == 0;
  60. }
  61. std::size_t CoreIndex() const {
  62. return core_index;
  63. }
  64. static std::unique_ptr<ExclusiveMonitor> MakeExclusiveMonitor(std::size_t num_cores);
  65. private:
  66. void Reschedule();
  67. std::unique_ptr<ARM_Interface> arm_interface;
  68. CpuBarrier& cpu_barrier;
  69. Kernel::GlobalScheduler& global_scheduler;
  70. std::unique_ptr<Kernel::Scheduler> scheduler;
  71. Timing::CoreTiming& core_timing;
  72. std::atomic<bool> reschedule_pending = false;
  73. std::size_t core_index;
  74. };
  75. } // namespace Core