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