scheduler.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/hle/kernel/object.h"
  10. #include "core/hle/kernel/thread.h"
  11. namespace Core {
  12. class ARM_Interface;
  13. }
  14. namespace Kernel {
  15. class Process;
  16. class Scheduler final {
  17. public:
  18. explicit Scheduler(Core::ARM_Interface& cpu_core);
  19. ~Scheduler();
  20. /// Returns whether there are any threads that are ready to run.
  21. bool HaveReadyThreads() const;
  22. /// Reschedules to the next available thread (call after current thread is suspended)
  23. void Reschedule();
  24. /// Gets the current running thread
  25. Thread* GetCurrentThread() const;
  26. /// Gets the timestamp for the last context switch in ticks.
  27. u64 GetLastContextSwitchTicks() const;
  28. /// Adds a new thread to the scheduler
  29. void AddThread(SharedPtr<Thread> thread, u32 priority);
  30. /// Removes a thread from the scheduler
  31. void RemoveThread(Thread* thread);
  32. /// Schedules a thread that has become "ready"
  33. void ScheduleThread(Thread* thread, u32 priority);
  34. /// Unschedules a thread that was already scheduled
  35. void UnscheduleThread(Thread* thread, u32 priority);
  36. /// Sets the priority of a thread in the scheduler
  37. void SetThreadPriority(Thread* thread, u32 priority);
  38. /// Returns a list of all threads managed by the scheduler
  39. const std::vector<SharedPtr<Thread>>& GetThreadList() const {
  40. return thread_list;
  41. }
  42. private:
  43. /**
  44. * Pops and returns the next thread from the thread queue
  45. * @return A pointer to the next ready thread
  46. */
  47. Thread* PopNextReadyThread();
  48. /**
  49. * Switches the CPU's active thread context to that of the specified thread
  50. * @param new_thread The thread to switch to
  51. */
  52. void SwitchContext(Thread* new_thread);
  53. /**
  54. * Called on every context switch to update the internal timestamp
  55. * This also updates the running time ticks for the given thread and
  56. * process using the following difference:
  57. *
  58. * ticks += most_recent_ticks - last_context_switch_ticks
  59. *
  60. * The internal tick timestamp for the scheduler is simply the
  61. * most recent tick count retrieved. No special arithmetic is
  62. * applied to it.
  63. */
  64. void UpdateLastContextSwitchTime(Thread* thread, Process* process);
  65. /// Lists all thread ids that aren't deleted/etc.
  66. std::vector<SharedPtr<Thread>> thread_list;
  67. /// Lists only ready thread ids.
  68. Common::ThreadQueueList<Thread*, THREADPRIO_LOWEST + 1> ready_queue;
  69. SharedPtr<Thread> current_thread = nullptr;
  70. Core::ARM_Interface& cpu_core;
  71. u64 last_context_switch_time = 0;
  72. static std::mutex scheduler_mutex;
  73. };
  74. } // namespace Kernel