global_scheduler_context.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "common/spin_lock.h"
  9. #include "core/hardware_properties.h"
  10. #include "core/hle/kernel/k_priority_queue.h"
  11. #include "core/hle/kernel/k_scheduler_lock.h"
  12. #include "core/hle/kernel/thread.h"
  13. namespace Kernel {
  14. class KernelCore;
  15. class SchedulerLock;
  16. using KSchedulerPriorityQueue =
  17. KPriorityQueue<Thread, Core::Hardware::NUM_CPU_CORES, THREADPRIO_LOWEST, THREADPRIO_HIGHEST>;
  18. static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
  19. class GlobalSchedulerContext final {
  20. friend class KScheduler;
  21. public:
  22. using LockType = KAbstractSchedulerLock<KScheduler>;
  23. explicit GlobalSchedulerContext(KernelCore& kernel);
  24. ~GlobalSchedulerContext();
  25. /// Adds a new thread to the scheduler
  26. void AddThread(std::shared_ptr<Thread> thread);
  27. /// Removes a thread from the scheduler
  28. void RemoveThread(std::shared_ptr<Thread> thread);
  29. /// Returns a list of all threads managed by the scheduler
  30. const std::vector<std::shared_ptr<Thread>>& GetThreadList() const {
  31. return thread_list;
  32. }
  33. /**
  34. * Rotates the scheduling queues of threads at a preemption priority and then does
  35. * some core rebalancing. Preemption priorities can be found in the array
  36. * 'preemption_priorities'.
  37. *
  38. * @note This operation happens every 10ms.
  39. */
  40. void PreemptThreads();
  41. /// Returns true if the global scheduler lock is acquired
  42. bool IsLocked() const;
  43. LockType& SchedulerLock() {
  44. return scheduler_lock;
  45. }
  46. const LockType& SchedulerLock() const {
  47. return scheduler_lock;
  48. }
  49. private:
  50. friend class KScopedSchedulerLock;
  51. friend class KScopedSchedulerLockAndSleep;
  52. KernelCore& kernel;
  53. std::atomic_bool scheduler_update_needed{};
  54. KSchedulerPriorityQueue priority_queue;
  55. LockType scheduler_lock;
  56. /// Lists all thread ids that aren't deleted/etc.
  57. std::vector<std::shared_ptr<Thread>> thread_list;
  58. Common::SpinLock global_list_guard{};
  59. };
  60. } // namespace Kernel