[all-commits] [llvm/llvm-project] 4faf0a: [libc] Implement pthread_cleanup_push and pthread_...

Pavel Labath via All-commits all-commits at lists.llvm.org
Tue Sep 22 03:16:13 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 4faf0ae1d518d97381ed195c5e12d3923f770152
      https://github.com/llvm/llvm-project/commit/4faf0ae1d518d97381ed195c5e12d3923f770152
  Author: Pavel Labath <pavel at labath.sk>
  Date:   2026-09-22 (Tue, 22 Sep 2026)

  Changed paths:
    M libc/config/linux/aarch64/entrypoints.txt
    M libc/config/linux/riscv/entrypoints.txt
    M libc/config/linux/x86_64/entrypoints.txt
    M libc/hdr/types/CMakeLists.txt
    A libc/hdr/types/struct___pthread_cleanup_frame.h
    M libc/include/CMakeLists.txt
    M libc/include/llvm-libc-macros/pthread-macros.h
    M libc/include/llvm-libc-types/CMakeLists.txt
    A libc/include/llvm-libc-types/struct___pthread_cleanup_frame.h
    M libc/include/pthread.yaml
    M libc/src/__support/threads/CMakeLists.txt
    A libc/src/__support/threads/cleanup_stack.h
    M libc/src/__support/threads/thread.cpp
    M libc/src/__support/threads/thread_attributes.h
    M libc/src/pthread/CMakeLists.txt
    A libc/src/pthread/__pthread_cleanup_pop.cpp
    A libc/src/pthread/__pthread_cleanup_pop.h
    A libc/src/pthread/__pthread_cleanup_push.cpp
    A libc/src/pthread/__pthread_cleanup_push.h
    M libc/test/integration/src/pthread/CMakeLists.txt
    A libc/test/integration/src/pthread/pthread_cleanup_test.cpp
    M libc/utils/docgen/pthread.yaml
    M utils/bazel/llvm-project-overlay/libc/BUILD.bazel
    M utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
    M utils/bazel/llvm-project-overlay/libc/startup/linux/BUILD.bazel

  Log Message:
  -----------
  [libc] Implement pthread_cleanup_push and pthread_cleanup_pop (#223427)

This patch implements the core POSIX thread cleanup mechanism
(pthread_cleanup_push and pthread_cleanup_pop).

POSIX allows these functions to be implemented as macros opening and
closing a lexical block. We allocate an automatic
__pthread_cleanup_frame structure on the stack in the push macro and
unlink it in the pop macro, calling helper functions
(__pthread_cleanup_push and __pthread_cleanup_pop) to link/unlink the
frame on the thread's cleanup_stack.

The stack is drained in LIFO order during thread termination in
call_atexit_callbacks before running __cxa_thread_atexit and TSS key
destructors.

It's worth noting that GLIBC implements this functionality in a much
more complicated way. The cleanup functions are registered with the
unwinding machinery so that throwing an exception (or even just
returning) from the push/pop blocks triggers evaluation of the cleanup
handlers. This behavior is not required (though not forbidden either) by
POSIX, which states that all of these result in undefined behavior. The
cleanup handlers are only run upon explicit
pthread_cleanup_pop(non_zero) or pthread_exit. POSIX also requires
running them on thread cancellation, but we don't implement cancellation
(yet).

*Another* thing worth noting is that this PR causes a couple of headers
to not compile in overlay mode (because I do not provide a definition of
__pthread_cleanup_frame in that case). These are threading headers which
are never supposed to be used in overlay mode (we can't overlay that),
and this is most visible in the bazel build, where I had to mark those
targets as incompatible. There are ways to work around this (the
simplest being providing a placeholder definition of the type for
overlay mode), but that's something I'd like to avoid if possible.

Assisted-by: Gemini



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list