JunyuanShen943 opened a new pull request, #22214:
URL: https://github.com/apache/nuttx/pull/22214

   ## Summary
   
   * Why: priority inheritance and priority protection only make sense for
     mutexes, which have a single owner.  Every in-tree user of
     `SEM_PRIO_INHERIT` already pairs it with `SEM_TYPE_MUTEX`
     (`nxmutex_t`, `pthread_mutex_t`), so priority inheritance on counting
     semaphores is unused, yet it keeps the multi-holder machinery: a
     `CONFIG_SEM_PREALLOCHOLDERS` pool, a per-semaphore holder list, per-holder
     count arithmetic and a two-pass priority restore.
   * How:
     - Keep a single holder embedded in `sem_t`; remove the `counts` field,
       the `hhead`/`flink` list, the preallocated pool,
       `nxsem_foreachholder()`/`nxsem_add_holder()` and
       `nxsem_initialize()`/`nxsem_initialize_holders()`.
     - `nxsem_set_protocol()` (sched and libc) rejects `SEM_PRIO_INHERIT` and
       `SEM_PRIO_PROTECT` on a semaphore without `SEM_TYPE_MUTEX` with
       `-EINVAL`.
     - Drop the counting-semaphore priority inheritance check from the libc
       wait/post/trywait fast paths.
     - Remove `CONFIG_SEM_PREALLOCHOLDERS` from Kconfig, three defconfigs and
       the espressif `struct __lock` sizing.
     - Second commit updates the documentation.
   * Mutex priority inheritance behaviour is unchanged.
   * Companion apps PR (ostest `priority_inheritance` converted to a
     `PTHREAD_PRIO_INHERIT` mutex): 
https://github.com/apache/nuttx-apps/pull/4116.  Without it,
     ostest `priority_inheritance` calls `sem_setprotocol(SEM_PRIO_INHERIT)`
     on a counting semaphore, which now fails.
   
   ## Impact
   
   * New feature or changed behavior: YES, see BREAKING CHANGE below.
   * User adaptation needed: YES.  Code that sets `SEM_PRIO_INHERIT` or
     `SEM_PRIO_PROTECT` on a counting semaphore gets `EINVAL` and should use a
     mutex instead (`nxmutex_t` in the kernel, `pthread_mutex_t` with
     `PTHREAD_PRIO_INHERIT` in applications).  Remove
     `CONFIG_SEM_PREALLOCHOLDERS` from out-of-tree defconfigs.
   * Build: NO.
   * Hardware: NO.
   * Documentation: YES, updated in this PR.
   * Security: NO.
   * Compatibility: with `CONFIG_PRIORITY_INHERITANCE`, `sem_t` layout
     changes.  Builds that used `CONFIG_SEM_PREALLOCHOLDERS > 0` (the default
     is 4 or 8) had a `hhead` pointer in `sem_t` plus a global pool of holder
     structures; they now embed one holder (three pointers) in `sem_t` and the
     pool is gone.  Builds with `CONFIG_SEM_PREALLOCHOLDERS=0` keep the
     embedded holder minus its `counts` field.
   
   BREAKING CHANGE: `sem_setprotocol()` and `nxsem_set_protocol()` fail with
   `EINVAL` for `SEM_PRIO_INHERIT` / `SEM_PRIO_PROTECT` on a counting
   semaphore, and `CONFIG_SEM_PREALLOCHOLDERS` is removed.  Migrate such
   semaphores to a mutex.
   
   ## Testing
   
   I confirm that changes are verified on local setup and works as intended:
   
   * Build Host: Ubuntu 22.04 x86_64, gcc 13.1.0
   * Target: sim (x86_64)
   
   Runs (this PR together with the companion apps PR):
   
   | Config | ostest | priority_inheritance |
   |---|---|---|
   | sim:ostest (`PRIORITY_INHERITANCE=y`) | Exiting with status 0 | PASSED |
   | sim:ostest + `PRIORITY_PROTECT=y` | Exiting with status 0 | PASSED |
   | sim:smp + `PRIORITY_INHERITANCE=y` | Exiting with status 0 | PASSED |
   | mps3-an547:nsh (QEMU, Cortex-M55) + `PRIORITY_INHERITANCE=y` 
`PRIORITY_PROTECT=y` | Exiting with status 0 | PASSED |
   | sim:ostest on master (baseline) | Exiting with status 0 | PASSED |
   
   Also: `tools/checkpatch.sh -m` clean, sim:nsh make and CMake builds pass,
   `ostest` exits with status 0, mps3-an547:nsh with PI/PROTECT builds with
   arm-none-eabi-gcc.
   
   Testing logs before change (sim:ostest, master, counting semaphore with
   `SEM_PRIO_INHERIT`, 3 low priority holders):
   
   ```
   lowpri_thread-3: Sem count: -1, No. highpri thread: 1
   highpri_thread-1: SUCCESS midpri_thread is still running!
   lowpri_thread-3: SUCCESS priority before sem_post: 255
   lowpri_thread-3: SUCCESS final priority: 1
   priority_inheritance: PASSED Priority were correctly restored.
   ```
   
   Testing logs after change (sim:ostest, this PR + apps PR, mutex with
   `PTHREAD_PRIO_INHERIT`):
   
   ```
   lowpri_thread-1: No. highpri thread waiting: 1
   highpri_thread-1: SUCCESS midpri_thread is still running!
   lowpri_thread-1: SUCCESS priority before unlock: 255
   lowpri_thread-1: SUCCESS final priority: 1
   priority_inheritance: PASSED Priority were correctly restored.
   ostest_main: Exiting with status 0
   ```
   
   Verified on sim and QEMU only, not on physical hardware.  The espressif
   `struct __lock` change was not built (no xtensa toolchain here); it only
   drops the `CONFIG_SEM_PREALLOCHOLDERS > 0` branch and keeps the larger
   reserved size.
   
   ## PR verification Self-Check
   
   * [x] This PR introduces only one functional change.
   * [x] I have updated all required description fields above.
   * [x] My PR adheres to Contributing Guidelines and Documentation (git commit 
title and message, coding standard, etc).
   * [ ] My PR is still work in progress (not ready for review).
   * [x] My PR is ready for review and can be safely merged into a codebase.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to