1 /* Test for exit/dlclose race (Bug 22180).
2 Copyright (C) 2017-2022 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 <https://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.
33 #include <semaphore.h>
34 #include <support/check.h>
35 #include <support/xdlfcn.h>
36 #include <support/xthread.h>
38 /* Semaphore to ensure we have a happens-before between the first function
39 starting and exit being called. */
42 /* Semaphore to ensure we have a happens-before between the second function
43 starting and the first function returning. */
47 exit_thread (void *arg
)
49 /* Wait for the dlclose to start... */
51 /* Then try to run the exit sequence which should call all
52 __cxa_atexit registered functions and in parallel with
53 the executing dlclose(). */
61 /* Let dlclose thread proceed. */
74 dso
= xdlopen ("$ORIGIN/test-dlclose-exit-race-helper.so",
75 RTLD_NOW
|RTLD_GLOBAL
);
76 if ((value
= pthread_create (&thread
, NULL
, exit_thread
, NULL
)) != 0)
78 /* If pthread_create fails, then exit() is called in the main
79 thread instead of a second thread, so the semaphore post that
80 would have happened in 'last' gets blocked behind the call to
81 first() - which is waiting on the semaphore, and thus
85 FAIL_EXIT1 ("pthread_create: %m");
89 xpthread_join (thread
);
91 FAIL_EXIT1 ("Did not terminate via exit(0) in exit_thread() as expected.");