548bcb
commit 5cc338565479a620244c2f8ff35956629c4dbf81
548bcb
Author: Florian Weimer <fweimer@redhat.com>
548bcb
Date:   Fri Dec 10 05:14:24 2021 +0100
548bcb
548bcb
    nptl: Add one more barrier to nptl/tst-create1
548bcb
    
548bcb
    Without the bar_ctor_finish barrier, it was possible that thread2
548bcb
    re-locked user_lock before ctor had a chance to lock it.  ctor then
548bcb
    blocked in its locking operation, xdlopen from the main thread
548bcb
    did not return, and thread2 was stuck waiting in bar_dtor:
548bcb
    
548bcb
    thread 1: started.
548bcb
    thread 2: started.
548bcb
    thread 2: locked user_lock.
548bcb
    constructor started: 0.
548bcb
    thread 1: in ctor: started.
548bcb
    thread 3: started.
548bcb
    thread 3: done.
548bcb
    thread 2: unlocked user_lock.
548bcb
    thread 2: locked user_lock.
548bcb
    
548bcb
    Fixes the test in commit 83b5323261bb72313bffcf37476c1b8f0847c736
548bcb
    ("elf: Avoid deadlock between pthread_create and ctors [BZ #28357]").
548bcb
    
548bcb
    Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
548bcb
548bcb
diff --git a/sysdeps/pthread/tst-create1.c b/sysdeps/pthread/tst-create1.c
548bcb
index 932586c30990d1d4..763ded8d7956f943 100644
548bcb
--- a/sysdeps/pthread/tst-create1.c
548bcb
+++ b/sysdeps/pthread/tst-create1.c
548bcb
@@ -33,6 +33,7 @@ thread 2: lock(user_lock) -> pthread_create
548bcb
 */
548bcb
 
548bcb
 static pthread_barrier_t bar_ctor;
548bcb
+static pthread_barrier_t bar_ctor_finish;
548bcb
 static pthread_barrier_t bar_dtor;
548bcb
 static pthread_mutex_t user_lock = PTHREAD_MUTEX_INITIALIZER;
548bcb
 
548bcb
@@ -46,6 +47,7 @@ ctor (void)
548bcb
   xpthread_mutex_unlock (&user_lock);
548bcb
   dprintf (1, "thread 1: in ctor: unlocked user_lock.\n");
548bcb
   dprintf (1, "thread 1: in ctor: done.\n");
548bcb
+  xpthread_barrier_wait (&bar_ctor_finish);
548bcb
 }
548bcb
 
548bcb
 void
548bcb
@@ -81,6 +83,7 @@ thread2 (void *a)
548bcb
   xpthread_mutex_unlock (&user_lock);
548bcb
   dprintf (1, "thread 2: unlocked user_lock.\n");
548bcb
   xpthread_join (t3);
548bcb
+  xpthread_barrier_wait (&bar_ctor_finish);
548bcb
 
548bcb
   xpthread_mutex_lock (&user_lock);
548bcb
   dprintf (1, "thread 2: locked user_lock.\n");
548bcb
@@ -99,6 +102,7 @@ thread1 (void)
548bcb
 {
548bcb
   dprintf (1, "thread 1: started.\n");
548bcb
   xpthread_barrier_init (&bar_ctor, NULL, 2);
548bcb
+  xpthread_barrier_init (&bar_ctor_finish, NULL, 2);
548bcb
   xpthread_barrier_init (&bar_dtor, NULL, 2);
548bcb
   pthread_t t2 = xpthread_create (0, thread2, 0);
548bcb
   void *p = xdlopen ("tst-create1mod.so", RTLD_NOW | RTLD_GLOBAL);