Skip to content

Commit 279364e

Browse files
committed
Add sched_t to kcb for O(1) scheduler support
Previously, the scheduler performed a linear search through the global task list (kcb->tasks) to find the next TASK_READY task. This approach limited scalability as the search iterations increased with the number of tasks, resulting in higher scheduling latency. To support an O(1) scheduler and improve extensibility, a sched_t structure is introduced and integrated into kcb. The new structure contains: - ready_queues: Holds all runnable tasks, including TASK_RUNNING and TASK_READY. The scheduler selects tasks directly from these queues. - ready_bitmap: Records the state of each ready queue. Using the bitmap, the scheduler can locate the highest-priority runnable task in O(1) time complexity. - rr_cursors: Round-robin cursors that track the next task node in each ready queue. Each priority level maintains its own RR cursor. The top priority cursor is assigned to kcb->task_current, which is advanced circularly after each scheduling cycle. - hart_id: Identifies the scheduler instance per hart (0 for single-hart configurations). - task_idle: The system idle task, executed when no runnable tasks exist. In the current design, kcb binds only one sched_t instance (hart0) for single-hart systems, but this structure can be extended for multi-hart scheduling in the future.
1 parent fdd7587 commit 279364e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/sys/task.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ typedef struct tcb {
8484
void *rt_prio; /* Opaque pointer for custom real-time scheduler hook */
8585
} tcb_t;
8686

87+
/* Scheduler attribution */
88+
typedef struct sched {
89+
uint8_t ready_bitmap; /* 8-bit priority bitmap */
90+
list_t
91+
*ready_queues[TASK_PRIORITY_LEVELS]; /* Separate queue per priority */
92+
uint16_t queue_counts[TASK_PRIORITY_LEVELS]; /* O(1) size tracking */
93+
94+
/* Weighted Round-Robin State per Priority Level */
95+
list_node_t *rr_cursors[TASK_PRIORITY_LEVELS]; /* Round-robin position */
96+
97+
/* Hart-Specific Data */
98+
uint8_t hart_id; /* RISC-V hart identifier */
99+
100+
} sched_t;
101+
87102
/* Kernel Control Block (KCB)
88103
*
89104
* Singleton structure holding global kernel state, including task lists,
@@ -104,6 +119,9 @@ typedef struct {
104119
/* Timer Management */
105120
list_t *timer_list; /* List of active software timers */
106121
volatile uint32_t ticks; /* Global system tick, incremented by timer */
122+
123+
/* per-hart scheduler management */
124+
sched_t *harts;
107125
} kcb_t;
108126

109127
/* Global pointer to the singleton Kernel Control Block */

kernel/task.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
static int32_t noop_rtsched(void);
1616
void _timer_tick_handler(void);
1717

18+
/* Hart scheduler */
19+
static sched_t hart0 = {
20+
.ready_bitmap = 0,
21+
.ready_queues = {NULL},
22+
.rr_cursors = {NULL},
23+
.hart_id = 0,
24+
};
25+
1826
/* Kernel-wide control block (KCB) */
1927
static kcb_t kernel_state = {
2028
.tasks = NULL,
@@ -25,6 +33,7 @@ static kcb_t kernel_state = {
2533
.task_count = 0,
2634
.ticks = 0,
2735
.preemptive = true, /* Default to preemptive mode */
36+
.harts = &hart0, /* Initial hart */
2837
};
2938
kcb_t *kcb = &kernel_state;
3039

0 commit comments

Comments
 (0)