How does linux kernel reset the 'used' flag in the 'tid' field in "struct pthread" of GLIBC?

Viewed 77

When going through the glibc code, a line is observed describing " Note that we do not reset the 'used' flag in the 'tid' field. This is done by the kernel" in glibc_source (version 2.21) (link to file in glibc source) at line 760.

As per my understanding glibc reuses the thread stack for T2 maintained in cache stack list. But before reusing that stack, it check for tid field in the thread descriptor of T1 (which is already reset to -1 after pthread_join of T1).

T1 - First thread.
T2 - Second thread created after T1 exited.

As per the comment in file (allocatestack.c:760), 'tid' field of struct pthread is reset by linux kernel. But, as per my understanding the tid field is reset by pthread_join.

how 'tid' field is reset by kernel ? Please correct if my understanding is wrong.

1 Answers

What is meant here is that the tid member is also used as a flag to indicate whether the stack is in use or not. It does not refer to a bit inside the member.

The kernel sets the tid member to zero when the thread exits because the clone system call is invoked with the CLONE_CHILD_CLEARTID flag, and the address of the tid member is passed to it. See sysdeps/unix/sysv/linux/createthread.c for details.

Related