-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[BUG] Assertion fail on RP2040 with heap_4 #1187
Comments
The reason we do not do that is because "we do not perform non-deterministic operations in critical sections". Memory allocation involves walking the list of free blocks which is a non-deterministic operation as the length of the list varies. Critical sections are implemented by masking interrupts which affects device's responsiveness if interrupts are masked for extended periods. We aim to keep the critical sections short and deterministic to not affect the device's responsiveness adversely. Is this your application code or the kernel code that is attempting a memory allocation inside a critical section? If it is your application code, what problem are you trying to solve by doing memory allocation in critical section. |
I hit the issue with the LVGL library in this function. The call stack goes like this: By default the framework uses heap_3 and a critical section is needed because the RP2040 is a dual core microcontroller. When I tried to switch from heap_3 to heap_4 I found this issue about critical section. |
This actually is an issue in the LVGL - this critical section should be changed to suspend scheduler. The comment above this line says the following -
If the intention is to prevent another thread and not ISR (as the comment says), then the code should be changed to the following: static void prvCheckMutexInit(lv_mutex_t * pxMutex)
{
/* Check if the mutex needs to be initialized. */
if(pxMutex->xIsInitialized == pdFALSE) {
/* Mutex initialization must be in a critical section to prevent two threads
* from initializing it at the same time. */
vTaskSuspendAll();
/* Check again that the mutex is still uninitialized, i.e. it wasn't
* initialized while this function was waiting to enter the critical
* section. */
if(pxMutex->xIsInitialized == pdFALSE) {
prvMutexInit(pxMutex);
}
/* Exit the critical section. */
xTaskResumeAll();
}
} |
Thanks for your help so far. I'm not sure how to fix this issue. |
Calling malloc from ISR is not a good idea. The correct way to fix it would be to fix the LVGL code. There are several options:
I do not know which option would be best for LVGL - LVGL maintainers can decide about that. In the short term, you can change heap_4 to use critical section instead of scheduler suspension. As I explained before, this masks interrupts for longer periods and therefore, may adversely affect responsiveness of your application. |
Thanks a lot. I'll apply these suggestions to my application. |
When using heap_4 and
pvPortMalloc
is called from a critical section, an assertion fails.pvPortMalloc
callsvTaskSuspendAll
here which in turn asserts that it is not in a critical section here.The Pull Request #982 says
which may have caused the issue with
pvPortMalloc
.I don't see why a memory allocation can't happen in a critical section. So I reverted part of the PR and it seems to work just fine.
Target
To Reproduce
Configure a project with heap_4 and try to allocate memory in a critical section.
The text was updated successfully, but these errors were encountered: