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 4 commits 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()).

@kilograham kilograham added this to the 2.1.2 milestone Apr 23, 2025
@kilograham kilograham self-assigned this Apr 23, 2025
@mbrase mbrase force-pushed the freertos-static-task branch 2 times, most recently from 968d67b to 636f390 Compare April 29, 2025 19:40
@mbrase
Copy link
Author

mbrase commented Apr 29, 2025

I pushed a new change that does not try to fall back to xTaskCreate() if no stack is provided. I also modified the cyw43_arch_init_default_async_context() function to statically allocate a stack when configSUPPORT_STATIC_ALLOCATION is enabled.

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 setting the task_stack field in `async_context_freertos_config_t`.
For convenience, The `cyw43_arch_init_default_async_context()` function
will reserve `CYW43_TASK_STACK_SIZE` words of stack space in static
memory.
@peterharperuk
Copy link
Contributor

peterharperuk commented May 30, 2025

LwIP NO_SYS=0 requires configSUPPORT_DYNAMIC_ALLOCATION so that's one use case for having both (it works fine) although I'm not sure why you'd bother?

peterharperuk and others added 3 commits May 30, 2025 12:49
In theory you can use your own async context therefore bypassing
cyw43_arch_init_default_async_context, which would leave
cyw43_async_context_freertos_task_stack unused.

Add a #define for this rare situation.
@mbrase
Copy link
Author

mbrase commented Jun 2, 2025

LwIP NO_SYS=0 requires configSUPPORT_DYNAMIC_ALLOCATION so that's one use case for having both (it works fine) although I'm not sure why you'd bother?

I'm curious, is there any reason that LwIP NO_SYS=0 mode can't use static allocation? (other than needing to update all the calls in src/rp2_common/pico_lwip/lwip_freertos.c to use the static function call variants)

@peterharperuk
Copy link
Contributor

I'm curious, is there any reason that LwIP NO_SYS=0 mode can't use static allocation?

There's this assert in LwIP. I didn't look any deeper.

#if !configSUPPORT_DYNAMIC_ALLOCATION
# error "lwIP FreeRTOS port requires configSUPPORT_DYNAMIC_ALLOCATION"
#endif

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.

3 participants