support: Add xstrndup, xunlink, xreadlink, support_create_temp_directory
[glibc.git] / stdlib / test-dlclose-exit-race.c
blob9ec294e16883912929baae96390e7150b51fac42
1 /* Test for exit/dlclose race (Bug 22180).
2 Copyright (C) 2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* This file must be run from within a directory called "stdlib". */
21 /* This test verifies that when dlopen in one thread races against exit
22 in another thread, we don't call registered destructor twice.
24 Expected result:
25 second
26 first
27 ... clean termination
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <semaphore.h>
33 #include <support/check.h>
34 #include <support/xdlfcn.h>
35 #include <support/xthread.h>
37 /* Semaphore to ensure we have a happens-before between the first function
38 starting and exit being called. */
39 sem_t order1;
41 /* Semaphore to ensure we have a happens-before between the second function
42 starting and the first function returning. */
43 sem_t order2;
45 void *
46 exit_thread (void *arg)
48 /* Wait for the dlclose to start... */
49 sem_wait (&order1);
50 /* Then try to run the exit sequence which should call all
51 __cxa_atexit registered functions and in parallel with
52 the executing dlclose(). */
53 exit (0);
57 void
58 last (void)
60 /* Let dlclose thread proceed. */
61 sem_post (&order2);
64 int
65 main (void)
67 void *dso;
68 pthread_t thread;
70 atexit (last);
72 dso = xdlopen ("$ORIGIN/test-dlclose-exit-race-helper.so",
73 RTLD_NOW|RTLD_GLOBAL);
74 thread = xpthread_create (NULL, exit_thread, NULL);
76 xdlclose (dso);
77 xpthread_join (thread);
79 FAIL_EXIT1 ("Did not terminate via exit(0) in exit_thread() as expected.");