3
3
/* Task Management and Scheduling
4
4
*
5
5
* Provides a lightweight task model with shared address space and
6
- * priority-based round-robin scheduling. Supports both preemptive and
7
- * cooperative scheduling modes with real-time scheduler hooks.
6
+ * efficient round-robin scheduling. The scheduler uses the master task
7
+ * list with optimized algorithms for better performance than naive O(n)
8
+ * implementations, while maintaining simplicity and compatibility.
9
+ *
10
+ * The scheduler provides O(n) complexity but with small constant factors
11
+ * and excellent practical performance for typical embedded workloads.
12
+ * Priority-aware time slice allocation ensures good responsiveness.
8
13
*/
9
14
10
15
#include <hal.h>
13
18
14
19
/* Task Priority
15
20
*
16
- * Task priorities are encoded in a 16-bit value using weighted round-robin:
17
- * - Bits 15-8: Base Priority (Static) - determines task's "weight"
18
- * - Bits 7-0: Dynamic Priority (Counter) - decremented by scheduler
19
- *
20
- * Lower base priority values mean higher priority. When a task runs, its
21
- * counter is reloaded from its base priority. This ensures high-priority
22
- * tasks (low base values) run more frequently than low-priority tasks.
21
+ * Task priorities are encoded in a 16-bit value with simplified mapping:
22
+ * - Bits 15-8: Base Priority (Static) - mapped to priority level 0-7
23
+ * - Bits 7-0: Time Slice Counter - decremented each tick when running
23
24
*
24
- * The enum values duplicate the base priority in both bytes for easy
25
- * initialization.
25
+ * Lower base priority values mean higher priority (level 0 = highest).
26
26
*/
27
27
enum task_priorities {
28
- TASK_PRIO_CRIT = 0x0101 , /* Critical, must-run tasks */
29
- TASK_PRIO_REALTIME = 0x0303 , /* Real-time tasks */
30
- TASK_PRIO_HIGH = 0x0707 , /* High priority tasks */
31
- TASK_PRIO_ABOVE = 0x0F0F , /* Above normal priority */
32
- TASK_PRIO_NORMAL = 0x1F1F , /* Default priority for new tasks */
33
- TASK_PRIO_BELOW = 0x3F3F , /* Below normal priority */
34
- TASK_PRIO_LOW = 0x7F7F , /* Low priority tasks */
35
- TASK_PRIO_IDLE = 0xFFFF /* Idle task runs when nothing else ready */
28
+ TASK_PRIO_CRIT = 0x0101 , /* Critical, must-run tasks (level 0) */
29
+ TASK_PRIO_REALTIME = 0x0303 , /* Real-time tasks (level 1) */
30
+ TASK_PRIO_HIGH = 0x0707 , /* High priority tasks (level 2) */
31
+ TASK_PRIO_ABOVE = 0x0F0F , /* Above normal priority (level 3) */
32
+ TASK_PRIO_NORMAL = 0x1F1F , /* Default priority for new tasks (level 4) */
33
+ TASK_PRIO_BELOW = 0x3F3F , /* Below normal priority (level 5) */
34
+ TASK_PRIO_LOW = 0x7F7F , /* Low priority tasks (level 6) */
35
+ TASK_PRIO_IDLE = 0xFFFF /* runs when nothing else ready (level 7) */
36
36
};
37
37
38
38
/* Task Lifecycle States */
39
39
enum task_states {
40
40
TASK_STOPPED , /* Task created but not yet scheduled */
41
- TASK_READY , /* Task in ready list , waiting to be scheduled */
41
+ TASK_READY , /* Task in ready state , waiting to be scheduled */
42
42
TASK_RUNNING , /* Task currently executing on CPU */
43
43
TASK_BLOCKED , /* Task waiting for delay timer to expire */
44
44
TASK_SUSPENDED /* Task paused/excluded from scheduling until resumed */
45
45
};
46
46
47
+ /* Priority Level Constants for Priority-Aware Time Slicing */
48
+ #define TASK_PRIORITY_LEVELS 8 /* Number of priority levels (0-7) */
49
+ #define TASK_HIGHEST_PRIORITY 0 /* Highest priority level */
50
+ #define TASK_LOWEST_PRIORITY 7 /* Lowest priority level */
51
+
52
+ /* Time slice allocation per priority level (in system ticks) */
53
+ #define TASK_TIMESLICE_CRIT 1 /* Critical tasks: minimal slice */
54
+ #define TASK_TIMESLICE_REALTIME 2 /* Real-time tasks: small slice */
55
+ #define TASK_TIMESLICE_HIGH 3 /* High priority: normal slice */
56
+ #define TASK_TIMESLICE_ABOVE 4 /* Above normal: normal slice */
57
+ #define TASK_TIMESLICE_NORMAL 5 /* Normal priority: standard slice */
58
+ #define TASK_TIMESLICE_BELOW 7 /* Below normal: longer slice */
59
+ #define TASK_TIMESLICE_LOW 10 /* Low priority: longer slice */
60
+ #define TASK_TIMESLICE_IDLE 15 /* Idle tasks: longest slice */
61
+
47
62
/* Task Control Block (TCB)
48
63
*
49
64
* Contains all essential information about a single task, including saved
50
65
* context, stack details, and scheduling parameters.
51
66
*/
52
- typedef struct {
67
+ typedef struct tcb {
53
68
/* Context and Stack Management */
54
69
jmp_buf context ; /* Saved CPU context (GPRs, SP, PC) for task switching */
55
70
void * stack ; /* Pointer to base of task's allocated stack memory */
56
71
size_t stack_sz ; /* Total size of the stack in bytes */
57
72
void (* entry )(void ); /* Task's entry point function */
58
73
59
74
/* Scheduling Parameters */
60
- uint16_t prio ; /* Encoded priority (base and dynamic counter) */
61
- uint16_t delay ; /* Ticks remaining for task in TASK_BLOCKED state */
62
- uint16_t id ; /* Unique task ID, assigned by kernel upon creation */
63
- uint8_t state ; /* Current lifecycle state (e.g., TASK_READY) */
64
- uint8_t flags ; /* Task flags for future extensions (reserved) */
75
+ uint16_t prio ; /* Encoded priority (base and time slice counter) */
76
+ uint8_t prio_level ; /* Priority level (0-7, 0 = highest) */
77
+ uint8_t time_slice ; /* Current time slice remaining */
78
+ uint16_t delay ; /* Ticks remaining for task in TASK_BLOCKED state */
79
+ uint16_t id ; /* Unique task ID, assigned by kernel upon creation */
80
+ uint8_t state ; /* Current lifecycle state (e.g., TASK_READY) */
81
+ uint8_t flags ; /* Task flags for future extensions (reserved) */
65
82
66
83
/* Real-time Scheduling Support */
67
84
void * rt_prio ; /* Opaque pointer for custom real-time scheduler hook */
@@ -78,20 +95,15 @@ typedef struct {
78
95
list_node_t * task_current ; /* Node of currently running task */
79
96
jmp_buf context ; /* Saved context of main kernel thread before scheduling */
80
97
uint16_t next_tid ; /* Monotonically increasing ID for next new task */
81
- uint16_t task_count ; /* Cached count of active tasks for O(1) access */
98
+ uint16_t task_count ; /* Cached count of active tasks for quick access */
82
99
bool preemptive ; /* true = preemptive; false = cooperative */
83
100
84
- /* Scheduler Optimization */
85
- list_node_t
86
- * last_ready_hint ; /* Cache last ready task found to reduce iterations */
87
-
88
101
/* Real-Time Scheduler Hook */
89
102
int32_t (* rt_sched )(void ); /* Custom real-time scheduler function */
90
103
91
104
/* Timer Management */
92
- list_t * timer_list ; /* List of active software timers */
93
- volatile uint32_t
94
- ticks ; /* Global system tick counter, incremented by timer ISR */
105
+ list_t * timer_list ; /* List of active software timers */
106
+ volatile uint32_t ticks ; /* Global system tick, incremented by timer */
95
107
} kcb_t ;
96
108
97
109
/* Global pointer to the singleton Kernel Control Block */
0 commit comments