π§© What is the TCB (Thread Control Block)?
The TCB (Thread Control Block) is a small, per-thread data structure used internally by the runtime to store metadata about each thread.
It is typically located at the beginning or end of the threadβs TLS (Thread-Local Storage) block.
π¦ Whatβs Inside?
The exact contents are platform-specific, but usually include:
-
A pointer to the DTV (Dynamic Thread Vector)
-
errno
(per-thread error value) -
Thread ID or pointer to the thread descriptor (
struct pthread
) -
Guards or padding for alignment
π Where is it?
-
On x86_64, the TCB is typically placed at the end of the TLS block.
-
The thread pointer (
TP
) register (e.g.,%fs
) points to the TCB.
π§ Memory Layout Example
mmap memory block (e.g., allocated with mmap or _dl_early_allocate)
0x00000000 ββββββββββββββββββββββββββββββββββββββββββββββ
β Thread Stack β
ββββββββββββββββββββββββββββββββββββββββββββββ€
β TLS (.tdata/.tbss) β
ββββββββββββββββββββββββββββββββββββββββββββββ€
0x00001230 β TCB (Thread Control Block) β β %fs points here (TLS_TCB_AT_TP)
ββββββββββββββββββββββββββββββββββββββββββββββ
Note: On systems with
TLS_TCB_AT_TP
, the TCB is at the end of the TLS block, and the%fs
register points to it directly.
π§ Why It Matters
-
Allows fast access to
__thread
variables -
Needed for per-thread libc functions (like
errno
) -
Fundamental for thread creation and context switching
π Summary
The TCB is the anchor of thread-local state. It sits in TLS and is accessed via the thread pointer register. Itβs set up early by functions like __libc_setup_tls
.