2025-01-30 10:25 AM
I have come across an issue which I believe is a bug in the implementation of the osThreadFlagsWait function.
Scenario
A thread is waiting on a flag (e.g. 0x2u) for a timeout above 0, before this timeout should occur, there are multiple other flags set on the thread (e.g. 0x1u) using osThreadFlagsSet, this results in the osThreadFlagsWait function returning with a value of osErrorTimeout sooner than the set timeout.
Cause
The xTaskNotifyWait function which is called by osThreadFlagsWait returns each time the 0x1u flag is set, resulting in this section of code being run:
Note that t0 is set to the return value of xTaskGetTickCount() at the start of the wait loop, so is the start time, tout is a timeout value passed to xTaskNotifyWait, which is originally the set timeout, but is adjusted each time the xTaskNotifyWait function returns.
The issue with the code above is that the value of td is the total time that the thread has been waiting, this is subtracting from tout each time a flag is set that is not waited on e.g.
for a set timeout of 100, if the flag that is not waited on happens every 10 ticks, the value of td and tout will be as follows for each time xTaskNotifyWait returns:
This gives a timeout of 40 ticks in this case instead of the desired 100.
Suggested Fix
To fix this issue, I would suggest removing the cumulative subtraction from tout, instead replacing it with a subtraction of td from the set timeout e.g.