ready_queue.rs 932 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. use crate::task::Task;
  2. use alloc::{collections::VecDeque, sync::Arc};
  3. use eonix_sync::Spin;
  4. #[eonix_percpu::define_percpu_shared]
  5. static READYQUEUE: Spin<FifoReadyQueue> = Spin::new(FifoReadyQueue::new());
  6. pub trait ReadyQueue {
  7. fn get(&mut self) -> Option<Arc<Task>>;
  8. fn put(&mut self, thread: Arc<Task>);
  9. }
  10. pub struct FifoReadyQueue {
  11. threads: VecDeque<Arc<Task>>,
  12. }
  13. impl FifoReadyQueue {
  14. pub const fn new() -> Self {
  15. FifoReadyQueue {
  16. threads: VecDeque::new(),
  17. }
  18. }
  19. }
  20. impl ReadyQueue for FifoReadyQueue {
  21. fn get(&mut self) -> Option<Arc<Task>> {
  22. self.threads.pop_front()
  23. }
  24. fn put(&mut self, thread: Arc<Task>) {
  25. self.threads.push_back(thread);
  26. }
  27. }
  28. pub fn local_rq() -> &'static Spin<dyn ReadyQueue> {
  29. &*READYQUEUE
  30. }
  31. pub fn cpu_rq(cpuid: usize) -> &'static Spin<dyn ReadyQueue> {
  32. READYQUEUE.get_for_cpu(cpuid).expect("CPU not found")
  33. }