[all-commits] [llvm/llvm-project] 2c930a: [libc] Store ThreadAttributes in the TCB (#220921)

Pavel Labath via All-commits all-commits at lists.llvm.org
Wed Sep 16 02:28:54 PDT 2026


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

  Changed paths:
    M libc/src/__support/threads/CMakeLists.txt
    M libc/src/__support/threads/linux/aarch64/tcb.h
    M libc/src/__support/threads/linux/riscv/tcb.h
    M libc/src/__support/threads/linux/thread.cpp
    M libc/src/__support/threads/linux/x86_64/tcb.h
    M libc/src/__support/threads/thread.cpp
    M libc/src/__support/threads/thread.h
    M libc/src/__support/threads/thread_attributes.h
    M libc/src/stdlib/exit.cpp
    M libc/startup/linux/CMakeLists.txt
    M libc/startup/linux/aarch64/tls.cpp
    M libc/startup/linux/do_start.cpp
    M libc/startup/linux/riscv/tls.cpp
    M libc/startup/linux/x86_64/tls.cpp
    M libc/test/integration/src/pthread/CMakeLists.txt
    A libc/test/integration/src/pthread/pthread_tls_align_test.cpp

  Log Message:
  -----------
  [libc] Store ThreadAttributes in the TCB (#220921)

This patch replaces the `thread_local self` object with the
ThreadAttributes pointer stored in the TCB.  current_thread() now
fetches this pointer directly via get_current_thread_attrib().

Since every thread now needs a TCB, init_tls can no longer bail out
when app.tls.size == 0 (this was actually dead code before this patch
since every binary contained at least one thread-local variable
(`self)`). I've removed that check and updated do_start to always set up
a TCB (defaulting to alignment 1 if there is no TLS segment).

The motivation for this change is to make the pthread_self function
async-signal-safe (as required by POSIX), including in the cases where
the function is invoked from a signal hander on the newly created thread
-- before pthread_create returns. This requires making sure that the
thread attribute pointer is initialized from the very first instruction
of the new thread -- something which is hard to do when the attributes
are stored in a TLS variable. This also aligns us with how other C
libraries operate.

While in there, I also remove atexit_callback_mgr from ThreadAttributes.
It simplifies thread startup, and the field was always initialized to
point to the same object anyway. If needed, this can always be changed
later.

Since aligning the various parts of the TLS block is the trickiest part
of this patch, I also add a test that verifies that we've done that
correctly.

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