global_scheduler_context.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <set>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "core/hardware_properties.h"
  9. #include "core/hle/kernel/k_priority_queue.h"
  10. #include "core/hle/kernel/k_scheduler_lock.h"
  11. #include "core/hle/kernel/k_thread.h"
  12. #include "core/hle/kernel/svc_types.h"
  13. namespace Kernel {
  14. class KernelCore;
  15. class SchedulerLock;
  16. using KSchedulerPriorityQueue =
  17. KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, Svc::LowestThreadPriority,
  18. Svc::HighestThreadPriority>;
  19. static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
  20. static_assert(Svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);
  21. static_assert(Svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);
  22. class GlobalSchedulerContext final {
  23. friend class KScheduler;
  24. public:
  25. using LockType = KAbstractSchedulerLock<KScheduler>;
  26. explicit GlobalSchedulerContext(KernelCore& kernel_);
  27. ~GlobalSchedulerContext();
  28. /// Adds a new thread to the scheduler
  29. void AddThread(KThread* thread);
  30. /// Removes a thread from the scheduler
  31. void RemoveThread(KThread* thread);
  32. /// Returns a list of all threads managed by the scheduler
  33. [[nodiscard]] const std::vector<KThread*>& GetThreadList() const {
  34. return thread_list;
  35. }
  36. /**
  37. * Rotates the scheduling queues of threads at a preemption priority and then does
  38. * some core rebalancing. Preemption priorities can be found in the array
  39. * 'preemption_priorities'.
  40. *
  41. * @note This operation happens every 10ms.
  42. */
  43. void PreemptThreads();
  44. /// Returns true if the global scheduler lock is acquired
  45. bool IsLocked() const;
  46. void UnregisterDummyThreadForWakeup(KThread* thread);
  47. void RegisterDummyThreadForWakeup(KThread* thread);
  48. void WakeupWaitingDummyThreads();
  49. [[nodiscard]] LockType& SchedulerLock() {
  50. return scheduler_lock;
  51. }
  52. [[nodiscard]] const LockType& SchedulerLock() const {
  53. return scheduler_lock;
  54. }
  55. private:
  56. friend class KScopedSchedulerLock;
  57. friend class KScopedSchedulerLockAndSleep;
  58. KernelCore& kernel;
  59. std::atomic_bool scheduler_update_needed{};
  60. KSchedulerPriorityQueue priority_queue;
  61. LockType scheduler_lock;
  62. /// Lists dummy threads pending wakeup on lock release
  63. std::set<KThread*> woken_dummy_threads;
  64. /// Lists all thread ids that aren't deleted/etc.
  65. std::vector<KThread*> thread_list;
  66. std::mutex global_list_guard;
  67. };
  68. } // namespace Kernel