am afb488a3: am 6fac2f68: Merge "Fix possible leak in pthread_detach."

* commit 'afb488a3e91c7ac297b4f91e9985fd10c09d6f81':
  Fix possible leak in pthread_detach.
This commit is contained in:
Yabin Cui 2015-01-15 19:47:35 +00:00 committed by Android Git Automerger
commit b8679a84df
4 changed files with 29 additions and 22 deletions

View File

@ -27,29 +27,32 @@
*/ */
#include <errno.h> #include <errno.h>
#include <pthread.h>
#include "pthread_accessor.h" #include "pthread_accessor.h"
int pthread_detach(pthread_t t) { int pthread_detach(pthread_t t) {
pthread_accessor thread(t); {
if (thread.get() == NULL) { pthread_accessor thread(t);
if (thread.get() == NULL) {
return ESRCH; return ESRCH;
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
return EINVAL; // Already detached.
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) {
return 0; // Already being joined; silently do nothing, like glibc.
}
// If the thread has not exited, we can detach it safely.
if ((thread->attr.flags & PTHREAD_ATTR_FLAG_ZOMBIE) == 0) {
thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
return 0;
}
} }
if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) { // The thread is in zombie state, use pthread_join to clean it up.
return EINVAL; // Already detached. return pthread_join(t, NULL);
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) {
return 0; // Already being joined; silently do nothing, like glibc.
}
if (thread->tid == 0) {
// Already exited; clean up.
_pthread_internal_remove_locked(thread.get(), true);
return 0;
}
thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
return 0;
} }

View File

@ -99,6 +99,9 @@ void pthread_exit(void* return_value) {
// pthread_internal_t is freed below with stack, not here. // pthread_internal_t is freed below with stack, not here.
_pthread_internal_remove_locked(thread, false); _pthread_internal_remove_locked(thread, false);
free_mapped_space = true; free_mapped_space = true;
} else {
// Mark the thread as exiting without freeing pthread_internal_t.
thread->attr.flags |= PTHREAD_ATTR_FLAG_ZOMBIE;
} }
pthread_mutex_unlock(&g_thread_list_lock); pthread_mutex_unlock(&g_thread_list_lock);

View File

@ -38,6 +38,9 @@
/* Has the thread been joined by another thread? */ /* Has the thread been joined by another thread? */
#define PTHREAD_ATTR_FLAG_JOINED 0x00000002 #define PTHREAD_ATTR_FLAG_JOINED 0x00000002
/* Did the thread exit without freeing pthread_internal_t? */
#define PTHREAD_ATTR_FLAG_ZOMBIE 0x00000004
/* Is this the main thread? */ /* Is this the main thread? */
#define PTHREAD_ATTR_FLAG_MAIN_THREAD 0x80000000 #define PTHREAD_ATTR_FLAG_MAIN_THREAD 0x80000000

View File

@ -431,7 +431,7 @@ TEST(pthread, pthread_detach__no_such_thread) {
ASSERT_EQ(ESRCH, pthread_detach(dead_thread)); ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
} }
TEST(pthread, pthread_detach__leak) { TEST(pthread, pthread_detach_no_leak) {
size_t initial_bytes = 0; size_t initial_bytes = 0;
// Run this loop more than once since the first loop causes some memory // Run this loop more than once since the first loop causes some memory
// to be allocated permenantly. Run an extra loop to help catch any subtle // to be allocated permenantly. Run an extra loop to help catch any subtle
@ -464,9 +464,7 @@ TEST(pthread, pthread_detach__leak) {
size_t final_bytes = mallinfo().uordblks; size_t final_bytes = mallinfo().uordblks;
int leaked_bytes = (final_bytes - initial_bytes); int leaked_bytes = (final_bytes - initial_bytes);
// User code (like this test) doesn't know how large pthread_internal_t is. ASSERT_EQ(0, leaked_bytes);
// We can be pretty sure it's more than 128 bytes.
ASSERT_LT(leaked_bytes, 32 /*threads*/ * 128 /*bytes*/);
} }
TEST(pthread, pthread_getcpuclockid__clock_gettime) { TEST(pthread, pthread_getcpuclockid__clock_gettime) {