Module scheduler

Module scheduler 

Source
Expand description

Scheduler module

The scheduler module is responsible for scheduling tasks on the CPU. Currently, the scheduler is a simple round-robin scheduler with separate queues for different task states to improve efficiency:

  • ready_queue: Tasks that are ready to run
  • blocked_queue: Tasks waiting for I/O or other events
  • zombie_queue: Finished tasks waiting to be cleaned up

This separation avoids unnecessary iteration over blocked/zombie tasks during normal scheduling operations.

§TaskPool Safety

The global TaskPool stores tasks in a fixed-size array indexed by task_id. This design avoids HashMap-related issues and provides stable memory locations:

  • Fixed Array: tasks[task_id] ensures stable addresses (no reallocation)
  • Direct Indexing: task_id == index for O(1) access without hash lookup
  • ID Recycling: Free list reuses task IDs to avoid exhaustion

The pool provides get_task() and get_task_mut() which return &'static references using raw pointers. This is unsafe but practical because:

  1. Tasks are stored at fixed addresses (task_id == index)
  2. The scheduler never removes running tasks
  3. Single-core execution prevents concurrent access
  4. Context switches never invalidate the current task’s reference

IMPORTANT: Never access TaskPool::tasks directly. Always use the provided methods which document and enforce safety invariants.

Structs§

Scheduler
TaskPool
Global task pool storing all tasks in a Box-ed fixed-size array

Constants§

MAX_TASKS 🔒
Task pool that stores tasks in fixed positions With each Task being 824 bytes, 1024 tasks consume approximately 824 KiB of memory, which is very reasonable for general-purpose systems. TODO: Refactor Task struct to use fine-grained Mutex on individual fields (e.g., state: Mutex, time_slice: Mutex) and change TaskPool to use Arc for safe sharing across threads/contexts. This would also eliminate the fixed-size limitation.

Statics§

SCHEDULER 🔒
TASK_POOL 🔒
Global task pool storing all tasks Using spin::Once with Box-ed tasks array to avoid large stack usage.

Functions§

get_scheduler
get_task_pool
Get the global task pool (lazy initialization on first call)
make_test_tasks