Clean up pthread_internal_t.

Bug: 11755300
Change-Id: Ib509e8c5ec6b23513aa78b5ac5141d7c34ce2dc8
This commit is contained in:
Elliott Hughes
2013-11-19 16:52:24 -08:00
parent 1b2d214b62
commit cef3faec0e
4 changed files with 19 additions and 18 deletions

View File

@@ -80,7 +80,7 @@ void __init_alternate_signal_stack(pthread_internal_t* thread) {
}
}
int _init_thread(pthread_internal_t* thread, bool add_to_thread_list) {
int __init_thread(pthread_internal_t* thread, bool add_to_thread_list) {
int error = 0;
// Set the scheduling policy/priority of the thread.
@@ -148,16 +148,19 @@ static int __pthread_start(void* arg) {
__init_alternate_signal_stack(thread);
if ((thread->internal_flags & PTHREAD_INTERNAL_FLAG_THREAD_INIT_FAILED) != 0) {
pthread_exit(NULL);
}
void* result = thread->start_routine(thread->start_routine_arg);
pthread_exit(result);
return 0;
}
// A dummy start routine for pthread_create failures where we've created a thread but aren't
// going to run user code on it. We swap out the user's start routine for this and take advantage
// of the regular thread teardown to free up resources.
static void* __do_nothing(void*) {
return NULL;
}
int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
void* (*start_routine)(void*), void* arg) {
ErrnoRestorer errno_restorer;
@@ -174,7 +177,6 @@ int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
__libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: couldn't allocate thread");
return EAGAIN;
}
thread->allocated_on_heap = true;
if (attr == NULL) {
pthread_attr_init(&thread->attr);
@@ -238,12 +240,13 @@ int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
return clone_errno;
}
int init_errno = _init_thread(thread, true);
int init_errno = __init_thread(thread, true);
if (init_errno != 0) {
// Mark the thread detached and let its __pthread_start run to completion.
// It'll check this flag and exit immediately, cleaning up its resources.
thread->internal_flags |= PTHREAD_INTERNAL_FLAG_THREAD_INIT_FAILED;
// Mark the thread detached and replace its start_routine with a no-op.
// Letting the thread run is the easiest way to clean up its resources.
thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
thread->start_routine = __do_nothing;
pthread_mutex_unlock(start_mutex);
return init_errno;
}