scheduler.h 2.1 KB

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