This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 7e1dcb2784da695c5c52cff27557f83564f818f0 Author: ganjing <[email protected]> AuthorDate: Tue Nov 18 10:45:53 2025 +0800 sched/semaphore: Keep the queue position on a PRIO_PROTECT unlock. POSIX requires that a thread that unlocks a PTHREAD_PRIO_PROTECT mutex is not moved to the tail of the scheduling queue of its priority. nxsem_protect_post() restored the priority with nxsched_set_priority(), which puts the thread behind the other ready threads of that priority. Restore the priority through the saved priority plus one, then set sched_priority to the saved priority, so that the thread stays at the head of the queue. If the saved priority is already the maximum, set it directly. Assisted-by: Kiro:auto Signed-off-by: ganjing <[email protected]> --- sched/semaphore/sem_protect.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/sched/semaphore/sem_protect.c b/sched/semaphore/sem_protect.c index 07a1656db2f..797231c0985 100644 --- a/sched/semaphore/sem_protect.c +++ b/sched/semaphore/sem_protect.c @@ -86,23 +86,20 @@ int nxsem_protect_wait(FAR sem_t *sem) * Name: nxsem_protect_post * * Description: - * This function attempts to unlock the protected semaphore, restore the - * holder tcb priority. - * - * This is an internal OS interface. It is functionally equivalent to - * sem_wait except that: - * - * - It is not a cancellation point, and - * - It does not modify the errno value. + * Restore the priority that the holder had before it took the + * PTHREAD_PRIO_PROTECT semaphore. POSIX does not let the thread move + * to the tail of the scheduling queue of its priority here, so set the + * priority to the saved priority plus one first, then write the saved + * priority back without requeueing the thread. * * Input Parameters: * sem - Semaphore descriptor. * * Returned Value: - * This is an internal OS interface and should not be used by applications. - * It follows the NuttX internal error return policy: Zero (OK) is - * returned on success. A negated errno value is returned on failure. - * Possible returned errors: + * None + * + * Assumptions: + * sem->saved holds the priority to restore. * ****************************************************************************/ @@ -110,7 +107,27 @@ void nxsem_protect_post(FAR sem_t *sem) { if (sem->saved > 0) { - nxsched_set_priority(this_task(), sem->saved); + if (sem->saved < SCHED_PRIORITY_MAX) + { + /* Temporary priority boost to trigger scheduler re-evaluation + * and prevent moving to the tail of the scheduling queue + */ + + nxsched_set_priority(this_task(), sem->saved + 1); + + /* Restore original priority but keep queue position */ + + this_task()->sched_priority = sem->saved; + } + else + { + /* Already at max priority, just trigger scheduler re-evaluation */ + + nxsched_set_priority(this_task(), sem->saved); + } + + /* Clear saved priority */ + sem->saved = 0; } }
