// 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);
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;
// 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);
}
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;
}
#include <stdatomic.h>
#include "private/bionic_futex.h"
+// Lock is used in places like pthread_rwlock_t, which can be initialized without calling
+// an initialization function. So make sure Lock can be initialized by setting its memory to 0.
class Lock {
private:
enum LockState {
bool process_shared;
public:
- Lock(bool process_shared = false) {
- init(process_shared);
- }
-
void init(bool process_shared) {
atomic_init(&state, Unlocked);
this->process_shared = process_shared;