Skip to content

libs/libc/pthread: Implement spin locks with atomics. - #21693

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:i-spinlock-up
Nov 11, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:i-spinlock-up

Conversation

@casaroli

@casaroli casaroli commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

pthread_spin_*() needed CONFIG_SPINLOCK and, outside a flat build, CONFIG_BOARDCTL: every lock and unlock was a boardctl(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 returning EPERM.

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:

  • A waiting thread only reads the lock word. With CONFIG_SMP it calls sched_yield() after 100 reads, without it after each read: with one CPU the holder cannot run while the waiter spins.
  • The lock word is an atomic variable in the memory of the program, so a lock in shared memory works between processes (PTHREAD_PROCESS_SHARED).
  • pthread_spin_lock() by the holder returns EDEADLK and pthread_spin_trylock() returns EBUSY, as before. pthread_spin_unlock() by another thread returns EPERM. pthread_spin_init() returns EINVAL for a pshared value that is not valid.
  • libc.csv lists the five functions.

Outside a flat build the atomic operations run in user mode. CONFIG_LIBC_ATOMIC_IRQ and CONFIG_LIBC_ATOMIC_HWSPINLOCK make them with up_irq_save(), which user mode cannot do: on RISC-V csrrc mstatus is an illegal instruction in U-mode (arch/risc-v/include/irq.h:870), and on ARMv6-M/ARMv7-M an unprivileged cpsid i or BASEPRI write has no effect, so the operation is not atomic. So CONFIG_PTHREAD_SPINLOCKS now depends on CONFIG_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_SPINLOCK has no user in the tree after this PR. #21696 removes it (a breaking change, so in its own PR).

Impact

  • CONFIG_PTHREAD_SPINLOCKS no longer depends on CONFIG_SPINLOCK and CONFIG_BOARDCTL, and no longer selects CONFIG_BOARDCTL_SPINLOCK. Every configuration that has the option keeps it, except a protected or kernel build on a chip with CONFIG_LIBC_ATOMIC_IRQ or CONFIG_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 (only esp32c3-legacy-devkit:knsh is such a build, and it does not set the option).
  • The lock word of pthread_spinlock_t is an atomic_t, not a spinlock_t: the same size, except with CONFIG_TICKET_SPINLOCK (4 bytes smaller). <pthread.h> includes <nuttx/atomic.h> instead of <nuttx/spinlock_type.h>.
  • Users in the tree: bl602evb:wifi, rv-virt:citest, sim:citest, sim:posix_test set CONFIG_PTHREAD_SPINLOCKS (all flat). They keep it and need no change; make savedefconfig gives the same defconfigs. Their CONFIG_SPINLOCK=y is no longer needed for spin locks but stays valid. nuttx-apps (examples/tlpi, testing/ltp) only test CONFIG_PTHREAD_SPINLOCKS: no change.
  • Without CONFIG_PTHREAD_SPINLOCKS nothing changes.
  • With it: no system call per lock and unlock in protected and kernel builds (qemu-armv8a:knsh: 110 ns per lock/unlock pair before, 4-7 ns after), and CONFIG_SPINLOCK is no longer needed. Size, nucleo-f072rb:nsh (armv6-m) with CONFIG_PTHREAD_SPINLOCKS: text 89723 bytes before (with the CONFIG_SPINLOCK it needed), 87611 after.
  • Flat-build API: pthread_spinlock_t (see above), pthread_spin_init() with a bad pshared (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) trylock by another thread: EBUSY; 3) trylock/lock by the holder: EBUSY/EDEADLK; 4) unlock by another thread and of a free lock: EPERM; 5) pthread_spin_init() with a bad pshared: EINVAL; 6) PTHREAD_PROCESS_SHARED: the lock and a counter in a shm_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.

Configuration master 03aea78 / 6f47a1c this PR
qemu-armv8a:knsh (kernel, arm64, 1 CPU) 1-3 PASS; 4: assertion in pthread_spinlock.c:294, the program ends; 5 FAIL (0); 6 PASS; 110 ns per pair all PASS, 4-7 ns per pair, ostest 0
qemu-armv8a:nsh (flat, arm64) all PASS, 19 ns, ostest 0
qemu-armv8a:nsh_smp (flat, arm64, 4 CPUs) all PASS (400000, no overlap), 10 ns, ostest 0
rv-virt:knsh64 (kernel, rv64) 1-5 PASS, 6 SKIP (CONFIG_FS_SHMFS needs CONFIG_ARCH_SHM_VBASE there), 385 ns, ostest 0
mps2-an500:knsh (protected, armv7-m: ldrex/strex in unprivileged mode) all PASS, 202 ns

LTP open_posix_testsuite 20230516, the 15 pthread_spin_* cases, on rv-virt:knsh64: 12 PASS (some with the note that a "may fail" error was not returned); pthread_spin_init/2-1 and 2-2 UNRESOLVED (no shared memory in that configuration; on qemu-armv8a:knsh with CONFIG_FS_SHMFS both PASS, before and after); pthread_spin_unlock/3-1 FAIL: the case wants 0 from pthread_spin_unlock() by a thread that does not hold the lock, and NuttX returns EPERM (POSIX allows it), before and after. On qemu-armv8a:knsh the cases where a thread calls exit() while main() waits in pthread_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=y added, make olddefconfig keeps it in qemu-armv8a:knsh, rv-virt:knsh64, mps2-an500:knsh and raspberrypi-pico:nsh (flat, CONFIG_LIBC_ATOMIC_IRQ), and drops it in esp32c3-legacy-devkit:knsh (protected, CONFIG_LIBC_ATOMIC_IRQ). make savedefconfig of bl602evb:wifi, rv-virt:citest, sim:citest and sim: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_SPINLOCKS with CMake (Ninja): no warning.

Real hardware, flat nsh with CONFIG_PTHREAD_SPINLOCKS and spltest as a builtin (case 6 SKIP: no shared memory in these configurations):

Board spltest ostest
Pimoroni Pico Plus 2, pimoroni-pico-2-plus:smp (RP2350, 2 Cortex-M33) 1-5 PASS (400000, no overlap), 2050 ns per pair status 0
Pimoroni Pico Plus 2, pimoroni-pico-2-plus:nsh 1-5 PASS, 1750 ns per pair status 0

tools/checkpatch.sh -c -u -m -g origin/master..HEAD passes.

@casaroli casaroli added the breaking change This change requires a mitigation entry in the release notes. label Nov 11, 2026
Comment thread libs/libc/pthread/pthread_spinlock.c Resolved
Comment thread include/pthread.h Resolved
Comment thread libs/libc/pthread/pthread_spinlock.c Resolved
@casaroli casaroli removed the breaking change This change requires a mitigation entry in the release notes. label Nov 11, 2026
@casaroli casaroli changed the title !libs/libc/pthread: Implement spin locks with atomics. libs/libc/pthread: Implement spin locks with atomics. Nov 11, 2026
@casaroli
casaroli marked this pull request as ready for review November 11, 2026 10:29
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels 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]>
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


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

@xiaoxiang781216
xiaoxiang781216 merged commit 89017e9 into apache:master Nov 11, 2026
47 of 54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants