From 118d5da898d27cbd6d927db7f2f256ee010a4462 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 24 May 2024 14:47:40 -0400 Subject: [PATCH] pthread_exit(): reduce duplication. Strictly, this is more of "swap one form of duplication for another", but I found the existing code non-obvious in part because people have added new code under existing comments (which don't apply), in two places. At this point, duplicating the _condition_ (which is much less likely to change at all, let alone grow more complex) clarifies the code and makes the comments match the code they're adjacent to again. Test: treehugger Change-Id: Ic8f01dc5b4fd14e942bf8dd7c72cab7df06d99d5 --- libc/bionic/pthread_exit.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/libc/bionic/pthread_exit.cpp b/libc/bionic/pthread_exit.cpp index f584b2722..0181abac9 100644 --- a/libc/bionic/pthread_exit.cpp +++ b/libc/bionic/pthread_exit.cpp @@ -133,20 +133,17 @@ void pthread_exit(void* return_value) { // pthread_internal_t is freed below with stack, not here. __pthread_internal_remove(thread); - - if (thread->mmap_size != 0) { - // We need to free mapped space for detached threads when they exit. - // That's not something we can do in C. - __notify_thread_exit_callbacks(); - __hwasan_thread_exit(); - _exit_with_stack_teardown(thread->mmap_base, thread->mmap_size); - } } - // No need to free mapped space. Either there was no space mapped, or it is left for - // the pthread_join caller to clean up. __notify_thread_exit_callbacks(); __hwasan_thread_exit(); + if (old_state == THREAD_DETACHED && thread->mmap_size != 0) { + // We need to free mapped space for detached threads when they exit. + // That's not something we can do in C. + _exit_with_stack_teardown(thread->mmap_base, thread->mmap_size); + } + // No need to free mapped space. Either there was no space mapped, + // or it is left for the pthread_join caller to clean up. __exit(0); }