# Module os::task

source · [−]
Expand description

Task management implementation

Everything about task management, like starting and switching tasks is implemented here.

A single global instance of `TaskManager` called `TASK_MANAGER` controls all the tasks in the whole operating system.

A single global instance of `Processor` called `PROCESSOR` monitors running task(s) for each core.

A single global instance of `PidAllocator` called `PID_ALLOCATOR` allocates pid for user apps.

Be careful when you see `__switch` ASM function in `switch.S`. Control flow around this function might not be what you expect.

## Modules

context 🔒

Implementation of `TaskContext`

manager 🔒

Implementation of `TaskManager`

pid 🔒

Implementation of `PidAllocator`

processor 🔒

Implementation of `Processor` and Intersection of control flow

switch 🔒

Wrap `switch.S` as a function

task 🔒

Implementation of `TaskControlBlock`

## Structs

INITPROC

Globle process that init user shell

KernelStack

Kernelstack for app

PidAllocator

Pid Allocator struct

PidHandle

Bind pid lifetime to `PidHandle`

Processor

Processor management structure

TaskContext

task context structure containing some registers

TaskManager

A array of `TaskControlBlock` that is thread-safe

## Functions

add_initproc

Add init process to the manager

add_task

Interface offered to add task

current_task

Get running task

current_trap_cx

Get the mutable reference to trap context of current task

current_user_token

Get token of the address space of current task

exit_current_and_run_next

Exit the current ‘Running’ task and run the next task in task list.

fetch_task

Interface offered to pop the first task

pid_alloc

Allocate a pid from PID_ALLOCATOR

run_tasks

The main part of process execution and scheduling Loop `fetch_task` to get the process that needs to run, and switch the process through `__switch`

schedule

Return to idle control flow for new scheduling

suspend_current_and_run_next

Suspend the current ‘Running’ task and run the next task in task list.

take_current_task

Take the current task,leaving a None in its place