Skip to content

Commit e7da7fe

Browse files
committed
Refactor launch sequence in main() for scheduler initialization
This change sets up the scheduler state during system startup by assigning kcb->task_current to kcb->harts->task_idle and dispatching to the idle task as the first execution context. This commit also keeps the scheduling entry path consistent between startup and runtime.
1 parent 70044ad commit e7da7fe

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

kernel/main.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,29 @@ int32_t main(void)
2323
printf("Heap initialized, %u bytes available\n",
2424
(unsigned int) (size_t) &_heap_size);
2525

26-
/* Initialize idle task */
26+
/* Initialize the first current task as idle sentinel node.
27+
* This ensures a valid entry point before any real task runs.
28+
*/
2729
idle_task_init();
30+
kcb->task_current = kcb->harts->task_idle;
2831

2932
/* Call the application's main entry point to create initial tasks. */
3033
kcb->preemptive = (bool) app_main();
3134
printf("Scheduler mode: %s\n",
3235
kcb->preemptive ? "Preemptive" : "Cooperative");
3336

34-
/* Verify that the application created at least one task.
35-
* If 'kcb->task_current' is still NULL, it means mo_task_spawn was never
36-
* successfully called.
37-
*/
38-
if (!kcb->task_current)
39-
panic(ERR_NO_TASKS);
40-
4137
/* Save the kernel's context. This is a formality to establish a base
4238
* execution context before launching the first real task.
4339
*/
4440
setjmp(kcb->context);
4541

46-
/* Launch the first task.
47-
* 'kcb->task_current' was set by the first call to mo_task_spawn.
48-
* This function transfers control and does not return.
42+
/* Launch the first task (idle task), then scheduler will select highest
43+
* priority task. This function transfers control and does not return.
4944
*/
50-
tcb_t *first_task = kcb->task_current->data;
51-
if (!first_task)
52-
panic(ERR_NO_TASKS);
45+
tcb_t *idle = kcb->task_current->data;
46+
idle->state = TASK_RUNNING;
5347

54-
hal_dispatch_init(first_task->context);
48+
hal_dispatch_init(idle->context);
5549

5650
/* This line should be unreachable. */
5751
panic(ERR_UNKNOWN);

0 commit comments

Comments
 (0)