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.h File Reference

ICARUS Semaphore - Counting Semaphores. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Functions

bool semaphore_init (uint8_t semaphore_idx, uint32_t semaphore_count)
 Initialize a counting semaphore.
 
bool semaphore_feed (uint8_t semaphore_idx)
 Increment semaphore count (V operation / signal)
 
bool semaphore_consume (uint8_t semaphore_idx)
 Decrement semaphore count (P operation / wait)
 
bool semaphore_consume_timeout (uint8_t semaphore_idx, uint32_t max_ticks)
 Decrement semaphore count with timeout (timed P / wait).
 
uint32_t semaphore_get_count (uint8_t semaphore_idx)
 Get current semaphore count.
 
uint32_t semaphore_get_max_count (uint8_t semaphore_idx)
 Get semaphore maximum count.
 
bool sem_can_feed (uint8_t semaphore_idx)
 Check if semaphore can accept a feed (SVC call gate)
 
bool sem_can_consume (uint8_t semaphore_idx)
 Check if semaphore can be consumed (SVC call gate)
 
void sem_increment (uint8_t semaphore_idx)
 Increment semaphore count (SVC call gate)
 
void sem_decrement (uint8_t semaphore_idx)
 Decrement semaphore count (SVC call gate)
 
bool __semaphore_init (uint8_t semaphore_idx, uint32_t semaphore_count)
 Privileged implementation of semaphore_init.
 
bool __semaphore_feed (uint8_t semaphore_idx)
 Privileged implementation of semaphore_feed.
 
bool __semaphore_consume (uint8_t semaphore_idx)
 Privileged implementation of semaphore_consume.
 
bool __semaphore_consume_timeout (uint8_t semaphore_idx, uint32_t max_ticks)
 Privileged implementation of semaphore_consume_timeout.
 
uint32_t __semaphore_get_count (uint8_t semaphore_idx)
 Privileged implementation of semaphore_get_count.
 
uint32_t __semaphore_get_max_count (uint8_t semaphore_idx)
 Privileged implementation of semaphore_get_max_count.
 
bool __sem_can_feed (uint8_t semaphore_idx)
 Privileged call gate: can semaphore accept a feed?
 
bool __sem_can_consume (uint8_t semaphore_idx)
 Privileged call gate: can semaphore be consumed?
 
void __sem_increment (uint8_t semaphore_idx)
 Privileged write gate: increment semaphore count.
 
void __sem_decrement (uint8_t semaphore_idx)
 Privileged write gate: decrement semaphore count.
 

Detailed Description

ICARUS Semaphore - Counting Semaphores.

Version
0.1.0

Counting semaphore implementation for task synchronization.

Author
Souham Biswas
Date
2025

Definition in file semaphore.h.

Function Documentation

◆ __sem_can_consume()

bool __sem_can_consume ( uint8_t  semaphore_idx)

Privileged call gate: can semaphore be consumed?

Note
Returns true if engaged and count > 0 Used by __semaphore_consume spin loop.

Definition at line 186 of file semaphore.c.

References icarus_semaphore_t::count, ICARUS_MAX_SEMAPHORES, and semaphore_list.

Referenced by sem_can_consume(), and SVC_Handler_C().

◆ __sem_can_feed()

bool __sem_can_feed ( uint8_t  semaphore_idx)

Privileged call gate: can semaphore accept a feed?

Note
Returns true if engaged and count < max_count Used by __semaphore_feed spin loop to avoid direct DTCM reads from unprivileged thread mode once DTCM is priv-only.

Definition at line 172 of file semaphore.c.

References icarus_semaphore_t::count, ICARUS_MAX_SEMAPHORES, icarus_semaphore_t::max_count, and semaphore_list.

Referenced by sem_can_feed(), and SVC_Handler_C().

◆ __sem_decrement()

void __sem_decrement ( uint8_t  semaphore_idx)

Privileged write gate: decrement semaphore count.

Note
Called after sem_can_consume() spin loop exits Runs in privileged SVC handler — already atomic, no critical section needed

Definition at line 213 of file semaphore.c.

References icarus_semaphore_t::count, ICARUS_MAX_SEMAPHORES, os_tick_count, semaphore_list, and icarus_semaphore_t::tick_updated_at.

Referenced by sem_decrement(), and SVC_Handler_C().

◆ __sem_increment()

void __sem_increment ( uint8_t  semaphore_idx)

Privileged write gate: increment semaphore count.

Note
Called after sem_can_feed() spin loop exits Runs in privileged SVC handler — already atomic, no critical section needed

Definition at line 199 of file semaphore.c.

References icarus_semaphore_t::count, ICARUS_MAX_SEMAPHORES, os_tick_count, semaphore_list, and icarus_semaphore_t::tick_updated_at.

Referenced by sem_increment(), and SVC_Handler_C().

◆ __semaphore_consume()

bool __semaphore_consume ( uint8_t  semaphore_idx)

Privileged implementation of semaphore_consume.

Note
Internal function - use semaphore_consume() wrapper
Spin-wait uses sem_can_consume() SVC gate — safe with DTCM priv-only

Definition at line 81 of file semaphore.c.

References ICARUS_MAX_SEMAPHORES, sem_can_consume(), sem_decrement(), semaphore_list, and task_active_sleep().

Referenced by semaphore_consume().

◆ __semaphore_consume_timeout()

bool __semaphore_consume_timeout ( uint8_t  semaphore_idx,
uint32_t  max_ticks 
)

Privileged implementation of semaphore_consume_timeout.

Spins with task_active_sleep(1) checking sem_can_consume() each tick. Returns false if max_ticks elapse without the semaphore becoming available. max_ticks == 0 is a non-blocking try (check once, return immediately).

Parameters
semaphore_idxSemaphore index.
max_ticksMaximum ticks to wait.
Return values
trueAcquired.
falseTimeout or invalid.

Definition at line 116 of file semaphore.c.

References ICARUS_MAX_SEMAPHORES, sem_can_consume(), sem_decrement(), semaphore_list, and task_active_sleep().

Referenced by semaphore_consume_timeout(), and SVC_Handler_C().

◆ __semaphore_feed()

bool __semaphore_feed ( uint8_t  semaphore_idx)

Privileged implementation of semaphore_feed.

Note
Internal function - use semaphore_feed() wrapper
Spin-wait uses sem_can_feed() SVC gate — safe with DTCM priv-only

Definition at line 54 of file semaphore.c.

References ICARUS_MAX_SEMAPHORES, sem_can_feed(), sem_increment(), semaphore_list, and task_active_sleep().

Referenced by semaphore_feed().

◆ __semaphore_get_count()

uint32_t __semaphore_get_count ( uint8_t  semaphore_idx)

Privileged implementation of semaphore_get_count.

Note
Internal function - use semaphore_get_count() wrapper

Definition at line 146 of file semaphore.c.

References icarus_semaphore_t::count, ICARUS_MAX_SEMAPHORES, and semaphore_list.

Referenced by semaphore_get_count(), and SVC_Handler_C().

◆ __semaphore_get_max_count()

uint32_t __semaphore_get_max_count ( uint8_t  semaphore_idx)

Privileged implementation of semaphore_get_max_count.

Note
Internal function - use semaphore_get_max_count() wrapper

Definition at line 158 of file semaphore.c.

References ICARUS_MAX_SEMAPHORES, icarus_semaphore_t::max_count, and semaphore_list.

Referenced by semaphore_get_max_count(), and SVC_Handler_C().

◆ __semaphore_init()

bool __semaphore_init ( uint8_t  semaphore_idx,
uint32_t  semaphore_count 
)

Privileged implementation of semaphore_init.

Note
Internal function - use semaphore_init() wrapper
Runs in SVC handler — already atomic, no critical section needed

Definition at line 35 of file semaphore.c.

References icarus_semaphore_t::count, icarus_semaphore_t::engaged, ICARUS_MAX_SEMAPHORES, icarus_semaphore_t::max_count, os_tick_count, semaphore_list, and icarus_semaphore_t::tick_updated_at.

Referenced by semaphore_init(), and SVC_Handler_C().

◆ sem_can_consume()

bool sem_can_consume ( uint8_t  semaphore_idx)

Check if semaphore can be consumed (SVC call gate)

Parameters
semaphore_idxSemaphore index
Returns
true if count > 0 and engaged
Note
Uses SVC to read semaphore_list from DTCM in privileged mode

Check if semaphore can be consumed (SVC call gate)

Note
Runs in privileged mode — safe once DTCM is priv-only

Definition at line 967 of file svc.c.

References __sem_can_consume(), and SVC_SEM_CAN_CONSUME.

Referenced by __semaphore_consume(), and __semaphore_consume_timeout().

◆ sem_can_feed()

bool sem_can_feed ( uint8_t  semaphore_idx)

Check if semaphore can accept a feed (SVC call gate)

Parameters
semaphore_idxSemaphore index
Returns
true if count < max_count and engaged
Note
Uses SVC to read semaphore_list from DTCM in privileged mode

Check if semaphore can accept a feed (SVC call gate)

Note
Runs in privileged mode — safe once DTCM is priv-only

Definition at line 946 of file svc.c.

References __sem_can_feed(), and SVC_SEM_CAN_FEED.

Referenced by __semaphore_feed().

◆ sem_decrement()

void sem_decrement ( uint8_t  semaphore_idx)

Decrement semaphore count (SVC call gate)

Parameters
semaphore_idxSemaphore index
Note
Uses SVC to write semaphore_list in DTCM from privileged mode
Called after sem_can_consume() spin loop exits

Decrement semaphore count (SVC call gate)

Note
Called after sem_can_consume() spin loop exits

Definition at line 1056 of file svc.c.

References __sem_decrement(), and SVC_SEM_DECREMENT.

Referenced by __semaphore_consume(), and __semaphore_consume_timeout().

◆ sem_increment()

void sem_increment ( uint8_t  semaphore_idx)

Increment semaphore count (SVC call gate)

Parameters
semaphore_idxSemaphore index
Note
Uses SVC to write semaphore_list in DTCM from privileged mode
Called after sem_can_feed() spin loop exits

Increment semaphore count (SVC call gate)

Note
Called after sem_can_feed() spin loop exits

Definition at line 1038 of file svc.c.

References __sem_increment(), and SVC_SEM_INCREMENT.

Referenced by __semaphore_feed().

◆ semaphore_consume()

bool semaphore_consume ( uint8_t  semaphore_idx)

Decrement semaphore count (P operation / wait)

Parameters
semaphore_idxSemaphore index
Returns
true on success
Note
Blocks if count == 0
Placed in ITCM for zero wait-state execution

Decrement semaphore count (P operation / wait)

Definition at line 807 of file svc.c.

References __semaphore_consume().

◆ semaphore_consume_timeout()

bool semaphore_consume_timeout ( uint8_t  semaphore_idx,
uint32_t  max_ticks 
)

Decrement semaphore count with timeout (timed P / wait).

Parameters
semaphore_idxSemaphore index.
max_ticksMaximum ticks to wait. 0 = non-blocking try.
Return values
trueSemaphore was acquired within the timeout.
falseTimeout expired without acquiring, or invalid index.

Decrement semaphore count with timeout (timed P / wait).

Definition at line 814 of file svc.c.

References __semaphore_consume_timeout().

◆ semaphore_feed()

bool semaphore_feed ( uint8_t  semaphore_idx)

Increment semaphore count (V operation / signal)

Parameters
semaphore_idxSemaphore index
Returns
true on success
Note
Blocks if count == max_count
Placed in ITCM for zero wait-state execution

Increment semaphore count (V operation / signal)

Definition at line 800 of file svc.c.

References __semaphore_feed().

◆ semaphore_get_count()

uint32_t semaphore_get_count ( uint8_t  semaphore_idx)

Get current semaphore count.

Parameters
semaphore_idxSemaphore index
Returns
Current count, or 0 if invalid

Definition at line 821 of file svc.c.

References __semaphore_get_count(), and SVC_SEMAPHORE_GET_COUNT.

◆ semaphore_get_max_count()

uint32_t semaphore_get_max_count ( uint8_t  semaphore_idx)

Get semaphore maximum count.

Parameters
semaphore_idxSemaphore index
Returns
Maximum count, or 0 if invalid

Definition at line 841 of file svc.c.

References __semaphore_get_max_count(), and SVC_SEMAPHORE_GET_MAX_COUNT.

◆ semaphore_init()

bool semaphore_init ( uint8_t  semaphore_idx,
uint32_t  semaphore_count 
)

Initialize a counting semaphore.

Parameters
semaphore_idxSemaphore index (0 to ICARUS_MAX_SEMAPHORES-1)
semaphore_countInitial and maximum count
Returns
true on success, false on error

Definition at line 778 of file svc.c.

References __semaphore_init(), and SVC_SEMAPHORE_INIT.