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 runblocked_queue: Tasks waiting for I/O or other eventszombie_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:
- Tasks are stored at fixed addresses (task_id == index)
- The scheduler never removes running tasks
- Single-core execution prevents concurrent access
- 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§
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