freertos-addons icon indicating copy to clipboard operation
freertos-addons copied to clipboard

Spurious wakeups on semaphores

Open dns13 opened this issue 1 year ago • 2 comments

In your ReadWriteLock you should check the return values of your xSemaphoreTake() calls to be prone against spurious wakeups. For example if you call vTaskAbortDelay() to wake up a sleeping task from another, it will also cancel your waits on the semaphore. Unfortunately that behaviour is not really documented, but I've ran into it - took me a while.

I do it like so

while (xSemaphoreTake(sem, portMAX_DELAY) != pdTRUE) {}

dns13 avatar Aug 07 '23 09:08 dns13

Seems to be fixed with FreeRTOS v11.0.0:

  • Prevent tasks waiting for a notification from being resumed by calls to vTaskResume or vTaskResumeFromISR. We thank @Moral-Hao for their contribution.

dns13 avatar Dec 18 '23 13:12 dns13

Hi, @dns13 , I don't think the change I made could fix the problem you described. Before my change, a task waiting for a notification with portMAX_DELAY may be identified as suspended, and this task could be woke up by vTaskResume. After my change, this task could not be woke up by vTaskResume. A task waiting for something(semaphore/event/notify) with portMAX_DELAY still could be woke up by xTaskAbortDelay.

Moral-Hao avatar Dec 19 '23 03:12 Moral-Hao