Skip to content

async_context_freertos: Add support for configSUPPORT_STATIC_ALLOCATION #2436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

mbrase
Copy link

@mbrase mbrase commented Apr 22, 2025

The implementation of async_context_freertos currently assumes that FreeRTOS has been configured with configSUPPORT_DYNAMIC_ALLOCATION, which causes it to allocate semaphores, timers and tasks from the heap. However, some projects may prefer configSUPPORT_STATIC_ALLOCATION, which requires memory to be allocated ahead of time. This change allows async_context_freertos to support either static or dynamic allocation.

The way this works is when configSUPPORT_STATIC_ALLOCATION is enabled, async_context_freertos struct will reserve extra space for the static objects (e.g. StaticSemaphore_t) and it will prefer to use the static creation functions (e.g. xSemaphoreCreateBinaryStatic()). For the task creation, the user will be responsible for allocating the stack memory and passing it in through the config. If a stack is not provided, then it will fallback to using dynamic allocation for the stack (e.g. xTaskCreate()).

The implementation of async_context_freertos currently assumes that
FreeRTOS has been configured with `configSUPPORT_DYNAMIC_ALLOCATION`,
which causes it to allocate semaphores, timers and tasks from the heap.
However, some projects may prefer `configSUPPORT_STATIC_ALLOCATION`,
which requires memory to be allocated ahead of time. This change allows
async_context_freertos to support either static or dynamic allocation.

The way this works is when `configSUPPORT_STATIC_ALLOCATION` is enabled,
`async_context_freertos` struct will reserve extra space for the static
objects (e.g. `StaticSemaphore_t`) and it will prefer to use the static
creation functions (e.g. `xSemaphoreCreateBinaryStatic()`). For the task
creation, the user will be responsible for allocating the stack memory
and passing it in through the config. If a stack is not provided, then
it will fallback to using dynamic allocation for the stack (e.g.
`xTaskCreate()`).

if (!self->lock_mutex ||
!self->work_needed_sem ||
!self->timer_handle ||
pdPASS != xTaskCreate(async_context_task, "async_context_task", config->task_stack_size, self,
config->task_priority, &self->task_handle)) {
!(self->task_handle ||
Copy link
Contributor

@kilograham kilograham Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a bit wordy.

Maybe just do

     !self->timer_handle ||
#if configSUPPORT_STATIC_ALLOCATION
     !self->task_handle
#else
     pdPASS != xTaskCreate(async_context_task, "async_context_task", config->task_stack_size, self, ... )
#endif

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was trying to do here is make it so that if someone has both configSUPPORT_STATIC_ALLOCATION and configSUPPORT_DYNAMIC_ALLOCATION enabled, and they don't provide a task_stack, then it will fallback to using xTaskCreate(). I'm open to suggestions on making this cleaner.

@@ -68,9 +76,21 @@ typedef struct async_context_freertos_config {
struct async_context_freertos {
async_context_t core;
SemaphoreHandle_t lock_mutex;
#if configSUPPORT_STATIC_ALLOCATION
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for style; i think i'd put all the _bufs in a single #ifdef after the handles (I donl think there is any particular code size reason for them being close together)

@kilograham kilograham added this to the 2.1.2 milestone Apr 23, 2025
@kilograham kilograham self-assigned this Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants