Repository navigation
fix(freertos): notifiy-based sync can be initialized from any task - #6692
Conversation
kisvegabor
left a comment
There was a problem hiding this comment.
Thank you Liam!
I wonder if other operating systems don't have the same issue.
Anyway, I've looked into LVGL and couldn't see an obvious way to restructure it to make it work even without this fix. So I suggest merging this fix now and see if the issue shows up for others too.
|
Thanks for picking it up. If there is a performance hit, then I believe it is better to simply enforce the users to use the same thread with proper documentation. This restriction is easy to address by the users, maybe you can propose to the users to use a similar setup as I did and documented in the original issue. If it works now without performance hit then this is of course perfect!! |
|
@liamHowatt thanks for this PR. |
| /* Send a notification to the task waiting. */ | ||
| xTaskNotifyGive(pxCond->xTaskToNotify); | ||
| _enter_critical(); | ||
| TaskHandle_t task_to_notify = pxCond->xTaskToNotify; |
There was a problem hiding this comment.
Use the new member BaseType_t xIsInitialized; to verify if the task to notify is valid.
| typedef struct { | ||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| TaskHandle_t xTaskToNotify; | ||
| BaseType_t xSignalSent; |
There was a problem hiding this comment.
Please use the freertos naming convention all over in this file:
https://www.freertos.org/Documentation/02-Kernel/06-Coding-guidelines/02-FreeRTOS-Coding-Standard-and-Style-Guide#naming-conventions
At a better look, I think you can use a member like
BaseType_t xIsInitialized;
similar as it is done on semaphore implementation.
E.G. no signal can be done until the initialization is done.
| #if USE_FREERTOS_TASK_NOTIFY | ||
| /* Store the handle of the calling task. */ | ||
| pxCond->xTaskToNotify = xTaskGetCurrentTaskHandle(); | ||
| pxCond->xTaskToNotify = NULL; |
There was a problem hiding this comment.
Please don't remove the scope of the init API.
Add a local function here like:
/* If the cond is uninitialized, perform initialization. */
prvCheckCondInit(pxCond);
and let the initialization happen if it get called explicitly.
Then make use of the same static function prvCheckCondInit(pxCond); in the lv_thread_sync_wait().
|
Thanks for the review! |
| typedef struct { | ||
| void (*pvStartRoutine)(void *); /**< Application thread function. */ | ||
| void * xTaskArg; /**< Arguments for application thread function. */ | ||
| void * pTaskArg; /**< Arguments for application thread function. */ |
There was a problem hiding this comment.
In the coding standard naming conventions you linked, the "v" seems to only apply for function names that return void (nothing), not void pointer variables.
| xIsInitialized; /**< Set to pdTRUE if this condition variable is initialized, pdFALSE otherwise. */ | ||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| TaskHandle_t xTaskToNotify; | ||
| BaseType_t xSignalSent; |
There was a problem hiding this comment.
No need for xSignalSent, so it can be removed, correct? We make use of the pxCond->xSyncSignal now.
There was a problem hiding this comment.
Not currently, but I can make that variable common between both implementations 👍
| static void prvCondInit(lv_thread_sync_t * pxCond) | ||
| { | ||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| pxCond->xTaskToNotify = NULL; |
There was a problem hiding this comment.
Implement this, similar as it is done for semaphores.
I see it like:
#if USE_FREERTOS_TASK_NOTIFY
pxCond->xTaskToNotify = xTaskGetCurrentTaskHandle();
pxCond->xIsInitialized = pdTRUE;
pxCond->xSyncSignal = pdFALSE;
#else
You might have the last two common for semaphores and sygnal thread.
There was a problem hiding this comment.
I mean to replace all the code under #if USE_FREERTOS_TASK_NOTIFY with the above suggestion and see if it works to you.
| prvCheckCondInit(pxCond); | ||
|
|
||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| _enter_critical(); |
There was a problem hiding this comment.
Replace all new code with:
#if USE_FREERTOS_TASK_NOTIFY
xTaskNotifyGive(pxCond->xTaskToNotify);
pxCond->xSyncSignal = pdTRUE;
#else
There was a problem hiding this comment.
I mean to replace all the code under #if USE_FREERTOS_TASK_NOTIFY with the above suggestion and see if it works to you.
There was a problem hiding this comment.
Replace all new code with:
#if USE_FREERTOS_TASK_NOTIFY xTaskNotifyGive(pxCond->xTaskToNotify); pxCond->xSyncSignal = pdTRUE; #else
pxCond->xTaskToNotify may be NULL which is why there is the current logic to determine which task to notify, if any.
| lv_result_t lvRes = LV_RESULT_OK; | ||
|
|
||
| /* If the cond is uninitialized, perform initialization. */ | ||
| prvCheckCondInit(pxCond); |
There was a problem hiding this comment.
If wait gets called prior of any init, this call will do the initialization.
There was a problem hiding this comment.
Correct. I applied your feedback after your last review to make it work that way. Is there something you want me to change?
|
|
||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| LV_UNUSED(pxCond); | ||
| TaskHandle_t current_task_handle = xTaskGetCurrentTaskHandle(); |
There was a problem hiding this comment.
Replace all new code with:
#if USE_FREERTOS_TASK_NOTIFY
while(!pxCond->xSyncSignal) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}
pxCond->xSyncSignal = pdFALSE;
#else
There was a problem hiding this comment.
I'm not completely clear on this. Is this a different solution?
There was a problem hiding this comment.
I mean to replace all the code under #if USE_FREERTOS_TASK_NOTIFY with the above suggestion and see if it works to you.
| prvCheckCondInit(pxCond); | ||
|
|
||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| _enter_critical(); |
There was a problem hiding this comment.
Similar as done for sync_signal.
Gotcha, I will have time to try tomorrow. |
|
@liamHowatt @nicusorcitu |
|
Yes, it is currently working. |
|
Thanks, merging then to make sure that it's fixed in v9.2. |
liamHowatt
left a comment
There was a problem hiding this comment.
I think you have a simpler way to do it than what I've done. Would you mind opening a PR? 🙂
| prvCheckCondInit(pxCond); | ||
|
|
||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| _enter_critical(); |
There was a problem hiding this comment.
Replace all new code with:
#if USE_FREERTOS_TASK_NOTIFY xTaskNotifyGive(pxCond->xTaskToNotify); pxCond->xSyncSignal = pdTRUE; #else
pxCond->xTaskToNotify may be NULL which is why there is the current logic to determine which task to notify, if any.
|
|
| prvCheckCondInit(pxCond); | ||
|
|
||
| #if USE_FREERTOS_TASK_NOTIFY | ||
| _enter_critical(); |
There was a problem hiding this comment.
_enter_critical() does not work for ISR, it will hang.
You would have to add new defines:
#define _enter_critical_isr() taskENTER_CRITICAL_FROM_ISR();
There was a problem hiding this comment.
Very good catch! 😮 In all of the environments I've seen so far, the same critical section API is used inside and outside of ISRs. I tested on ESP32-S3 and it worked for me. Maybe only some ports of FreeRTOS implement the ISR version with important extra steps.
We have a strict way of pushing commits to upstream, need to pass some additional checks. For this particular issue we might send a commit with the proposal, will open a PR soon. |
|
I have done a lot of tests and investigated the new code which does not seem to work anymore on FreeRTOS.
But why? Task notification is different than semaphore implementation. I would revert the changes due to this commit and revise it from the application level. It had more sense as it was before. |
|
Even after using the Please see the issue this PR is linked to #6414. It's non-obvious to users that the task that calls It seems like the task notify vs semaphore choice is not very configurable. It's a define in a header file, not part of the config. I think the task notify implementation has the potential to deprecate the semaphore implementation at some point, but only if it doesn't have too many tradeoffs. If both implementations stay, I think it should be easier for a user to choose which implementation they want. |
|
I'm looking at the code you're talking about. I think I don't think |
|
Exactly that is the case, the sync goes first then wait will return immediately. So It will create a loop in dispatcher. All the user need to do is to keep in mind that the sync point means that the thread handle
Please also see the example in FreeRTOS: https://www.freertos.org/Documentation/02-Kernel/04-API-references/05-Direct-to-task-notifications/01-xTaskNotifyGive I will prepare a patch and will push it to github on PR #6793 |
It would block at the next call, though. No? lv_result_t lv_thread_sync_wait(lv_thread_sync_t * pxCond)
{
lv_result_t lvRes = LV_RESULT_OK;
/* If the cond is uninitialized, perform initialization. */
prvCheckCondInit(pxCond);
#if USE_FREERTOS_TASK_NOTIFY
TaskHandle_t current_task_handle = xTaskGetCurrentTaskHandle();
_enter_critical();
BaseType_t signal_sent = pxCond->xSyncSignal;
pxCond->xSyncSignal = pdFALSE;
if(signal_sent == pdFALSE) {
/* The signal hasn't been sent yet. Tell the sender to notify this task */
pxCond->xTaskToNotify = current_task_handle;
}
/* If we have a signal from the other task, we should not ask to be notified */
_exit_critical();
if(signal_sent == pdFALSE) {
/* Wait for other task to notify this task. */
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}
/* If the signal was received, no wait needs to be done */
#else
/* ... */
#endif
return lvRes;
} |
|
I will loop because the signal will always come first in this order: signal, wait, signal, wait, signal, wait.... |
|
So it's not spinning in an infinite loop in one task. It's switching between tasks. What's wrong with signal, wait, signal, wait, signal, wait? |
|
Both |
|
Ok I see. Hmm. |
|
Please see the PR #6793 |
|
It looks like draw drivers are supposed to provide a How did the old implementation work? How does a high priority task trigger a context switch to a lower priority task by the high priority task sending a signal to itself? |
|
Units does not mandatory need to have In any case, please don't try to change the scope of the task notify. It is what it is, a task notification who can be used in one way. That is what the comment says from the beginning on the implementation: Line 44 in 559618d It is by design to do so. I gave you an FreeRTOS example to clearly show how it should work on synchronization between two tasks. |
| } | ||
| /* If we have a signal from the other task, we should not ask to be notified */ | ||
| _exit_critical(); | ||
|
|
There was a problem hiding this comment.
After exiting from critical section the pxCond->xSyncSignal might get changed, so this is not thread safe.
| } | ||
| /* If a task is already waiting, there is no need to set the sync signal */ | ||
| _exit_critical(); | ||
|
|
There was a problem hiding this comment.
Same here, you exit from critical section but still rely on the task_to_notify. Not thread safe.
|
@cosmindanielradu19 @cristian-stoica
|
|
@nicusorcitu Please open a PR to see the proposed changes. @liamHowatt, you were more involved into this issue, please take a look on @nicusorcitu's comment. |
|
I do not have a fix yet, neither time to look at it right now. But I will try a patch till 20Dec. I will let you know both about it. |
|
I would like to understand what's wrong with the fix I did. You raised some concerns in the PR during review. Can you reiterate the ones that you still believe are potential causes of instability? I truly believe the parts you flagged as thread-unsafe are thread-safe. Can we begin reviewing those changes, starting with a specific bug? |
|
@liamHowatt I'm looking at a situation when running lv_demo_benchmark with both VGLITE and PXP enabled (SW draw unit = 0). Tests run as expected until rotated_argb_image_cb when the screen turns black and app stops. Debugging shows the code is still running in a dispatch-execute loop without any progress at all. I'm not sure what is actually going on. What I've also tried is disabling TASK_NOTIFY and the application runs. I can reproduce the issue under MCUX when running from flash (LVGL 9.2.1). |
|
The issue we see works just fine with semaphore implementation. It hangs only if we use
I will try to do some debugs but can't have right now, I will let you know when I have something. |



Description of the feature or fix
Fixes #6414
lv_thread_sync_waitcan be called from a thread other than the one that calledlv_thread_sync_init.@nicusorcitu
Notes
lv_conf_template.hrun lv_conf_internal_gen.py and update Kconfig.scripts/code-format.py(astyle version v3.4.12 needs to be installed) and follow the Code Conventions.