git: libc: Merge libthread_xu into libc
Aaron LI
aly at crater.dragonflybsd.org
Thu Sep 17 06:25:14 PDT 2026
commit 28e770f9356419201058747986287acae231f334
Author: LI Leding <lileding at gmail.com>
Date: Thu Sep 17 17:06:01 2026 +0800
libc: Merge libthread_xu into libc
* Build the pthread implementation in libthread_xu as part of libc.
Keep libthread_xu and libpthread as empty compatibility shims so
existing DT_NEEDED entries continue to load.
Initialize pthread state lazily from public pthread entry points,
so processes that never use pthread avoid startup work, signal
handlers, extra mappings, and rtld lock activation.
Preserve cold fork and cancellation-point syscall performance with a
direct __fork() path when no pthread work is needed and raw
read/write/readv/writev syscalls before pthread state exists. Repair
fork-child pid and tid state lazily.
Keep existing libc symbol versions, publish the newly linkable
pthread_create and pthread_cancel interfaces in DF606.0, and retain
the historical pthread names as compatibility ABI.
* Honor pending self-cancel at cancellation points in _thr_cancel_enter()
The single-threaded cancellation fast path skips cancellation
bookkeeping because another thread cannot target the process. A
thread can still call pthread_cancel(pthread_self()), however, leaving
THR_CANCEL_NEEDED pending for the next cancellation point.
Only use the fast path when no cancellation request is pending.
Otherwise enter the normal cancellation-point path so the request is
acted on.
* Relock cond mutex on cancellation in cond_cancel_handler()
POSIX requires pthread_cond_wait() cancellation to reacquire the
associated mutex before invoking the first cancellation cleanup
handler:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_cond_clockwait.html
The cancellation handler removed the waiter from the
condition-variable queue but left the mutex unlocked. A cleanup
handler that followed the usual unlock pattern therefore received
EPERM, and the mutex retained its condition-wait reference so
pthread_mutex_destroy() returned EBUSY.
Reacquire the mutex with _mutex_cv_lock() before user cleanup handlers
run, and panic if the internal relock unexpectedly fails.
* nmalloc: clean up magazines directly at thread exit
nmalloc currently creates a pthread key and registers every thread
with pthread_setspecific() solely to reclaim its per-thread magazines.
Now that pthreads live in libc, use direct libc thread lifecycle hooks
instead.
Run application TSD destructors first, then drain the allocator
magazines. Marking the magazine state dead also makes any later
internal pthread teardown allocations bypass the magazine layer.
* Initialize pthread state during TLS startup
Initialize the initial pthread after installing its TCB, before
preinit and constructors run. Remove the per-entry lazy initialization
checks so internal libc callers, including DSO finalization, can rely
on curthread being available.
Keep threading activation and the single-threaded fast paths
unchanged. Preserve initialization reentry protection and fork-child
reinitialization.
GitHub-PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/52
Summary of changes:
lib/libc/Makefile | 1 +
lib/libc/gen/Makefile.inc | 6 +-
lib/libc/gen/Symbol.map | 8 +-
.../libc_private.h => libc/gen/_atomic_lock.c} | 24 +-
lib/libc/gen/_pthread_stubs.c | 308 ---------------------
lib/libc/gen/_spinlock_stub.c | 97 -------
lib/libc/gen/_thread_init.c | 37 +--
lib/libc/gen/raise.c | 40 ---
lib/libc/gen/tls.c | 1 +
lib/libc/include/libc_private.h | 3 +
lib/libc/stdlib/Symbol.map | 1 -
lib/libc/stdlib/dmalloc.c | 9 +
lib/libc/stdlib/nmalloc.c | 48 +---
lib/libc/thread/Makefile.inc | 54 ++++
lib/libc/thread/Symbol.map | 169 +++++++++++
lib/libc/upmap/upmap.c | 8 +-
lib/libpthread/Makefile | 17 +-
lib/libpthread/dummy.c | 28 +-
lib/libpthread/pthread.3 | 89 +-----
lib/libthread_xu/Makefile | 51 +---
lib/libthread_xu/arch/x86_64/Makefile.inc | 3 -
lib/libthread_xu/dummy.c | 8 +
lib/libthread_xu/thread/Makefile.inc | 60 ----
lib/libthread_xu/thread/thr_cancel.c | 21 ++
lib/libthread_xu/thread/thr_cond.c | 11 +-
lib/libthread_xu/thread/thr_create.c | 11 +-
lib/libthread_xu/thread/thr_exit.c | 3 +
lib/libthread_xu/thread/thr_fork.c | 28 +-
lib/libthread_xu/thread/thr_getthreadid_np.c | 2 +
lib/libthread_xu/thread/thr_init.c | 121 ++++----
lib/libthread_xu/thread/thr_list.c | 1 +
lib/libthread_xu/thread/thr_mutex.c | 4 -
lib/libthread_xu/thread/thr_once.c | 2 -
lib/libthread_xu/thread/thr_private.h | 11 +-
lib/libthread_xu/thread/thr_self.c | 6 +-
lib/libthread_xu/thread/thr_spec.c | 7 +-
lib/libthread_xu/thread/thr_syscalls.c | 31 ++-
37 files changed, 516 insertions(+), 813 deletions(-)
copy lib/{libc_rtld/libc_private.h => libc/gen/_atomic_lock.c} (83%)
delete mode 100644 lib/libc/gen/_pthread_stubs.c
delete mode 100644 lib/libc/gen/_spinlock_stub.c
delete mode 100644 lib/libc/gen/raise.c
create mode 100644 lib/libc/thread/Makefile.inc
create mode 100644 lib/libc/thread/Symbol.map
delete mode 100644 lib/libthread_xu/arch/x86_64/Makefile.inc
create mode 100644 lib/libthread_xu/dummy.c
delete mode 100644 lib/libthread_xu/thread/Makefile.inc
http://gitweb.dragonflybsd.org/dragonfly.git/commitdiff/28e770f9356419201058747986287acae231f334
--
DragonFly BSD source repository
More information about the Commits
mailing list