Better docs and structure

This commit is contained in:
2026-02-26 21:33:16 +00:00
parent d449150169
commit 13a281fa4f
18 changed files with 892 additions and 387 deletions

86
task.h
View File

@@ -1,56 +1,80 @@
/*
* task.h Cooperative multitasking interface.
*
* Provides process control blocks (PCBs), a round-robin scheduler,
* and voluntary yield/exit primitives. Context switching is performed
* by the assembly routine in context_switch.S.
*/
#ifndef TASK_H
#define TASK_H
#include <efi.h>
#include "boot_info.h"
/* Maximum number of concurrent tasks */
#define TASK_MAX 32
/* ================================================================
* Configuration
* ================================================================ */
/* Stack size per task: 32 KB (8 pages) */
#define TASK_STACK_PAGES 8
#define TASK_MAX 32 /* max concurrent tasks */
#define TASK_STACK_PAGES 8 /* 32 KB stack per task */
#define TASK_STACK_SIZE (TASK_STACK_PAGES * 4096)
#define TASK_NAME_LEN 32 /* max name length (CHAR16) */
/* Maximum task name length */
#define TASK_NAME_LEN 32
/* ================================================================
* Task states
* ================================================================ */
/* Task states */
typedef enum {
TASK_STATE_FREE = 0, /* PCB slot is unused */
TASK_STATE_READY, /* Runnable, waiting for CPU */
TASK_STATE_RUNNING, /* Currently executing on CPU */
TASK_STATE_TERMINATED /* Finished execution */
TASK_STATE_FREE = 0, /* PCB slot is unused */
TASK_STATE_READY, /* runnable, waiting to be scheduled */
TASK_STATE_RUNNING, /* currently executing on the CPU */
TASK_STATE_TERMINATED /* finished; slot will be recycled */
} TaskState;
/* Task entry function signature */
/* ================================================================
* Task entry function
* ================================================================ */
/* Signature for the function a new task begins executing. */
typedef void (*TaskEntryFn)(void *arg);
/* ================================================================
* Process Control Block (PCB)
* ================================================================ */
typedef struct Task {
UINT32 pid; /* Process ID */
TaskState state; /* Current state */
CHAR16 name[TASK_NAME_LEN]; /* Human-readable name */
UINT64 saved_rsp; /* Saved stack pointer (context switch) */
UINT64 stack_base; /* Base address of allocated stack */
UINTN stack_pages; /* Stack size in pages */
TaskEntryFn entry; /* Entry function pointer */
void *arg; /* Argument passed to entry */
UINTN switches; /* Number of times scheduled */
UINT32 pid; /* unique process ID */
TaskState state; /* current lifecycle state */
CHAR16 name[TASK_NAME_LEN]; /* human-readable label */
/* Context switch state */
UINT64 saved_rsp; /* RSP saved by context_switch() */
UINT64 stack_base; /* base phys addr of stack alloc */
UINTN stack_pages; /* stack size in pages */
/* Entry point */
TaskEntryFn entry; /* function pointer */
void *arg; /* argument passed to entry() */
/* Scheduling metadata */
UINTN switches; /* number of times scheduled */
} Task;
/* -------- Task API -------- */
/* ================================================================
* Public API
* ================================================================ */
void task_init(BootInfo *Boot);
Task *task_create(const CHAR16 *name, TaskEntryFn entry, void *arg);
void task_yield(void);
void task_exit(void);
Task *task_current(void);
UINTN task_count(void);
void task_print_list(BootInfo *Boot);
void task_init(BootInfo *Boot); /* initialise scheduler */
Task *task_create(const CHAR16 *name, /* spawn a new task */
TaskEntryFn entry, void *arg);
void task_yield(void); /* voluntarily give up the CPU */
void task_exit(void); /* terminate the current task */
Task *task_current(void); /* return the running task's PCB */
UINTN task_count(void); /* number of non-FREE tasks */
void task_print_list(BootInfo *Boot); /* print task table (for `ps`) */
/* Assembly context switch (defined in context_switch.S) */
/* Assembly context switch (defined in context_switch.S). */
extern void context_switch(UINT64 *old_rsp, UINT64 new_rsp);
#endif
#endif /* TASK_H */