core_cpu.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(std::shared_ptr<ExclusiveMonitor> exclusive_monitor,
  34. std::shared_ptr<CpuBarrier> cpu_barrier, std::size_t core_index);
  35. ~Cpu();
  36. void RunLoop(bool tight_loop = true);
  37. void SingleStep();
  38. void PrepareReschedule();
  39. ARM_Interface& ArmInterface() {
  40. return *arm_interface;
  41. }
  42. const ARM_Interface& ArmInterface() const {
  43. return *arm_interface;
  44. }
  45. const std::shared_ptr<Kernel::Scheduler>& Scheduler() const {
  46. return scheduler;
  47. }
  48. bool IsMainCore() const {
  49. return core_index == 0;
  50. }
  51. std::size_t CoreIndex() const {
  52. return core_index;
  53. }
  54. static std::shared_ptr<ExclusiveMonitor> MakeExclusiveMonitor(std::size_t num_cores);
  55. private:
  56. void Reschedule();
  57. std::shared_ptr<ARM_Interface> arm_interface;
  58. std::shared_ptr<CpuBarrier> cpu_barrier;
  59. std::shared_ptr<Kernel::Scheduler> scheduler;
  60. std::atomic<bool> reschedule_pending = false;
  61. std::size_t core_index;
  62. };
  63. } // namespace Core