roadmap NOT-STARTED

912. C thread-locals on nife's thread pointer

(Promoted 2026-10-12 (UTC) by lane milestone/912-c-thread-locals-on-the-thread-pointer from the proposal c-thread-locals-on-the-thread-pointer, after calef approved the lane that day. The number is provisional until the merge queue lands it; the title and slug are drafts.)

Raised on 2026-10-12 (UTC) by lane milestone/836-a-c-library-stage-two-threads. It built POSIX threads for nife's C library and found that a C program's own thread-locals cannot be linked.

Reuse: the layouts are the ELF TLS ABI's. musl's __init_tls and pthread_impl.h show how a C library copies the template into each thread's block and where its own per-thread record sits beside it. Neither can be taken whole, because the block must also be std's, so the copy is written against std's thread_local/key/nife.rs.

Not started, and held for one question to calef (below). Nothing is built until he rules.

What is missing

A C variable declared __thread or _Thread_local is ELF thread-local storage. The compiler addresses it as an offset from the thread pointer (TPIDR_EL0, tp, the FS base). The linker lays the variables out in a PT_TLS segment, and a C library copies that into a block for each thread.

On nife the thread pointer already names std's own per-thread block. That is patches/std-nife/overlay/std/src/sys/thread_local/key/nife.rs, under §269 (how threads share a process) fork 4. The userspace link script has no PT_TLS segment. So a C program with one thread-local does not link:

rust-lld: error: ...(tls.o) has an STT_TLS symbol but doesn't have a PT_TLS segment

That is the right failure, since nothing is silently overwritten, and c_library/README.md's BUGS records it. Milestone 836 (a C library, stage 2: threads) moved relibc's own thread-locals onto std's thread_local!, so the library itself needs none of this.

Who needs it: not fio, corrected 2026-10-12 (UTC)

The proposal said fio (milestone 833 (the same storage benchmark on nife and Linux, by porting real fio)) needs this, citing __thread in stat.h, gettime.c and verify-state.h. Read in fio 3.40's source (ff930c4653ae), that is too strong:

So fio builds without this milestone, at the cost of one pthread_getspecific per fio_gettime call when the CPU clock is the source, against a Linux build that uses __thread. That is a few nanoseconds against microseconds per I/O, and it counts against nife, not for it: a caveat on milestone 833's numbers. This milestone no longer gates fatal risk 6. It is for the next C program that has no such fallback, and no program on the roadmap is yet known to be one.

What it would take

The kernel changes nothing. CONFIGURE and SET_THREAD_POINTER store a user-half value and install it at each switch; neither reads what it names (kernel/src/sched/configure.rs, is_thread_pointer). The proposal said the kernel agrees with std on what the pointer names. It does not, which narrows the question to std.

What the toolchain already fixes

Measured 2026-10-12 (UTC) with Homebrew clang 23.1.3 and the pinned nightly's rust-lld, linking three thread-locals (8 bytes initialized, 40 of .tbss, 8 aligned to 64) statically on each target with the C library's own flags:

variant first variable's address what the code also reads
aarch64 I pointer + 0x40 (16 bytes reserved, rounded up to the 64-byte alignment) nothing
riscv64 I pointer + 0 nothing
x86_64 II pointer - 0xc0 (the block ends at the pointer) fs:[0], which must hold the pointer itself

Every C object compiled for nife bakes these offsets in, and nife cannot choose them: the processor-specific ABIs and LLVM do. That part is irreversible in the sense that matters, and it is not a fork. Three facts follow:

Parked, 2026-10-12 (UTC)

calef parked this milestone the day it was minted: "Park it." fio, the program that prompted it, does not need it, and no other program on the path to a fatal risk does. The question below stays open and unasked until a C program that needs __thread is about to be ported; that lane brings it back to calef with this analysis.

Open questions for calef

  1. Where does std's block sit relative to the ELF block? §269 fork 4 ruled one thread-pointer ABI, set by the kernel. This keeps that contract and decides what the value names. Only std reads std's part, and std is rebuilt with every program, so whichever option is taken can be changed later by rebuilding; the part other code acts on is the table above, which is not a choice.

Recommendation: (a). It changes nothing the kernel or any test of the contract sees, costs nothing on the paths that are hot today, and is musl's and Fuchsia's shape. Asked whether (a) would still win if (c) cost the same: not entirely. At equal cost, (c) is the better end state for Rust's own thread-locals, faster and with no key limit. Preferring (a) here is about scope, and so partly about effort: (c) also replaces std's thread-local backend, which wants its own measurement. (a) does not foreclose (c), because (c) changes only where std keeps its own data, which (a) leaves private to std.

If calef says no to all of (a) to (c), C thread-locals stay unlinkable, BUGS keeps saying so, and (d) remains available per program.

Index row

C's own thread-locals, __thread and _Thread_local, on the thread pointer std already switches, so C programs that need them link. fio does not need it after all: its configure falls back to pthread_getspecific.