Skip to content
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

Fix xTaskNotifyWait & ulTaskNotifyTake determinism. #833

Merged
merged 6 commits into from
Oct 17, 2023

Conversation

kar-rahul-aws
Copy link
Member

@kar-rahul-aws kar-rahul-aws commented Oct 13, 2023

Description

Fixes the bug described in this issue: #612. This was originally contributed in this PR: #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:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

#612

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

@kar-rahul-aws kar-rahul-aws requested a review from a team as a code owner October 13, 2023 07:16
@kar-rahul-aws kar-rahul-aws marked this pull request as draft October 13, 2023 07:16
tasks.c Outdated Show resolved Hide resolved
@aggarg aggarg marked this pull request as ready for review October 16, 2023 15:09
@sonarqubecloud
Copy link

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

@chinglee-iot chinglee-iot self-requested a review October 17, 2023 07:48
@aggarg aggarg merged commit 30283b5 into FreeRTOS:main Oct 17, 2023
15 checks passed
@aggarg aggarg deleted the pr_625_final branch October 17, 2023 09:35
n9wxu pushed a commit to n9wxu/FreeRTOS-Kernel that referenced this pull request Oct 26, 2023
This PR fixes the bug described in the following issue:
FreeRTOS#612.
This was originally contributed in the following PR:
FreeRTOS#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.

------------

Co-authored-by: Jacob Carver <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants