core_cpu.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <condition_variable>
  5. #include <mutex>
  6. #include "common/logging/log.h"
  7. #ifdef ARCHITECTURE_x86_64
  8. #include "core/arm/dynarmic/arm_dynarmic.h"
  9. #endif
  10. #include "core/arm/unicorn/arm_unicorn.h"
  11. #include "core/core_cpu.h"
  12. #include "core/core_timing.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/kernel/scheduler.h"
  15. #include "core/hle/kernel/thread.h"
  16. #include "core/settings.h"
  17. namespace Core {
  18. void CpuBarrier::NotifyEnd() {
  19. std::unique_lock<std::mutex> lock(mutex);
  20. end = true;
  21. condition.notify_all();
  22. }
  23. bool CpuBarrier::Rendezvous() {
  24. if (!Settings::values.use_multi_core) {
  25. // Meaningless when running in single-core mode
  26. return true;
  27. }
  28. if (!end) {
  29. std::unique_lock<std::mutex> lock(mutex);
  30. --cores_waiting;
  31. if (!cores_waiting) {
  32. cores_waiting = NUM_CPU_CORES;
  33. condition.notify_all();
  34. return true;
  35. }
  36. condition.wait(lock);
  37. return true;
  38. }
  39. return false;
  40. }
  41. Cpu::Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index)
  42. : cpu_barrier{std::move(cpu_barrier)}, core_index{core_index} {
  43. if (Settings::values.use_cpu_jit) {
  44. #ifdef ARCHITECTURE_x86_64
  45. arm_interface = std::make_shared<ARM_Dynarmic>();
  46. #else
  47. cpu_core = std::make_shared<ARM_Unicorn>();
  48. LOG_WARNING(Core, "CPU JIT requested, but Dynarmic not available");
  49. #endif
  50. } else {
  51. arm_interface = std::make_shared<ARM_Unicorn>();
  52. }
  53. scheduler = std::make_shared<Kernel::Scheduler>(arm_interface.get());
  54. }
  55. void Cpu::RunLoop(bool tight_loop) {
  56. // Wait for all other CPU cores to complete the previous slice, such that they run in lock-step
  57. if (!cpu_barrier->Rendezvous()) {
  58. // If rendezvous failed, session has been killed
  59. return;
  60. }
  61. // If we don't have a currently active thread then don't execute instructions,
  62. // instead advance to the next event and try to yield to the next thread
  63. if (Kernel::GetCurrentThread() == nullptr) {
  64. LOG_TRACE(Core, "Core-{} idling", core_index);
  65. if (IsMainCore()) {
  66. CoreTiming::Idle();
  67. CoreTiming::Advance();
  68. }
  69. PrepareReschedule();
  70. } else {
  71. if (IsMainCore()) {
  72. CoreTiming::Advance();
  73. }
  74. if (tight_loop) {
  75. arm_interface->Run();
  76. } else {
  77. arm_interface->Step();
  78. }
  79. }
  80. Reschedule();
  81. }
  82. void Cpu::SingleStep() {
  83. return RunLoop(false);
  84. }
  85. void Cpu::PrepareReschedule() {
  86. arm_interface->PrepareReschedule();
  87. reschedule_pending = true;
  88. }
  89. void Cpu::Reschedule() {
  90. if (!reschedule_pending) {
  91. return;
  92. }
  93. reschedule_pending = false;
  94. scheduler->Reschedule();
  95. }
  96. } // namespace Core