Repository navigation
libs/libc/pthread: Implement spin locks with atomics. - #21693
Merged
Merged
Conversation
casaroli
force-pushed
the
i-spinlock-up
branch
from
November 11, 2026 10:23
ae8a9d4 to
4f781b1
Compare
casaroli
force-pushed
the
i-spinlock-up
branch
from
November 11, 2026 10:29
4f781b1 to
f7bf405
Compare
casaroli
marked this pull request as ready for review
November 11, 2026 10:29
casaroli
requested review from
Donny9,
jerpelea,
simbit18 and
yamt
as code owners
November 11, 2026 10:30
casaroli
force-pushed
the
i-spinlock-up
branch
from
November 11, 2026 10:47
f7bf405 to
3b4e991
Compare
xiaoxiang781216
approved these changes
Nov 11, 2026
jerpelea
approved these changes
Nov 11, 2026
pthread_spin_*() needed CONFIG_SPINLOCK and, outside a flat build, BOARDCTL: every lock and unlock was a boardctl(BOARDIOC_SPINLOCK) call, so a system call in protected and kernel builds. Implement the functions in libc with an atomic compare-and-swap on the lock word (nuttx/atomic.h), so they need no system call. A waiting thread only reads the lock word, and calls sched_yield() after 100 reads with CONFIG_SMP and after each read without it, so that the holder can run. The lock word is an atomic_t, so PTHREAD_PROCESS_SHARED also works. pthread_spin_unlock() by a thread that does not hold the lock returns EPERM also in a debug build, and pthread_spin_init() returns EINVAL for a pshared value that is not valid. Add the functions to libc.csv. CONFIG_PTHREAD_SPINLOCKS no longer needs CONFIG_SPINLOCK and CONFIG_BOARDCTL, and no longer selects CONFIG_BOARDCTL_SPINLOCK. Outside a flat build the atomic operations run in user mode. LIBC_ATOMIC_IRQ and LIBC_ATOMIC_HWSPINLOCK disable the interrupts, which user mode cannot do, so there the option needs LIBC_ATOMIC_TOOLCHAIN or LIBC_ATOMIC_ARCH. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
Say that pthread_spin_*() need no system call, which atomic operations CONFIG_PTHREAD_SPINLOCKS needs outside a flat build, and that a mutex is the better choice, except on SMP with very short critical sections. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
i-spinlock-up
branch
from
November 11, 2026 13:14
3b4e991 to
5b794ad
Compare
|
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
pthread_spin_*()neededCONFIG_SPINLOCKand, outside a flat build,CONFIG_BOARDCTL: every lock and unlock was aboardctl(BOARDIOC_SPINLOCK)call, so a system call in protected and kernel builds, and the waiting thread spun inside the kernel. In a debug build,pthread_spin_unlock()by a thread that does not hold the lock ended the program with an assertion (pthread_spinlock.c:294) instead of returningEPERM.The first commit implements the functions in libc with an atomic compare-and-swap on the lock word (
nuttx/atomic.h), so they need no system call:CONFIG_SMPit callssched_yield()after 100 reads, without it after each read: with one CPU the holder cannot run while the waiter spins.PTHREAD_PROCESS_SHARED).pthread_spin_lock()by the holder returnsEDEADLKandpthread_spin_trylock()returnsEBUSY, as before.pthread_spin_unlock()by another thread returnsEPERM.pthread_spin_init()returnsEINVALfor apsharedvalue that is not valid.libc.csvlists the five functions.Outside a flat build the atomic operations run in user mode.
CONFIG_LIBC_ATOMIC_IRQandCONFIG_LIBC_ATOMIC_HWSPINLOCKmake them withup_irq_save(), which user mode cannot do: on RISC-Vcsrrc mstatusis an illegal instruction in U-mode (arch/risc-v/include/irq.h:870), and on ARMv6-M/ARMv7-M an unprivilegedcpsid iorBASEPRIwrite has no effect, so the operation is not atomic. SoCONFIG_PTHREAD_SPINLOCKSnow depends onCONFIG_BUILD_FLAT || CONFIG_LIBC_ATOMIC_TOOLCHAIN || CONFIG_LIBC_ATOMIC_ARCH. A flat build on such a chip (bl602evb:wifi) keeps the option.The second commit describes the spin locks and the dependency in
Documentation/standards/posix.rst(prefer a mutex: a spin lock only helps on SMP with very short critical sections).BOARDIOC_SPINLOCKhas no user in the tree after this PR. #21696 removes it (a breaking change, so in its own PR).Impact
CONFIG_PTHREAD_SPINLOCKSno longer depends onCONFIG_SPINLOCKandCONFIG_BOARDCTL, and no longer selectsCONFIG_BOARDCTL_SPINLOCK. Every configuration that has the option keeps it, except a protected or kernel build on a chip withCONFIG_LIBC_ATOMIC_IRQorCONFIG_LIBC_ATOMIC_HWSPINLOCK: there the user-mode atomics of libc do not work (the semaphore and mutex fast paths of master have the same problem). No configuration in the tree is affected (onlyesp32c3-legacy-devkit:knshis such a build, and it does not set the option).pthread_spinlock_tis anatomic_t, not aspinlock_t: the same size, except withCONFIG_TICKET_SPINLOCK(4 bytes smaller).<pthread.h>includes<nuttx/atomic.h>instead of<nuttx/spinlock_type.h>.bl602evb:wifi,rv-virt:citest,sim:citest,sim:posix_testsetCONFIG_PTHREAD_SPINLOCKS(all flat). They keep it and need no change;make savedefconfiggives the same defconfigs. TheirCONFIG_SPINLOCK=yis no longer needed for spin locks but stays valid. nuttx-apps (examples/tlpi,testing/ltp) only testCONFIG_PTHREAD_SPINLOCKS: no change.CONFIG_PTHREAD_SPINLOCKSnothing changes.qemu-armv8a:knsh: 110 ns per lock/unlock pair before, 4-7 ns after), andCONFIG_SPINLOCKis no longer needed. Size,nucleo-f072rb:nsh(armv6-m) withCONFIG_PTHREAD_SPINLOCKS: text 89723 bytes before (with theCONFIG_SPINLOCKit needed), 87611 after.pthread_spinlock_t(see above),pthread_spin_init()with a badpshared(EINVAL),pthread_spin_unlock()by another thread (EPERM, no assertion).Testing
Test program (
spltest.c, compiled against the export or as a builtin): 1) 4 threads x 100000 increments of a plain counter under one lock, with a check that no two threads are inside at once; 2)trylockby another thread:EBUSY; 3)trylock/lockby the holder:EBUSY/EDEADLK; 4)unlockby another thread and of a free lock:EPERM; 5)pthread_spin_init()with a badpshared:EINVAL; 6)PTHREAD_PROCESS_SHARED: the lock and a counter in ashm_open()object, this program and a child (posix_spawnp()) 50000 increments each; 7) time of a lock/unlock pair. Then ostest. QEMU on macOS (HVF for arm64, TCG for the others),CONFIG_PTHREAD_SPINLOCKS=y(+CONFIG_FS_SHMFS),EXTRAFLAGS=-Werror.qemu-armv8a:knsh(kernel, arm64, 1 CPU)pthread_spinlock.c:294, the program ends; 5 FAIL (0); 6 PASS; 110 ns per pairqemu-armv8a:nsh(flat, arm64)qemu-armv8a:nsh_smp(flat, arm64, 4 CPUs)rv-virt:knsh64(kernel, rv64)CONFIG_FS_SHMFSneedsCONFIG_ARCH_SHM_VBASEthere), 385 ns, ostest 0mps2-an500:knsh(protected, armv7-m:ldrex/strexin unprivileged mode)LTP open_posix_testsuite 20230516, the 15
pthread_spin_*cases, onrv-virt:knsh64: 12 PASS (some with the note that a "may fail" error was not returned);pthread_spin_init/2-1and2-2UNRESOLVED (no shared memory in that configuration; onqemu-armv8a:knshwithCONFIG_FS_SHMFSboth PASS, before and after);pthread_spin_unlock/3-1FAIL: the case wants 0 frompthread_spin_unlock()by a thread that does not hold the lock, and NuttX returnsEPERM(POSIX allows it), before and after. Onqemu-armv8a:knshthe cases where a thread callsexit()whilemain()waits inpthread_join()(pthread_spin_init/4-1,pthread_spin_destroy/3-1) do not end, before and after; a program without spin locks does the same on master.Kconfig: with
CONFIG_PTHREAD_SPINLOCKS=yadded,make olddefconfigkeeps it inqemu-armv8a:knsh,rv-virt:knsh64,mps2-an500:knshandraspberrypi-pico:nsh(flat,CONFIG_LIBC_ATOMIC_IRQ), and drops it inesp32c3-legacy-devkit:knsh(protected,CONFIG_LIBC_ATOMIC_IRQ).make savedefconfigofbl602evb:wifi,rv-virt:citest,sim:citestandsim:posix_test: no change.Builds, each OK with
EXTRAFLAGS=-Werror:bl602evb:wifi(flat,CONFIG_LIBC_ATOMIC_IRQ),rv-virt:citest(with the LTP spin cases),nucleo-f072rb:nsh+CONFIG_PTHREAD_SPINLOCKS(armv6-m,CONFIG_LIBC_ATOMIC_IRQ);qemu-armv8a:nsh+CONFIG_PTHREAD_SPINLOCKSwith CMake (Ninja): no warning.Real hardware, flat
nshwithCONFIG_PTHREAD_SPINLOCKSandspltestas a builtin (case 6 SKIP: no shared memory in these configurations):pimoroni-pico-2-plus:smp(RP2350, 2 Cortex-M33)pimoroni-pico-2-plus:nshtools/checkpatch.sh -c -u -m -g origin/master..HEADpasses.