Skip to content

!libc/pthread, sched: Fix PSE52 conformance issues. - #21834

Merged
xiaoxiang781216 merged 13 commits into
apache:masterfrom
Shanks0224:pse52-conformance
Nov 17, 2026
Merged

xiaoxiang781216 merged 13 commits into
apache:masterfrom
Shanks0224:pse52-conformance

Conversation

@Shanks0224

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

This series fixes behavior that the PSE52 profile (IEEE 1003.13) requires
and that its test suite checks: the PSE52 thread options in <unistd.h>
and sysconf(), thread cancellation, thread-specific data keys, circular
pthread_join(), scheduling policies and the PTHREAD_PRIO_PROTECT
unlock, EINVAL for invalid pthread objects, and the wait count of
condition variables. Each commit message describes its change.

The commits are submitted together because they are one conformance
change: the validity checks need the new constant values, and the
condition variable fixes build on each other.

Depends-On: apache/nuttx-apps#4049

Impact

BREAKING CHANGE: two commits change the values of public constants:

  • SCHED_FIFO, SCHED_RR, SCHED_SPORADIC, SCHED_OTHER and
    SCHED_NORMAL (commit "sched: Make the SCHED_* values match the TCB
    policy bits.").
  • PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED and the CLOCK_*
    IDs (commit "libc/pthread: Return EINVAL for invalid pthread objects.").

Code that uses the symbolic names only needs to be rebuilt. Code that
takes numeric values from the user must map them to the names; the
nuttx-apps PR above does this for the -c option of cyclictest.
pthread_cond_destroy() now returns EBUSY while threads wait on the
condition; the nuttx-apps PR updates the ostest cancel test for it.

Size, b-l475e-iot01a:nsh: text 121231 -> 121295 bytes (+64).

Testing

ostest passes before and after the change on a B-L475E-IOT01A board
(b-l475e-iot01a:nsh with CONFIG_TESTING_OSTEST=y), and with the change
in these builds:

  • flat: sim:ostest (Make and CMake builds), 5 loops.
  • protected: qemu-armv7r:pnsh with CONFIG_TESTING_OSTEST=y, QEMU 10.2.1.
  • kernel: qemu-armv7a:knsh with CONFIG_TESTING_OSTEST=y.

B-L475E-IOT01A, with the change:

nsh> uname -a
NuttX 13.1.1-RC0 4420b1cb38 Nov 13 2026 16:56:40 arm b-l475e-iot01a
nsh> ostest
stdio_test: write fd=1
...
user_main: Exiting
ostest_main: Exiting with status 0

No new regression test is added: the changes fix failures that the PSE52
test suite found, and that suite is not part of nuttx-apps.

Not tested: RISC-V, AArch64 and other architectures, SMP, and
CONFIG_SCHED_SPORADIC.

Shanks0224 and others added 13 commits November 13, 2026 15:02
PSE52 requires the thread priority inheritance, thread priority
protection and thread-safe functions options.  <unistd.h> did not
define _POSIX_THREAD_PRIO_INHERIT, _POSIX_THREAD_PRIO_PROTECT and
_POSIX_THREAD_SAFE_FUNCTIONS, so applications could not detect them at
build time, and sysconf(_SC_THREAD_SAFE_FUNCTIONS) returned -1.

Define the three macros.  The priority protocols are only advertised
when CONFIG_PRIORITY_INHERITANCE or CONFIG_PRIORITY_PROTECT is enabled,
and sysconf() returns the macros, so that the build time and the run
time answers agree.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
When a thread calls pthread_exit(), POSIX requires it to set its
cancelability state to PTHREAD_CANCEL_DISABLE and its cancelability
type to PTHREAD_CANCEL_DEFERRED.  pthread_exit() only disabled
cancellation, so a thread that had selected asynchronous cancellation
kept it while its cleanup handlers and TLS destructors ran.

Also set the cancelability type to deferred.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_kill() passed an invalid signal number to tkill() and took the
error from errno, so the call also changed the errno of the caller.
Check the signal number first and return EINVAL directly.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_join() returned EDEADLK only when a thread joined itself.
Threads that join each other in a circle, for example A joins B while
B joins A, blocked forever.  POSIX allows pthread_join() to fail with
EDEADLK when it detects a deadlock.

Follow the chain of the threads that the target is joining, and return
EDEADLK if it leads back to the caller.  Each step walks the members
of the task group, so the check is only built with
CONFIG_DEBUG_FEATURES.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_key_delete() and pthread_setspecific() only checked that the
key was in range.  They returned success for a key that was never
created or was already deleted, and pthread_setspecific() then stored
a value in a free slot.

Return EINVAL for such a key, as POSIX allows.  A key that is out of
range also returns EINVAL instead of failing an assertion.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
The SCHED_* values were one more than the policy bits that the TCB
stores, so every policy had to be translated between the two.

Renumber SCHED_FIFO, SCHED_RR, SCHED_SPORADIC and SCHED_OTHER to the
values 0 to 3, the same as the policy bits of the TCB, and define
TCB_FLAG_SCHED_OTHER for the value 3:
* nxsched_get_scheduler() returns the policy bits directly instead of
  adding one.
* nxsched_set_scheduler() handles SCHED_OTHER, SCHED_FIFO and SCHED_RR
  in one path, and maps SCHED_OTHER to SCHED_FIFO also when
  CONFIG_RR_INTERVAL is 0.
* sched_get_priority_min() returns EINVAL for an unknown policy instead
  of failing an assertion, as sched_get_priority_max() already does.

BREAKING CHANGE: The values of SCHED_FIFO, SCHED_RR, SCHED_SPORADIC,
SCHED_OTHER and SCHED_NORMAL changed.  Code that uses the symbolic
names only needs to be rebuilt.  Code that stores or exchanges the
numeric values must be updated.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
The previous commit renumbered the scheduling policies from
SCHED_FIFO (0) to SCHED_OTHER (3).  Update the range of the assertion
in posix_spawnattr_setschedpolicy(), which still checked SCHED_OTHER
to SCHED_SPORADIC.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
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 <ganjing@xiaomi.com>
nxnotify_cancellation() only woke a thread that waited at a
cancellation point with deferred cancellation.  For a thread with
asynchronous cancellation it returned false: pthread_cancel() then ran
the cleanup handlers of the target in the context of the caller and
terminated the target with nxtask_terminate(), also while the target
was blocked.

When a thread with asynchronous cancellation is blocked, mark the
cancellation pending and wake it with ECANCELED as for deferred
cancellation, so that it exits through its own cancellation path.  A
thread with asynchronous cancellation that is not blocked is still
terminated by the caller.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
The field holds one of the PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT and
PTHREAD_PRIO_PROTECT values.  Name it prio after them, so that the
name tells what the field selects.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
POSIX allows the pthread functions to fail with EINVAL when an
attribute object, a condition variable or a mutex is not initialized
or was destroyed, and PSE52 tests it.  Most of the functions only
checked for NULL or failed an assertion.

Check that the object is valid:
* pthread_attr_t: the priority is in range.
* pthread_condattr_t, pthread_mutexattr_t: pshared is a valid value.
* pthread_cond_t: clockid is CLOCK_REALTIME or CLOCK_MONOTONIC.
  pthread_cond_destroy() clears clockid, and returns EBUSY while
  threads wait on the condition.
* pthread_mutex_t: the semaphore is a mutex.  nxsem_destroy() clears
  the semaphore flags.

A zero-filled object must not pass the checks, so 0 is no longer a
valid PTHREAD_PROCESS_* or CLOCK_* value.  CLOCK_FD, CLOCK_MASK and
CLOCK_SHIFT move from <nuttx/clock.h> to <time.h>, next to the clock
IDs.

BREAKING CHANGE: PTHREAD_PROCESS_PRIVATE and PTHREAD_PROCESS_SHARED
are now 1 and 2, and CLOCK_REALTIME, CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, CLOCK_BOOTTIME and
CLOCK_FD are now 1 to 6.  Code that uses the symbolic names only needs
to be rebuilt.  Code that takes numeric values from the user, such as
the -c option of cyclictest in nuttx-apps, must map them to the names.
pthread_cond_destroy() now returns EBUSY while threads wait on the
condition: wake them before the condition is destroyed.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_cond_clockwait() counts the waiter in wait_count before it
blocks, and only pthread_cond_signal() and pthread_cond_broadcast()
drop the count of the waiters they wake.  A waiter whose wait timed out
kept its count, so pthread_cond_destroy() returned EBUSY afterwards,
and a later pthread_cond_broadcast() posted the semaphore for waiters
that were gone.

Drop the count when the wait times out.

Assisted-by: Kiro:auto
Signed-off-by: ganjing <ganjing@xiaomi.com>
pthread_cond_wait() and pthread_cond_clockwait() release the mutex and
only then block on cond->sem.  If the waiter is preempted in between,
another thread can take the mutex, call pthread_cond_signal() (the post
finds no blocked waiter and just raises the count) and then wait on the
same condition itself, consuming that post.  The original waiter blocks
afterwards and both threads wait forever.

PSE52 pthread_cancel, pthread_sigmask and sigwait stress hang this way
with RR scheduling: the coredumps show flag == 1, wait_count == 2 and
semcount == -2 with both threads on the cond waitlist and the mutex
free.

Hold sched_lock() across the release and the block so no other thread
on this CPU can run in the window.  Blocking with the scheduler locked
is already done by nxsem_wait_slow() for priority inheritance.  This
does not close the window against threads on another CPU under SMP.

Assisted-by: Kiro:auto
Signed-off-by: pangzhen1 <pangzhen1@xiaomi.com>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: L The size of the change in this PR is large labels Nov 13, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +24 B (+0.0%, 68,154 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +76 B (+0.1%, 131,784 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +76 B (+0.1%, 97,656 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +284 B (+0.3%, 88,004 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text -12 B (-0.0%, 70,624 B / 131,072 B, total: 54% used)

qemu-armv8a

  • Code: .rodata +88 B, .text.clock_nanosleep +4 B, .text.mqueue_test -4 B, .text.nx_pthread_create +4 B, .text.nxclock_gettime +4 B, .text.nxsched_get_scheduler -4 B, .text.nxsched_set_scheduler +4 B, .text.nxsem_clockwait -4 B, .text.nxsem_destroy +4 B, .text.nxsig_clockwait +4 B, .text.pthread_attr_destroy +8 B, .text.pthread_attr_setinheritsched +8 B, .text.pthread_attr_setschedparam +4 B, .text.pthread_attr_setschedpolicy +12 B, .text.pthread_attr_setstacksize +8 B, .text.pthread_barrierattr_init +4 B, .text.pthread_cond_broadcast +16 B, .text.pthread_cond_clockwait +84 B, .text.pthread_cond_destroy +40 B, .text.pthread_cond_init +20 B, .text.pthread_cond_signal +16 B, .text.pthread_cond_wait +32 B, .text.pthread_exit +12 B, .text.pthread_join +164 B, .text.pthread_kill +24 B, .text.pthread_mutex_destroy +4 B, .text.pthread_mutex_init +28 B, .text.pthread_mutexattr_init +4 B, .text.restart_thread.constprop.0 +64 B, .text.rr_test -4 B, .text.run_test_thread.constprop.0 +8 B, .text.sched_get_priority_min +8 B, .text.semtimed_test -4 B, .text.timeout_thread2 +4 B, .text.timer_create +4 B (+0.2%, 367,912 B)

qemu-intel64

  • Code: .text +769 B (+0.0%, 8,699,937 B)

rx65n-rsk2mb

  • ROM: .text +48 B (+0.1%, 93,172 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +1,360 B (+0.3%, 396,528 B)

stm32-nucleo-f103rb

  • flash: .text +8 B (+0.0%, 36,212 B / 131,072 B, total: 28% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/43403109691

@Shanks0224

Copy link
Copy Markdown
Contributor Author

CI fails because b-g431b-esc1:nsh, nucleo-g431kb:nsh, nucleo-g431rb:nsh and nucleo-l432kc:wgen overflow the flash by 160 to 1028 bytes.They build ostest with LTO, and the new EINVAL checks keep the ostest error paths that LTO used to drop.

Earlier overflows here were fixed by disabling unused NSH commands(cb6d987, b118542).Is there a guideline for which options to disable, or would you prefer another approach? @xiaoxiang781216

@xiaoxiang781216

xiaoxiang781216 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

CI fails because b-g431b-esc1:nsh, nucleo-g431kb:nsh, nucleo-g431rb:nsh and nucleo-l432kc:wgen overflow the flash by 160 to 1028 bytes.They build ostest with LTO, and the new EINVAL checks keep the ostest error paths that LTO used to drop.

Earlier overflows here were fixed by disabling unused NSH commands(cb6d987, b118542).Is there a guideline for which options to disable, or would you prefer another approach? @xiaoxiang781216

let's enable LTO first, but why this pr increase the code size so larger?

@Shanks0224

Copy link
Copy Markdown
Contributor Author

CI fails because b-g431b-esc1:nsh, nucleo-g431kb:nsh, nucleo-g431rb:nsh and nucleo-l432kc:wgen overflow the flash by 160 to 1028 bytes.They build ostest with LTO, and the new EINVAL checks keep the ostest error paths that LTO used to drop.
Earlier overflows here were fixed by disabling unused NSH commands(cb6d987, b118542).Is there a guideline for which options to disable, or would you prefer another approach? @xiaoxiang781216

let's enable LTO first, but why this pr increase the code size so larger?

Most of the growth is in ostest. On nucleo-g431rb:nsh the "Return EINVAL for invalid pthread objects" commit adds 1191 of the 1235 bytes.
About 800 bytes of that are ostest error messages: LTO inlines pthread_attr_setschedparam() and pthread_attr_setstacksize() and could prove that they return 0 for ostest's constant arguments, so it dropped
those error paths. The new PTHREAD_ATTR_IS_VALID() check keeps 12 of them.

nucleo-l432kc:wgen fits with CONFIG_LTO_FULL=y. The three stm32g4 configs already use LTO and still overflow by 160-1028 bytes.

@Shanks0224
Shanks0224 marked this pull request as ready for review November 16, 2026 11:08
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/43855070933

@xiaoxiang781216
xiaoxiang781216 merged commit ef20f41 into apache:master Nov 17, 2026
88 of 90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants