scheduler.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <vector>
  6. #include "common/common_types.h"
  7. #include "common/thread_queue_list.h"
  8. #include "core/arm/arm_interface.h"
  9. #include "core/hle/kernel/thread.h"
  10. namespace Kernel {
  11. class Scheduler final {
  12. public:
  13. explicit Scheduler(ARM_Interface* cpu_core);
  14. ~Scheduler();
  15. /// Returns whether there are any threads that are ready to run.
  16. bool HaveReadyThreads();
  17. /// Reschedules to the next available thread (call after current thread is suspended)
  18. void Reschedule();
  19. /// Gets the current running thread
  20. Thread* GetCurrentThread() const;
  21. /// Adds a new thread to the scheduler
  22. void AddThread(SharedPtr<Thread> thread, u32 priority);
  23. /// Removes a thread from the scheduler
  24. void RemoveThread(Thread* thread);
  25. /// Schedules a thread that has become "ready"
  26. void ScheduleThread(Thread* thread, u32 priority);
  27. /// Unschedules a thread that was already scheduled
  28. void UnscheduleThread(Thread* thread, u32 priority);
  29. /// Sets the priority of a thread in the scheduler
  30. void SetThreadPriority(Thread* thread, u32 priority);
  31. /// Returns a list of all threads managed by the scheduler
  32. const std::vector<SharedPtr<Thread>>& GetThreadList() const {
  33. return thread_list;
  34. }
  35. private:
  36. /**
  37. * Pops and returns the next thread from the thread queue
  38. * @return A pointer to the next ready thread
  39. */
  40. Thread* PopNextReadyThread();
  41. /**
  42. * Switches the CPU's active thread context to that of the specified thread
  43. * @param new_thread The thread to switch to
  44. */
  45. void SwitchContext(Thread* new_thread);
  46. /// Lists all thread ids that aren't deleted/etc.
  47. std::vector<SharedPtr<Thread>> thread_list;
  48. /// Lists only ready thread ids.
  49. Common::ThreadQueueList<Thread*, THREADPRIO_LOWEST + 1> ready_queue;
  50. SharedPtr<Thread> current_thread = nullptr;
  51. ARM_Interface* cpu_core;
  52. };
  53. } // namespace Kernel