core_cpu.h 1.8 KB

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