1 /* Test concurrent dlopen and pthread_create: BZ 19329.
2    Copyright (C) 2021-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 #include <dlfcn.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdatomic.h>
23 #include <support/xdlfcn.h>
24 #include <support/xthread.h>
25 
26 #define THREADS 10000
27 
28 static atomic_int done;
29 
30 static void *
start(void * a)31 start (void *a)
32 {
33   /* Load a module with many dependencies that each have TLS.  */
34   xdlopen ("tst-tls21mod.so", RTLD_LAZY);
35   atomic_store_explicit (&done, 1, memory_order_release);
36   return 0;
37 }
38 
39 static void *
nop(void * a)40 nop (void *a)
41 {
42   return 0;
43 }
44 
45 static int
do_test(void)46 do_test (void)
47 {
48   pthread_t t1, t2;
49   int i;
50 
51   /* Load a module with lots of dependencies and TLS.  */
52   t1 = xpthread_create (0, start, 0);
53 
54   /* Concurrently create lots of threads until dlopen is observably done.  */
55   for (i = 0; i < THREADS; i++)
56     {
57       if (atomic_load_explicit (&done, memory_order_acquire) != 0)
58 	break;
59       t2 = xpthread_create (0, nop, 0);
60       xpthread_join (t2);
61     }
62 
63   xpthread_join (t1);
64   printf ("threads created during dlopen: %d\n", i);
65   return 0;
66 }
67 
68 #include <support/test-driver.c>
69