Skip to content

Added opt-in priority inheritance through chains of mutexes - #808

Merged
fdesbiens merged 4 commits into
eclipse-threadx:devfrom
howjmay:fix/mutex-inherit-chain
Nov 25, 2026
Merged

fdesbiens merged 4 commits into
eclipse-threadx:devfrom
howjmay:fix/mutex-inherit-chain

Conversation

@howjmay

@howjmay howjmay commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

ThreadX raises the priority of a mutex owner when a higher priority thread waits on an inheriting mutex. A second owner in the wait chain can still run at its original priority and delay the waiter.

TX_MUTEX_INHERIT_CHAIN_MAX enables propagation through the first owner and up to the configured number of further wait edges. The single-core and SMP kernels recompute inheritance when waiters leave, change priority, or transfer ownership. Cycles contained within the configured bound can drop a stale boost. A cycle that exceeds it may retain one. The walk holds scheduler protection, and its cost also depends on the number of mutexes and waiters it visits.

GCC 14 Linux regressions passed 125/125 single-core and 120/120 SMP with chaining on and off. Across six builds per kernel, gcov measured 150/150 changed executable lines and 102/102 branch outcomes in single-core, and 152/152 lines and 104/104 branch outcomes in SMP. The new release test failed with ERROR #366 in both kernels when the release guard was removed. Two focused RISC-V QEMU tests passed. Not run on hardware.

tx_mutex_get raised the owner of a TX_INHERIT mutex and stopped. If that owner was
itself blocked on a second inheriting mutex, the second owner never inherited the
priority: with H blocked on m1, owned by L1, blocked on m2, owned by L2, L2 stayed at
L1's old priority and H waited behind every thread between the two.

When TX_MUTEX_INHERIT_CHAIN_MAX is defined, tx_mutex_get passes the inherited priority
on through a new internal function, _tx_mutex_priority_chain. While the thread it last
raised is waiting on an inheriting mutex, it updates that mutex's highest waiting
priority and raises the mutex owner with _tx_mutex_priority_change, which handles ready
and suspended owners alike, for at most TX_MUTEX_INHERIT_CHAIN_MAX mutexes. It stops at
the first owner already at the priority or higher, which also ends it on a lock cycle.
The walk is iterative and only raises priorities. It is opt-in, so existing builds keep
their worst-case tx_mutex_get time. Documented in tx_user_sample.h. Applied to common
and common_smp.

threadx_mutex_inherit_chain_test covers a chain of four suspended owners, which also
checks the length limit, a chain ending at a ready owner, and a lock cycle, and checks
the unchained behaviour when the option is not defined. Added to both suites, with
TX_MUTEX_INHERIT_CHAIN_MAX=2 in default_build_coverage so the walk is built and covered.
Without the fix it fails with ERROR eclipse-threadx#12 in both; with it, the mutex tests pass in both
suites' default_build_coverage configuration and the new test passes in
disable_notify_callbacks_build, where the option is not defined. The changed sources
build cleanly under -Wall -Wextra -Werror with TX_NOT_INTERRUPTABLE, TX_MISRA_ENABLE and
TX_DISABLE_PREEMPTION_THRESHOLD, with and without the option.
@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. The bounded chain walk and its cycle test are useful. I left a comment on how the inherited priority is restored. Please retarget the PR to dev and share coverage for the new production code.

Comment thread common/src/tx_mutex_get.c Outdated
#ifdef TX_MUTEX_INHERIT_CHAIN_MAX

/* Pass the priority on to the owner of any mutex the owner is waiting on. */
_tx_mutex_priority_chain(mutex_owner, thread_ptr -> tx_thread_priority);

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.

This passes a priority boost to downstream owners, but I cannot find a matching update when the triggering waiter leaves or lowers its priority. In an H → L1 → L2 chain, L2 can retain H’s priority after H stops waiting, even if #807 restores L1. Could you update the affected chain on those paths and add regression coverage?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@howjmay
howjmay force-pushed the fix/mutex-inherit-chain branch 3 times, most recently from c5797de to f5fc49c Compare October 17, 2026 14:57
@howjmay
howjmay requested a review from fdesbiens October 18, 2026 03:55
The chain walk passed priority boosts to downstream mutex owners. A downstream
owner could retain a boost after the waiter stopped waiting or changed priority.

Recomputed inheritance after waiter and ownership changes, handled cycles
within the bound, and corrected the SMP priority change fast path. Added
restoration regressions and documented the chain bound in the sample user headers.

The contributor reported that the mutex regressions passed in both kernels.
Coverage of the chain update routines included 12/12 and 91/91 executable lines
and 5/6 and 82/86 branch outcomes, respectively, in each kernel.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>
@fdesbiens
fdesbiens force-pushed the fix/mutex-inherit-chain branch from f5fc49c to 4c2e0fb Compare November 24, 2026 22:13
@fdesbiens
fdesbiens changed the base branch from master to dev November 24, 2026 22:13
The bounded mutex chain changes and the wait-exit fix both changed the mutex
cleanup path and added the same regression to the single-core and SMP suites.

Kept the chain update under scheduler protection, retained the ready-owner
scenarios from dev, and registered each test once in each suite.

GCC 14 Linux tests passed 125/125 single-core and 120/120 SMP with chaining
on and off. The changed production lines covered 144/145 single-core and
145/146 SMP, and branch coverage reached 101/114 and 103/116 respectively.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>
The chain walk and the priority change API duplicated wait validity checks.
Several checks tested states the mutex lifecycle rules out. The release
path also retained a fallback for a mutex handled by the initial fast path.

Shared the active wait check and relied on the mutex lifecycle for the
callback, wait pointer, and owner. Removed the redundant release fallback.
The chain walk retains separate exits for an inactive edge, a release, a
cycle, and the bound to avoid maintaining an extra loop state (MISRA C:2023
Rule 15.4). Added tests for deletion in a cycle, a priority change on a
mutex without inheritance, and a wait abort during release.

GCC 14 Linux tests passed 125/125 single-core and 120/120 SMP with chaining
on and off. Changed code covered 150/150 single-core and 152/152 SMP lines
and 102/102 and 104/104 branch outcomes. Without the release guard, the
new test failed with ERROR eclipse-threadx#366 in both kernels. Two RISC-V QEMU tests
passed. The SMP noninterruptible performance configuration passed 13/13
on a serial rerun after one timeout regression failed in the parallel run.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>

@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.

The chain now restores inherited priorities when waiters leave or change priority, including in cycles within the configured bound. The single-core and SMP regressions cover deletion in a cycle, non-inheriting mutexes, and wait abort during release. Local GCC 14 coverage reached every changed executable line and branch outcome in both kernels, and all nine CI workflows passed.

@fdesbiens
fdesbiens merged commit f0783ff into eclipse-threadx:dev Nov 25, 2026
24 checks passed
fdesbiens pushed a commit that referenced this pull request Nov 25, 2026
The waiter priority update in #808 lacked a regression that checks
acquisition order after a priority change on a mutex without inheritance.

Added tests for the effective priority of suspended and ready owners and
for FIFO acquisition after one of three waiters changes priority.
Registered the tests in the single-core, SMP, and RISC-V suites.

GCC 14 Linux single-core and SMP runs and RISC-V QEMU runs passed with
chaining on and off. Both Linux kernels failed with ERROR #30 before the
waiter priority update. All three CI workflows passed.

Assisted-by: Codex (GPT-6.2-Sol) <noreply@openai.com>
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