global_scheduler_context.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/k_thread.h"
  13. #include "core/hle/kernel/svc_types.h"
  14. namespace Kernel {
  15. class KernelCore;
  16. class SchedulerLock;
  17. using KSchedulerPriorityQueue =
  18. KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, Svc::LowestThreadPriority,
  19. Svc::HighestThreadPriority>;
  20. static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
  21. static_assert(Svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);
  22. static_assert(Svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);
  23. class GlobalSchedulerContext final {
  24. friend class KScheduler;
  25. public:
  26. using LockType = KAbstractSchedulerLock<KScheduler>;
  27. explicit GlobalSchedulerContext(KernelCore& kernel);
  28. ~GlobalSchedulerContext();
  29. /// Adds a new thread to the scheduler
  30. void AddThread(std::shared_ptr<KThread> thread);
  31. /// Removes a thread from the scheduler
  32. void RemoveThread(std::shared_ptr<KThread> thread);
  33. /// Returns a list of all threads managed by the scheduler
  34. [[nodiscard]] const std::vector<std::shared_ptr<KThread>>& GetThreadList() const {
  35. return thread_list;
  36. }
  37. /**
  38. * Rotates the scheduling queues of threads at a preemption priority and then does
  39. * some core rebalancing. Preemption priorities can be found in the array
  40. * 'preemption_priorities'.
  41. *
  42. * @note This operation happens every 10ms.
  43. */
  44. void PreemptThreads();
  45. /// Returns true if the global scheduler lock is acquired
  46. bool IsLocked() const;
  47. [[nodiscard]] LockType& SchedulerLock() {
  48. return scheduler_lock;
  49. }
  50. [[nodiscard]] const LockType& SchedulerLock() const {
  51. return scheduler_lock;
  52. }
  53. private:
  54. friend class KScopedSchedulerLock;
  55. friend class KScopedSchedulerLockAndSleep;
  56. KernelCore& kernel;
  57. std::atomic_bool scheduler_update_needed{};
  58. KSchedulerPriorityQueue priority_queue;
  59. LockType scheduler_lock;
  60. /// Lists all thread ids that aren't deleted/etc.
  61. std::vector<std::shared_ptr<KThread>> thread_list;
  62. Common::SpinLock global_list_guard{};
  63. };
  64. } // namespace Kernel