Make getpid work before TLS has been initialized.
Bug: http://b/29622562
Change-Id: I648adc35c04604a7e8bc649c425f07a723e96d3a
Test: code dependent on this change no longer crashes
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index 1d9e03e..0bfbbf1 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -123,7 +123,13 @@
// Make __get_thread() inlined for performance reason. See http://b/19825434.
static inline __always_inline pthread_internal_t* __get_thread() {
- return reinterpret_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]);
+ void** tls = __get_tls();
+ if (__predict_true(tls)) {
+ return reinterpret_cast<pthread_internal_t*>(tls[TLS_SLOT_THREAD_ID]);
+ }
+
+ // This happens when called during libc initialization before TLS has been initialized.
+ return nullptr;
}
__LIBC_HIDDEN__ void pthread_key_clean_all(void);