Skip to content

Passed a mutex waiter's priority change on to the mutex owner - #809

Open
howjmay wants to merge 2 commits into
eclipse-threadx:masterfrom
howjmay:fix/mutex-inherit-waiter-priority-change
Open

howjmay wants to merge 2 commits into
eclipse-threadx:masterfrom
howjmay:fix/mutex-inherit-waiter-priority-change

Conversation

@howjmay

@howjmay howjmay commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

A mutex owner inherits a waiter's priority only at the moment the waiter blocks in tx_mutex_get. _tx_thread_priority_change on a thread that is not ready updated that thread's own priority fields and nothing else, so a thread waiting on a TX_INHERIT mutex could be raised above the owner without the owner following, and a waiter the owner had inherited from could be lowered while the owner kept the old priority.

When the thread is suspended on a mutex, _tx_thread_priority_change now calls a new internal function, _tx_mutex_inherit_priority_update, which recomputes the mutex's highest waiting priority, the owner's inherited priority across every mutex it holds, and the owner's priority. The call goes through a new function pointer, _tx_thread_mutex_priority_update, set by tx_mutex_create in the same way as _tx_thread_mutex_release, so the thread services still do not link the mutex services. Applied to common and common_smp.

threadx_mutex_inherit_waiter_priority_change_test changes the priorities of two waiters in four steps that raise the owner, lower it to the other waiter, and finally return it to its own priority. Added to both suites. Without the fix it fails with ERROR #30 in both; with it, the mutex tests pass in both suites' default_build_coverage configuration, and the changed sources build cleanly under -Wall -Wextra -Werror with TX_NOT_INTERRUPTABLE, TX_MISRA_ENABLE, TX_DISABLE_PREEMPTION_THRESHOLD and event trace.

PR checklist

  • Updated function header with a short description and version number
  • Added test case for bug fix or new feature
  • Validated on real hardware

A mutex owner inherits a waiter's priority only at the moment the waiter blocks in
tx_mutex_get. _tx_thread_priority_change on a thread that is not ready updated that
thread's own priority fields and nothing else, so a thread waiting on a TX_INHERIT mutex
could be raised above the owner without the owner following, and a waiter the owner had
inherited from could be lowered while the owner kept the old priority.

When the thread is suspended on a mutex, _tx_thread_priority_change now calls a new
internal function, _tx_mutex_inherit_priority_update, which recomputes the mutex's
highest waiting priority, the owner's inherited priority across every mutex it holds,
and the owner's priority. The call goes through a new function pointer,
_tx_thread_mutex_priority_update, set by tx_mutex_create in the same way as
_tx_thread_mutex_release, so the thread services still do not link the mutex services.
Applied to common and common_smp.

threadx_mutex_inherit_waiter_priority_change_test changes the priorities of two waiters
in four steps that raise the owner, lower it to the other waiter, and finally return it
to its own priority. Added to both suites. Without the fix it fails with ERROR eclipse-threadx#30 in
both; with it, the mutex tests pass in both suites' default_build_coverage
configuration, and the changed sources build cleanly under -Wall -Wextra -Werror with
TX_NOT_INTERRUPTABLE, TX_MISRA_ENABLE, TX_DISABLE_PREEMPTION_THRESHOLD and event trace.
@fdesbiens
fdesbiens self-requested a review October 16, 2026 13:30
@fdesbiens fdesbiens self-assigned this Oct 16, 2026

@fdesbiens fdesbiens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @howjmay. Thank you for this contribution. Updating inheritance when a waiter’s priority changes addresses a real gap. I left one behavior issue below. Please also add a ready-owner test, report coverage for the modified production code, and retarget the PR to dev.

#endif

/* Update the priority inheritance of the mutex owner. */
(_tx_thread_mutex_priority_update)(mutex_ptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this update be limited to TX_INHERIT mutexes? For a TX_NO_INHERIT mutex with multiple waiters, the helper calls _tx_mutex_prioritize, so changing a waiter’s priority can change FIFO acquisition order. A regression checking that order would help protect this case.

_tx_thread_priority_change called _tx_mutex_inherit_priority_update for a thread
waiting on any mutex. For a TX_NO_INHERIT mutex with several waiters, the helper's
call to _tx_mutex_prioritize moved the changed waiter to the front of the suspension
list, so changing a waiter's priority changed the FIFO order in which the waiters
obtain the mutex. The update now runs only when the mutex was created with
TX_INHERIT, and the helper's header states that requirement. Applied to common and
common_smp.

threadx_mutex_inherit_waiter_priority_change_test now also checks:
- that three waiters on a TX_NO_INHERIT mutex obtain it in the order they blocked
  after the last one is raised above the others. Without the fix it fails with
  ERROR eclipse-threadx#62 in both suites.
- a ready owner, moved 10, 15, 8 and back to 15 by changes to its waiter's
  priority, and that the owner runs before the call returns when the caller is
  below the owner's new priority.
- an owner holding two TX_INHERIT mutexes and one TX_NO_INHERIT mutex, including
  a step that leaves the owner's priority unchanged.
- two cases set up by hand: a TX_MUTEX_SUSP thread whose cleanup has already been
  cleared, and the helper run on a mutex with no waiters left.

The test passes in every tx and smp build configuration. Merged coverage of the
changed lines is 100% of lines and branches, except the TX_MISRA_ENABLE retry loop
around _tx_mutex_prioritize, which cannot iterate because _tx_misra_status_get
always returns TX_SUCCESS. The same loops in tx_mutex_put.c are also half covered.
@howjmay
howjmay requested a review from fdesbiens October 18, 2026 03:55

This branch has not been deployed

No deployments
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.

2 participants