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
This commit is contained in:
Josh Gao 2016-06-24 16:13:33 -07:00
parent 250667aaf6
commit 3e45901439
1 changed files with 6 additions and 4 deletions

View File

@ -35,11 +35,13 @@ extern "C" pid_t __getpid();
pid_t getpid() { pid_t getpid() {
pthread_internal_t* self = __get_thread(); pthread_internal_t* self = __get_thread();
if (__predict_true(self)) {
// Do we have a valid cached pid? // Do we have a valid cached pid?
pid_t cached_pid; pid_t cached_pid;
if (__predict_true(self->get_cached_pid(&cached_pid))) { if (__predict_true(self->get_cached_pid(&cached_pid))) {
return cached_pid; return cached_pid;
} }
}
// We're still in the dynamic linker or we're in the middle of forking, so ask the kernel. // We're still in the dynamic linker or we're in the middle of forking, so ask the kernel.
// We don't know whether it's safe to update the cached value, so don't try. // We don't know whether it's safe to update the cached value, so don't try.