readyqueue.cc 1012 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <kernel/task/readyqueue.hpp>
  2. #include <list>
  3. #include <kernel/async/lock.hpp>
  4. #include <kernel/task/thread.hpp>
  5. using namespace kernel::task;
  6. using kernel::async::mutex, kernel::async::lock_guard_irq;
  7. static mutex dispatcher_mtx;
  8. static std::list<thread*> dispatcher_thds;
  9. static thread* idle_task;
  10. void dispatcher::setup_idle(thread* _idle)
  11. {
  12. idle_task = _idle;
  13. }
  14. void dispatcher::enqueue(thread* thd)
  15. {
  16. lock_guard_irq lck(dispatcher_mtx);
  17. dispatcher_thds.push_back(thd);
  18. }
  19. void dispatcher::dequeue(thread* thd)
  20. {
  21. lock_guard_irq lck(dispatcher_mtx);
  22. dispatcher_thds.remove(thd);
  23. }
  24. thread* dispatcher::next()
  25. {
  26. lock_guard_irq lck(dispatcher_mtx);
  27. if (dispatcher_thds.empty()) {
  28. idle_task->elected_times++;
  29. return idle_task;
  30. }
  31. auto* front = dispatcher_thds.front();
  32. if (dispatcher_thds.size() != 1) {
  33. dispatcher_thds.pop_front();
  34. dispatcher_thds.push_back(front);
  35. }
  36. front->elected_times++;
  37. return front;
  38. }