Skip to content

Commit

Permalink
feat: update for kernel schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ludics committed Sep 16, 2023
1 parent d48904a commit 61fe623
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 6 additions & 1 deletion code/exercises/ex_9_2/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ void start_kernel(void)

os_main();

schedule();
while (1) {
// uart_puts("OS: Activate next task\n");
schedule();
// uart_puts("OS: Back to OS\n");
// uart_puts("\n");
}

uart_puts("Would not go here!\n");
while (1) {}; // stop here!
Expand Down
19 changes: 12 additions & 7 deletions code/exercises/ex_9_2/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern void switch_to(struct context *next);
*/
uint8_t __attribute__((aligned(16))) task_stack[MAX_TASKS][STACK_SIZE];
struct context ctx_tasks[MAX_TASKS];
struct context ctx_os;

uint8_t task_priorities[MAX_TASKS];

#define TASK_EMPTY 0
Expand All @@ -36,13 +38,18 @@ static void w_mscratch(reg_t x)

void sched_init()
{
w_mscratch(0);
w_mscratch((reg_t) &ctx_os);
for (int i = 0; i < MAX_TASKS; i++) {
task_status[i] = TASK_EMPTY;
task_priorities[i] = 0xff;
}
}

void back_to_os()
{
switch_to(&ctx_os);
}

/*
* implment a simple cycle FIFO schedular
*/
Expand Down Expand Up @@ -75,10 +82,8 @@ void schedule()
}
}
if (next_task_id == -1) {
panic("no task to run!\n");
return;
}
if (next_task_id == _current) {
printf("no schedulable task\n");
task_delay(10000);
return;
}
_current = next_task_id;
Expand Down Expand Up @@ -124,7 +129,7 @@ int task_create(void (*start_routin)(void* param), void* param, uint8_t priority
*/
void task_exit(void) {
task_status[_current] = TASK_EXITED;
schedule();
back_to_os();
}

/*
Expand All @@ -135,7 +140,7 @@ void task_exit(void) {
void task_yield()
{
task_status[_current] = TASK_READY;
schedule();
back_to_os();
}

/*
Expand Down

0 comments on commit 61fe623

Please sign in to comment.