Make getpid work before the main thread is initialized.
Bug: http://b/29622562
Test: code dependent on this change no longer crashes
Change-Id: I40936f7b35d9e58182aeb2e34e52f54088700825
diff --git a/libc/bionic/getpid.cpp b/libc/bionic/getpid.cpp
index a3d5b35..779b147 100644
--- a/libc/bionic/getpid.cpp
+++ b/libc/bionic/getpid.cpp
@@ -35,10 +35,12 @@
pid_t getpid() {
pthread_internal_t* self = __get_thread();
- // Do we have a valid cached pid?
- pid_t cached_pid;
- if (__predict_true(self->get_cached_pid(&cached_pid))) {
- return cached_pid;
+ if (__predict_true(self)) {
+ // Do we have a valid cached pid?
+ pid_t cached_pid;
+ if (__predict_true(self->get_cached_pid(&cached_pid))) {
+ return cached_pid;
+ }
}
// We're still in the dynamic linker or we're in the middle of forking, so ask the kernel.