ICARUS OS (Intelligent Cooperative Architecture for Real-time Unified Systems) 0.1.0
Preemptive Real-Time Operating System for ARM Cortex-M7
Loading...
Searching...
No Matches
task.c
Go to the documentation of this file.
1
16#include "icarus/task.h"
17#include "icarus/kernel.h"
18#include "icarus/scheduler.h"
19
20#include <string.h>
21#include <stdio.h>
22
23/* ============================================================================
24 * TASK CREATION
25 * ========================================================================= */
26
27ITCM_FUNC void os_create_task(icarus_task_t *task, void (*function)(void),
28 uint32_t *stack, uint32_t stack_size,
29 uint32_t *data, const char *name)
30{
31 if (running_task_count >= (uint8_t)ICARUS_MAX_TASKS) {
32 return;
33 }
34
35 task->function = function;
36 task->stack_base = stack;
37 task->stack_size = stack_size;
40 task->global_tick_paused = 0;
41 task->ticks_to_pause = 0;
42
43 (void)strncpy(task->name, name, (size_t)ICARUS_MAX_TASK_NAME_LEN);
44 task->name[ICARUS_MAX_TASK_NAME_LEN - 1] = '\0';
45
46 uint32_t *stack_top = &stack[stack_size - 1u];
47
48 *(stack_top--) = 0x01000000; /* xPSR */
49 *(stack_top--) = (uint32_t)(uintptr_t)function; /* PC */
50 *(stack_top--) = (uint32_t)(uintptr_t)os_exit_task; /* LR */
51 *(stack_top--) = 0; /* R12 */
52 *(stack_top--) = 0; /* R3 */
53 *(stack_top--) = 0; /* R2 */
54 *(stack_top--) = 0; /* R1 */
55 *(stack_top) = 0; /* R0 */
56
57 task->stack_pointer = stack_top;
58 task->data_pointer = data;
59 task->dispatch_count = 0;
60 task->stack_watermark = stack_size; /* full stack = max free */
61
62 /* Fill stack with sentinel pattern for watermark detection */
63 for (uint32_t i = 0u; i < (stack_size - 16u); i++) {
64 stack[i] = 0xDEADC0DEu;
65 }
66
68}
69
74ITCM_FUNC void __os_register_task(void (*function)(void), const char *name)
75{
78 (uint32_t)ICARUS_STACK_WORDS,
80}
81
82/* ============================================================================
83 * TASK LIFECYCLE
84 * ========================================================================= */
85
91{
93
94 if (running_task_count > 0u) {
96 }
97
98 /* Add to cleanup queue */
101 }
102
103 __os_yield();
104}
105
110ITCM_FUNC void __os_kill_process(uint8_t task_index)
111{
112 if ((task_index >= num_created_tasks) || (task_index == 0u)) {
113 return; /* Cannot kill task 0 (idle task) or invalid indices */
114 }
115
117
118 if (running_task_count > 0u) {
120 }
121
122 /* Add to cleanup queue */
124 cleanup_task_idx[++current_cleanup_task_idx] = (int8_t)task_index;
125 }
126
127 if (task_index == current_task_index) {
128 __os_yield();
129 }
130}
131
137{
138 (void)printf("[INFO] %s committed suicide\r\n",
141}
142
154ITCM_FUNC void __os_restart_task(uint8_t task_index)
155{
156 if ((task_index >= num_created_tasks) || (task_index == 0u)) {
157 return;
158 }
159
160 icarus_task_t *t = task_list[task_index];
161
162 if ((t->task_state != TASK_STATE_KILLED) &&
164 return;
165 }
166
167 /* Re-fill stack with sentinel for watermark tracking */
168 for (uint32_t i = 0u; i < (t->stack_size - 16u); i++) {
169 t->stack_base[i] = 0xDEADC0DEu;
170 }
171
172 /* Re-initialize the exception frame — same as os_create_task */
173 uint32_t *stack_top = &t->stack_base[t->stack_size - 1u];
174
175 *(stack_top--) = 0x01000000; /* xPSR */
176 *(stack_top--) = (uint32_t)(uintptr_t)t->function; /* PC */
177 *(stack_top--) = (uint32_t)(uintptr_t)os_exit_task; /* LR */
178 *(stack_top--) = 0; /* R12 */
179 *(stack_top--) = 0; /* R3 */
180 *(stack_top--) = 0; /* R2 */
181 *(stack_top--) = 0; /* R1 */
182 *(stack_top) = 0; /* R0 */
183
184 t->stack_pointer = stack_top;
186 t->global_tick_paused = 0;
187 t->ticks_to_pause = 0;
188 t->dispatch_count = 0;
190
192}
#define ICARUS_MAX_TASK_NAME_LEN
Maximum length of task name string (including null terminator)
Definition config.h:102
#define ICARUS_MAX_TASKS
Maximum number of concurrent tasks.
Definition config.h:54
#define ICARUS_STACK_WORDS
Stack size per task in 32-bit words.
Definition config.h:84
#define ITCM_FUNC
Definition config.h:155
ICARUS Kernel Core - State and Initialization.
uint8_t num_created_tasks
Total number of created tasks.
Definition kernel.c:65
uint8_t running_task_count
Count of tasks in active states.
Definition kernel.c:64
uint8_t current_task_index
Index of currently executing task.
Definition kernel.c:63
icarus_task_t * task_list[128]
Array of pointers to all task control blocks.
Definition kernel.c:50
int8_t current_cleanup_task_idx
Current cleanup queue write index.
Definition kernel.c:73
uint32_t * __kernel_get_data(uint8_t task_idx)
Privileged implementation of kernel_get_data.
Definition kernel.c:282
int8_t cleanup_task_idx[128]
Cleanup task index queue.
Definition kernel.c:57
uint32_t * __kernel_get_stack(uint8_t task_idx)
Privileged implementation of kernel_get_stack.
Definition kernel.c:269
ICARUS Scheduler - Task Scheduling and Timing.
void __os_yield(void)
Privileged implementation of os_yield.
Definition scheduler.c:99
Task Control Block (TCB)
Definition types.h:115
icarus_task_priority_t task_priority
Task priority level.
Definition types.h:130
uint32_t global_tick_paused
Tick count when blocked (offset 20)
Definition types.h:124
char name[32]
Human-readable task name.
Definition types.h:131
icarus_task_state_t task_state
Current task state (offset 16)
Definition types.h:123
uint32_t dispatch_count
Times this task has been dispatched.
Definition types.h:132
uint32_t * stack_base
Base address of task stack.
Definition types.h:118
uint32_t stack_size
Stack size in 32-bit words.
Definition types.h:119
uint32_t * data_pointer
Current data pointer (offset 28)
Definition types.h:127
uint32_t * stack_pointer
Current stack pointer (offset 12)
Definition types.h:122
uint32_t stack_watermark
Minimum free stack words observed.
Definition types.h:133
void(* function)(void)
Task entry point function pointer.
Definition types.h:117
uint32_t ticks_to_pause
Ticks to remain blocked (offset 24)
Definition types.h:125
void os_create_task(icarus_task_t *task, void(*function)(void), uint32_t *stack, uint32_t stack_size, uint32_t *data, const char *name)
Create a task with explicit stack allocation.
Definition task.c:27
void __os_kill_process(uint8_t task_index)
Privileged implementation of os_kill_process.
Definition task.c:110
void __os_exit_task(void)
Privileged implementation of os_exit_task.
Definition task.c:90
void __os_restart_task(uint8_t task_index)
Privileged implementation of os_restart_task.
Definition task.c:154
void __os_register_task(void(*function)(void), const char *name)
Privileged implementation of os_register_task.
Definition task.c:74
void __os_task_suicide(void)
Privileged implementation of os_task_suicide.
Definition task.c:136
ICARUS Task Management - Creation and Lifecycle.
void os_exit_task(void)
Terminate current task normally.
Definition svc.c:636
@ TASK_STATE_FINISHED
Task completed execution normally.
Definition types.h:81
@ TASK_STATE_KILLED
Task terminated by another task.
Definition types.h:80
@ TASK_STATE_COLD
Task created but never started.
Definition types.h:76
@ TASK_PRIORITY_LOW
Background/idle priority.
Definition types.h:91