core_cpu.h 1.8 KB

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