core_cpu.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <condition_variable>
  6. #include <memory>
  7. #include <mutex>
  8. #include <string>
  9. #include "common/common_types.h"
  10. class ARM_Interface;
  11. namespace Kernel {
  12. class Scheduler;
  13. }
  14. namespace Core {
  15. constexpr unsigned NUM_CPU_CORES{4};
  16. class CpuBarrier {
  17. public:
  18. void Rendezvous() {
  19. std::unique_lock<std::mutex> lock(mutex);
  20. --cores_waiting;
  21. if (!cores_waiting) {
  22. cores_waiting = NUM_CPU_CORES;
  23. condition.notify_all();
  24. return;
  25. }
  26. condition.wait(lock);
  27. }
  28. private:
  29. unsigned cores_waiting{NUM_CPU_CORES};
  30. std::mutex mutex;
  31. std::condition_variable condition;
  32. };
  33. class Cpu {
  34. public:
  35. Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index);
  36. void RunLoop(bool tight_loop = true);
  37. void SingleStep();
  38. void PrepareReschedule();
  39. ARM_Interface& CPU() {
  40. return *arm_interface;
  41. }
  42. Kernel::Scheduler& Scheduler() {
  43. return *scheduler;
  44. }
  45. bool IsMainCore() const {
  46. return core_index == 0;
  47. }
  48. private:
  49. void Reschedule();
  50. std::shared_ptr<ARM_Interface> arm_interface;
  51. std::shared_ptr<CpuBarrier> cpu_barrier;
  52. std::unique_ptr<Kernel::Scheduler> scheduler;
  53. bool reschedule_pending{};
  54. size_t core_index;
  55. };
  56. } // namespace Core