core_cpu.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <memory>
  8. #include <mutex>
  9. #include <string>
  10. #include "common/common_types.h"
  11. #include "core/arm/exclusive_monitor.h"
  12. class ARM_Interface;
  13. namespace Kernel {
  14. class Scheduler;
  15. }
  16. namespace Core {
  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, size_t core_index);
  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. const std::shared_ptr<Kernel::Scheduler>& Scheduler() const {
  45. return scheduler;
  46. }
  47. bool IsMainCore() const {
  48. return core_index == 0;
  49. }
  50. size_t CoreIndex() const {
  51. return core_index;
  52. }
  53. static std::shared_ptr<ExclusiveMonitor> MakeExclusiveMonitor(size_t num_cores);
  54. private:
  55. void Reschedule();
  56. std::shared_ptr<ARM_Interface> arm_interface;
  57. std::shared_ptr<CpuBarrier> cpu_barrier;
  58. std::shared_ptr<Kernel::Scheduler> scheduler;
  59. bool reschedule_pending{};
  60. size_t core_index;
  61. };
  62. } // namespace Core