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
semaphore.c
Go to the documentation of this file.
1
16#include "icarus/semaphore.h"
17#include "icarus/kernel.h"
18#include "icarus/scheduler.h"
19
20/* ============================================================================
21 * SECTION PLACEMENT MACROS
22 * ========================================================================= */
23
24/* Defined centrally in icarus/config.h — included via icarus/kernel.h */
25
26/* ============================================================================
27 * SEMAPHORE IMPLEMENTATION
28 * ========================================================================= */
29
35ITCM_FUNC bool __semaphore_init(uint8_t semaphore_idx, uint32_t semaphore_count) {
36 if ((semaphore_idx < (uint8_t)ICARUS_MAX_SEMAPHORES) && (semaphore_count > 0u)) {
37 if (!semaphore_list[semaphore_idx]->engaged) {
38 semaphore_list[semaphore_idx]->count = semaphore_count;
39 semaphore_list[semaphore_idx]->max_count = semaphore_count;
41 semaphore_list[semaphore_idx]->engaged = true;
42 return true;
43 }
44 }
45
46 return false;
47}
48
54ITCM_FUNC bool __semaphore_feed(uint8_t semaphore_idx) {
55#ifdef HOST_TEST
56 /* In host tests, check validity directly (no MPU) */
57 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
58 (!semaphore_list[semaphore_idx]->engaged)) {
59 return false;
60 }
61#else
62 /* On target, index check only — gate checks engaged via SVC */
63 if (semaphore_idx >= ICARUS_MAX_SEMAPHORES) {
64 return false;
65 }
66#endif
67
68 while (!sem_can_feed(semaphore_idx)) {
69 (void)task_active_sleep(1);
70 }
71
72 sem_increment(semaphore_idx);
73 return true;
74}
75
81ITCM_FUNC bool __semaphore_consume(uint8_t semaphore_idx) {
82#ifdef HOST_TEST
83 /* In host tests, check validity directly (no MPU) */
84 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
85 (!semaphore_list[semaphore_idx]->engaged)) {
86 return false;
87 }
88#else
89 /* On target, index check only — gate checks engaged via SVC */
90 if (semaphore_idx >= ICARUS_MAX_SEMAPHORES) {
91 return false;
92 }
93#endif
94
95 while (!sem_can_consume(semaphore_idx)) {
96 (void)task_active_sleep(1);
97 }
98
99 sem_decrement(semaphore_idx);
100 return true;
101}
102
116ITCM_FUNC bool __semaphore_consume_timeout(uint8_t semaphore_idx,
117 uint32_t max_ticks) {
118#ifdef HOST_TEST
119 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
120 (!semaphore_list[semaphore_idx]->engaged)) {
121 return false;
122 }
123#else
124 if (semaphore_idx >= ICARUS_MAX_SEMAPHORES) {
125 return false;
126 }
127#endif
128
129 uint32_t waited = 0;
130 while (!sem_can_consume(semaphore_idx)) {
131 if (waited >= max_ticks) {
132 return false;
133 }
134 (void)task_active_sleep(1);
135 waited++;
136 }
137
138 sem_decrement(semaphore_idx);
139 return true;
140}
141
146ITCM_FUNC uint32_t __semaphore_get_count(uint8_t semaphore_idx) {
147 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
148 (!semaphore_list[semaphore_idx]->engaged)) {
149 return 0;
150 }
151 return semaphore_list[semaphore_idx]->count;
152}
153
158ITCM_FUNC uint32_t __semaphore_get_max_count(uint8_t semaphore_idx) {
159 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
160 (!semaphore_list[semaphore_idx]->engaged)) {
161 return 0;
162 }
163 return semaphore_list[semaphore_idx]->max_count;
164}
165
172ITCM_FUNC bool __sem_can_feed(uint8_t semaphore_idx) {
173 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
174 (!semaphore_list[semaphore_idx]->engaged)) {
175 return false;
176 }
177 return semaphore_list[semaphore_idx]->count <
178 semaphore_list[semaphore_idx]->max_count;
179}
180
186ITCM_FUNC bool __sem_can_consume(uint8_t semaphore_idx) {
187 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
188 (!semaphore_list[semaphore_idx]->engaged)) {
189 return false;
190 }
191 return semaphore_list[semaphore_idx]->count > 0u;
192}
193
199ITCM_FUNC void __sem_increment(uint8_t semaphore_idx) {
200 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
201 (!semaphore_list[semaphore_idx]->engaged)) {
202 return;
203 }
204 ++semaphore_list[semaphore_idx]->count;
206}
207
213ITCM_FUNC void __sem_decrement(uint8_t semaphore_idx) {
214 if ((semaphore_idx >= (uint8_t)ICARUS_MAX_SEMAPHORES) ||
215 (!semaphore_list[semaphore_idx]->engaged)) {
216 return;
217 }
218 --semaphore_list[semaphore_idx]->count;
220}
#define ICARUS_MAX_SEMAPHORES
Maximum number of semaphores.
Definition config.h:61
#define ITCM_FUNC
Definition config.h:155
ICARUS Kernel Core - State and Initialization.
icarus_semaphore_t * semaphore_list[64]
Array of pointers to all semaphores.
Definition kernel.c:51
volatile uint32_t os_tick_count
System tick counter.
Definition kernel.c:69
uint32_t task_active_sleep(uint32_t ticks)
Sleep for specified ticks (cooperative, via SVC)
Definition svc.c:522
ICARUS Scheduler - Task Scheduling and Timing.
uint32_t __semaphore_get_count(uint8_t semaphore_idx)
Privileged implementation of semaphore_get_count.
Definition semaphore.c:146
bool __semaphore_consume_timeout(uint8_t semaphore_idx, uint32_t max_ticks)
Privileged implementation of semaphore_consume_timeout.
Definition semaphore.c:116
uint32_t __semaphore_get_max_count(uint8_t semaphore_idx)
Privileged implementation of semaphore_get_max_count.
Definition semaphore.c:158
void __sem_decrement(uint8_t semaphore_idx)
Privileged write gate: decrement semaphore count.
Definition semaphore.c:213
bool __semaphore_feed(uint8_t semaphore_idx)
Privileged implementation of semaphore_feed.
Definition semaphore.c:54
bool __sem_can_consume(uint8_t semaphore_idx)
Privileged call gate: can semaphore be consumed?
Definition semaphore.c:186
bool __semaphore_init(uint8_t semaphore_idx, uint32_t semaphore_count)
Privileged implementation of semaphore_init.
Definition semaphore.c:35
bool __sem_can_feed(uint8_t semaphore_idx)
Privileged call gate: can semaphore accept a feed?
Definition semaphore.c:172
void __sem_increment(uint8_t semaphore_idx)
Privileged write gate: increment semaphore count.
Definition semaphore.c:199
bool __semaphore_consume(uint8_t semaphore_idx)
Privileged implementation of semaphore_consume.
Definition semaphore.c:81
ICARUS Semaphore - Counting Semaphores.
bool sem_can_feed(uint8_t semaphore_idx)
Check if semaphore can accept a feed (SVC call gate)
Definition svc.c:946
void sem_decrement(uint8_t semaphore_idx)
Decrement semaphore count (SVC call gate)
Definition svc.c:1056
bool sem_can_consume(uint8_t semaphore_idx)
Check if semaphore can be consumed (SVC call gate)
Definition svc.c:967
void sem_increment(uint8_t semaphore_idx)
Increment semaphore count (SVC call gate)
Definition svc.c:1038
uint32_t tick_updated_at
Last modification timestamp (for debugging)
Definition types.h:155
uint32_t max_count
Maximum count (initial value)
Definition types.h:154
bool engaged
true if semaphore is initialized and active
Definition types.h:152
uint32_t count
Current semaphore count.
Definition types.h:153