Skip to content

Commit b49766a

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 59f7b4a commit b49766a

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

kernel/main.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ int32_t main(void)
3131
uint32_t entropy = read_csr(mcycle) ^ (uint32_t) _read_us();
3232
srand(entropy);
3333

34+
/* Initialize idle task */
35+
/* Initialize the first current task as idle sentinel node.
36+
* This ensures a valid entry point before any real task runs.
37+
*/
38+
idle_task_init();
39+
kcb->task_current = &kcb->task_idle;
40+
3441
/* Initialize deferred logging system.
3542
* Must be done after heap init but before app_main() to ensure
3643
* application tasks can use thread-safe printf.
@@ -47,27 +54,18 @@ int32_t main(void)
4754
printf("Scheduler mode: %s\n",
4855
kcb->preemptive ? "Preemptive" : "Cooperative");
4956

50-
/* Verify that the application created at least one task.
51-
* If 'kcb->task_current' is still NULL, it means mo_task_spawn was never
52-
* successfully called.
53-
*/
54-
if (!kcb->task_current)
55-
panic(ERR_NO_TASKS);
56-
5757
/* Save the kernel's context. This is a formality to establish a base
5858
* execution context before launching the first real task.
5959
*/
6060
setjmp(kcb->context);
6161

62-
/* Launch the first task.
63-
* 'kcb->task_current' was set by the first call to mo_task_spawn.
64-
* This function transfers control and does not return.
62+
/* Launch the first task (idle task), then scheduler will select highest
63+
* priority task. This function transfers control and does not return.
6564
*/
66-
tcb_t *first_task = kcb->task_current->data;
67-
if (!first_task)
68-
panic(ERR_NO_TASKS);
65+
tcb_t *idle = kcb->task_current->data;
66+
idle->state = TASK_RUNNING;
6967

70-
hal_dispatch_init(first_task->context);
68+
hal_dispatch_init(idle->context);
7169

7270
/* This line should be unreachable. */
7371
panic(ERR_UNKNOWN);

0 commit comments

Comments
 (0)