core_cpu.h 1.7 KB

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