FreeRTOS / FreeRTOS-Kernel

FreeRTOS kernel files only, submoduled into https://github.com/FreeRTOS/FreeRTOS and various other repos.
https://www.FreeRTOS.org
MIT License
2.76k stars 1.12k forks source link

Fix xTaskNotifyWait & ulTaskNotifyTake determinism. #833

Closed kar-rahul-aws closed 1 year ago

kar-rahul-aws commented 1 year ago

Description

Fixes the bug described in this issue: https://github.com/FreeRTOS/FreeRTOS-Kernel/issues/612. This was originally contributed in this PR: https://github.com/FreeRTOS/FreeRTOS-Kernel/pull/625.

The implementation suspends the scheduler before exiting the critical section (i.e. before enabling interrupts). If we do not do so, a notification sent from an ISR, which happens after exiting the critical section and before suspending the scheduler, will get lost. The sequence of events will be:

  1. Exit critical section.
  2. Interrupt - ISR calls xTaskNotifyFromISR which adds the task to the Ready list.
  3. Suspend scheduler.
  4. prvAddCurrentTaskToDelayedList moves the task to the delayed or suspended list.
  5. Resume scheduler does not touch the task (because it is not on the pendingReady list), effectively losing the notification from the ISR.

The same does not happen when we suspend the scheduler before exiting the critical section. The sequence of events in this case will be:

  1. Suspend scheduler.
  2. Exit critical section.
  3. Interrupt - ISR calls xTaskNotifyFromISR which adds the task to the pendingReady list as the scheduler is suspended.
  4. prvAddCurrentTaskToDelayedList adds the task to delayed or suspended list. Note that this operation does not nullify the add to pendingReady list done in the above step because a different list item, namely xEventListItem, is used for adding the task to the pendingReady list. In other words, the task still remains on the pendingReady list.
  5. Resume scheduler moves the task from pendingReady list to the Ready list.

Test Steps

Tested by adding a dummy loop in the implementation to cause a button press interrupt at the desired point and ensured that the notification is not lost.

Checklist:

Related Issue

https://github.com/FreeRTOS/FreeRTOS-Kernel/issues/612

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication