Use bionic lock in pthread_internal_t.
It removes calling to pthread_mutex_lock() at the beginning of new
thread, which helps to support thread sanitizer.
Change-Id: Ia3601c476de7976a9177b792bd74bb200cee0e13
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index e260e97..ce43009 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -193,8 +193,7 @@
// notify gdb about this thread before we start doing anything.
// This also provides the memory barrier needed to ensure that all memory
// accesses previously made by the creating thread are visible to us.
- pthread_mutex_lock(&thread->startup_handshake_mutex);
- pthread_mutex_destroy(&thread->startup_handshake_mutex);
+ thread->startup_handshake_lock.lock();
__init_alternate_signal_stack(thread);
@@ -233,14 +232,14 @@
return result;
}
- // Create a mutex for the thread in TLS to wait on once it starts so we can keep
+ // Create a lock for the thread to wait on once it starts so we can keep
// it from doing anything until after we notify the debugger about it
//
// This also provides the memory barrier we need to ensure that all
// memory accesses previously performed by this thread are visible to
// the new thread.
- pthread_mutex_init(&thread->startup_handshake_mutex, NULL);
- pthread_mutex_lock(&thread->startup_handshake_mutex);
+ thread->startup_handshake_lock.init(false);
+ thread->startup_handshake_lock.lock();
thread->start_routine = start_routine;
thread->start_routine_arg = arg;
@@ -263,7 +262,7 @@
// We don't have to unlock the mutex at all because clone(2) failed so there's no child waiting to
// be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a
// reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker.
- pthread_mutex_unlock(&thread->startup_handshake_mutex);
+ thread->startup_handshake_lock.unlock();
if (thread->mmap_size != 0) {
munmap(thread->attr.stack_base, thread->mmap_size);
}
@@ -278,13 +277,13 @@
atomic_store(&thread->join_state, THREAD_DETACHED);
__pthread_internal_add(thread);
thread->start_routine = __do_nothing;
- pthread_mutex_unlock(&thread->startup_handshake_mutex);
+ thread->startup_handshake_lock.unlock();
return init_errno;
}
// Publish the pthread_t and unlock the mutex to let the new thread start running.
*thread_out = __pthread_internal_add(thread);
- pthread_mutex_unlock(&thread->startup_handshake_mutex);
+ thread->startup_handshake_lock.unlock();
return 0;
}