Repository navigation
include/unistd.h: Make the POSIX option macros follow the configuration. - #21146
Merged
Merged
Conversation
<unistd.h> defined _POSIX_SEMAPHORES, _POSIX_SHARED_MEMORY_OBJECTS, _POSIX_MAPPED_FILES, _POSIX_MESSAGE_PASSING, _POSIX_THREADS, _POSIX_THREAD_PROCESS_SHARED, _POSIX_CPUTIME and _POSIX_THREAD_CPUTIME in every configuration. A program that tests them then used functions that the build does not have: sem_open() needs CONFIG_FS_NAMED_SEMAPHORES, shm_open() needs CONFIG_FS_SHMFS, mmap() of a regular file needs CONFIG_FS_RAMMAP, and the CPU-time clocks need the run-time accounting of CONFIG_SCHED_CRITMONITOR. It also did not define _POSIX_BARRIERS, _POSIX_READER_WRITER_LOCKS, _POSIX_SPIN_LOCKS and _POSIX_THREAD_ATTR_STACKADDR, although the functions exist. <pthread.h> defined _POSIX_THREAD_ATTR_STACKSIZE with no value, so "#if _POSIX_THREAD_ATTR_STACKSIZE > 0" did not compile. Define each option as _POSIX_VERSION when the configuration has it, and as -1 when it does not. A kernel build has process-shared mutexes only with memory that two processes can share (CONFIG_FS_SHMFS or CONFIG_MM_SHM). Remove the empty fallbacks from <pthread.h>. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
_POSIX_SEMAPHORES, _POSIX_SHARED_MEMORY_OBJECTS, _POSIX_MAPPED_FILES, _POSIX_THREAD_PROCESS_SHARED and the CPU-time options are now -1 when the configuration does not have the functions. Give the Kconfig options in the remarks. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
_POSIX_THREAD_KEYS_MAX is the smallest PTHREAD_KEYS_MAX that POSIX allows, and it is always 128. <limits.h> defined it from CONFIG_TLS_NELEM (0 by default), so a program could not see that the configured number of keys is below the POSIX minimum. Define it as 128. PTHREAD_KEYS_MAX still follows CONFIG_TLS_NELEM; the Kconfig help now says that POSIX requires at least 128 keys and what each key costs. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
<limits.h> did not define PTHREAD_DESTRUCTOR_ITERATIONS, and tls_destruct() called each key destructor once. It also cleared the value after the call, so a value that a destructor set again was lost without a second call. Clear the value before the call, and repeat while a destructor ran, up to PTHREAD_DESTRUCTOR_ITERATIONS (4, the POSIX minimum) passes. Define PTHREAD_DESTRUCTOR_ITERATIONS in <limits.h>. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
POSIX requires LOGIN_NAME_MAX in <limits.h>, and sysconf() lists _SC_LOGIN_NAME_MAX. getlogin() returns at most NAME_MAX bytes, so define LOGIN_NAME_MAX as NAME_MAX + 1 (the terminating null included) and size the getlogin() buffer with it. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The pthread_key_create() page said that the destructor argument is ignored. The destructors run when a thread exits, and now again while a destructor sets a value, up to PTHREAD_DESTRUCTOR_ITERATIONS times. Say so, and that PTHREAD_KEYS_MAX is CONFIG_TLS_NELEM, with the POSIX minimum of 128. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
casaroli
requested review from
GUIDINGLI,
Ouss4,
jerpelea,
simbit18 and
xiaoxiang781216
as code owners
October 29, 2026 21:15
|
xiaoxiang781216
approved these changes
Oct 30, 2026
casaroli
marked this pull request as draft
October 30, 2026 08:28
acassis
pushed a commit
to apache/nuttx-apps
that referenced
this pull request
Oct 31, 2026
NuttX has the CPU-time clocks (CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID) only with the run-time accounting of the critical section monitor (CONFIG_SCHED_CRITMONITOR_MAXTIME_THREAD >= 0). Without it, <unistd.h> defines _POSIX_CPUTIME and _POSIX_THREAD_CPUTIME as -1 (apache/nuttx#21146), and the LTP tests of these options print "unsupported" or "not supported" instead of a result. Do not build them then, as for the other options that a configuration does not have. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
casaroli
marked this pull request as ready for review
October 31, 2026 22:10
acassis
approved these changes
Oct 31, 2026
Contributor
|
please fix ci error, @casaroli |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes the POSIX option macros of
<unistd.h>and some limits of<limits.h>tell the truth about the configuration, and makes key destructors repeat as POSIX requires.<unistd.h>defined_POSIX_SEMAPHORES,_POSIX_SHARED_MEMORY_OBJECTS,_POSIX_MAPPED_FILES,_POSIX_MESSAGE_PASSING,_POSIX_THREADS,_POSIX_THREAD_PROCESS_SHARED,_POSIX_CPUTIMEand_POSIX_THREAD_CPUTIMEin every configuration. A program that tests them then used functions that the build does not have:sem_open()needsCONFIG_FS_NAMED_SEMAPHORES,shm_open()needsCONFIG_FS_SHMFS,mmap()of a regular file needsCONFIG_FS_RAMMAP, and the CPU-time clocks need the run-time accounting ofCONFIG_SCHED_CRITMONITOR. It did not define_POSIX_BARRIERS,_POSIX_READER_WRITER_LOCKS,_POSIX_SPIN_LOCKSand_POSIX_THREAD_ATTR_STACKADDR, although the functions exist. Now each option is_POSIX_VERSIONwhen the configuration has it, and -1 when it does not. A kernel build has process-shared mutexes only with memory that two processes can share (CONFIG_FS_SHMFSorCONFIG_MM_SHM). The empty_POSIX_THREADSand_POSIX_THREAD_ATTR_STACKSIZEfallbacks in<pthread.h>go (<pthread.h>includes<unistd.h>first, so they never took effect).posix.rstgives the Kconfig option of each of these macros._POSIX_THREAD_KEYS_MAXis the smallestPTHREAD_KEYS_MAXthat POSIX allows, always 128.<limits.h>defined it fromCONFIG_TLS_NELEM(0 by default). Now it is 128;PTHREAD_KEYS_MAXstill followsCONFIG_TLS_NELEM, and the Kconfig help says that POSIX requires at least 128 keys and what each key costs.tls_destruct()called each key destructor once and cleared the value after the call, so a value that a destructor set again was lost. Now the value is cleared before the call, and the pass repeats while a destructor ran, up toPTHREAD_DESTRUCTOR_ITERATIONS(4, the POSIX minimum, new in<limits.h>) times.NAME_MAX + 1(getlogin()returns at mostNAME_MAXbytes), and thegetlogin()buffer uses it.08_pthread.rstsaid that the destructor argument ofpthread_key_create()is ignored.Impact
Possible breaking change (source level, every build mode, flat too): the values of these option macros change. An option that the configuration does not have is now -1:
_POSIX_SEMAPHORESCONFIG_FS_NAMED_SEMAPHORES_POSIX_SHARED_MEMORY_OBJECTSCONFIG_FS_SHMFS_POSIX_MAPPED_FILESCONFIG_FS_RAMMAP_POSIX_CPUTIME,_POSIX_THREAD_CPUTIMECONFIG_SCHED_CRITMONITOR_POSIX_MESSAGE_PASSINGCONFIG_DISABLE_MQUEUE_POSIX_SPIN_LOCKS(new)CONFIG_PTHREAD_SPINLOCKS_POSIX_THREAD_PROCESS_SHAREDCONFIG_FS_SHMFSandCONFIG_MM_SHM_POSIX_THREADS,_POSIX_THREAD_PRIORITY_SCHEDULING,_POSIX_THREAD_ATTR_STACKADDR(new),_POSIX_THREAD_ATTR_STACKSIZE,_POSIX_BARRIERS(new),_POSIX_READER_WRITER_LOCKS(new)CONFIG_DISABLE_PTHREADA program that only tests
#ifdefsees no difference. A program that tests the value (#if _POSIX_SEMAPHORES > 0) now takes its fallback when the function is missing; before, it did not link or failed at run time.sysconf(_SC_CPUTIME)andsysconf(_SC_THREAD_CPUTIME)return -1 withoutCONFIG_SCHED_CRITMONITOR(before: 201712L, whileclock_gettime()of these clocks failed withEINVAL).In-tree users: no C code in nuttx or nuttx-apps tests these macros.
lib_sysconf.creturns_POSIX_CPUTIME/_POSIX_THREAD_CPUTIME(see above). The lely-canopen patch in nuttx-apps already excludes NuttX from its_POSIX_MAPPED_FILESpaths.getlogin()keeps its buffer size.<limits.h>:_POSIX_THREAD_KEYS_MAXis 128 in every configuration (wasCONFIG_TLS_NELEM);LOGIN_NAME_MAXandPTHREAD_DESTRUCTOR_ITERATIONSare new.Key destructors: a destructor that sets a value again is called again (at most 4 passes), and a destructor sees its key with the value
NULL. In-tree users ofpthread_key_create()(ostest, the sim SMP code) give no destructor. Task-local storage (task_tls_alloc()) is not changed.No size change of a structure. No new Kconfig option.
Testing
Test programs
posixopts.c(each option macro must match what the build can do) andlimitsposix.c, on qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build, the tests as builtin apps), QEMU with HVF,CONFIG_TLS_NELEM=8:_POSIX_BARRIERS,_POSIX_READER_WRITER_LOCKS,_POSIX_SPIN_LOCKSdefined; barriers and rwlocks work_POSIX_THREAD_ATTR_STACKSIZE/STACKADDRhave a value_POSIX_MAPPED_FILESmatchesmmap()of a filemmap()fails: FAIL_POSIX_SEMAPHORESmatchessem_open()(default)sem_open(): FAILCONFIG_FS_NAMED_SEMAPHORES(knsh)_POSIX_THREAD_PROCESS_SHARED: a process-shared mutex and condition variable work across two tasks inshm_open()memory (knsh; flat withCONFIG_FS_SHMFS)_POSIX_THREAD_KEYS_MAX == 128LOGIN_NAME_MAXdefined, >= 9PTHREAD_DESTRUCTOR_ITERATIONSdefined, >= 4NULLNULLeach time: PASSThe results are the same on knsh and on flat nsh. ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh (also in the flat runs with the test apps), on master and with this PR.
Build, each OK: qemu-armv8a:nsh (flat, arm64), also with
CONFIG_FS_RAMMAPandCONFIG_SCHED_CRITMONITOR; qemu-armv8a:knsh (kernel, arm64), also withCONFIG_FS_NAMED_SEMAPHORES,CONFIG_PTHREAD_SPINLOCKSandCONFIG_MM_SHM; rv-virt:knsh64 (kernel, rv64); mps2-an500:nsh (flat, Cortex-M7), also withCONFIG_DISABLE_PTHREAD(without the ostest and getprime apps, which need pthreads, as on master); mps2-an500:knsh (protected, Cortex-M7).tools/checkpatch.sh -c -u -m -gpasses.