[libc] Make it possible to join the main thread (#221177)
Joining the main thread currently crashes because its ThreadAttributes
has a null platform_data pointer, causing Thread::wait() to dereference
a null futex.
While this is not a frequently used feature, it is supported by other
implementations, and I believe it is required by POSIX (the exec page
says that the main thread is created in a joinable state, and neither
pthread_join nor pthread_exit mention them not working on the main
thread).
This patch sets up the main thread attributes during startup:
- allocate a static futex for the main thread's clear_tid and point
platform_data to it
- invoke set_tid_address to have the kernel clear the futex and wake
waiters on thread termination
- mark the main thread as joinable (it was previously defaulting to
detached)
The test for this functionality exposed the bug where the thread return
value is not preserved (and therefore not passed to pthread_join) for
threads exiting via pthread_exit, so I fix that as well.
This patch required accessing the CLEAR_TID_VALUE constant from multiple
files, but I couldn't find a suitable header to place it in. Instead of
creating a new one, I just delete the constant, as we don't really care
about its value -- only that it is not zero.
Assisted-by: Gemini