1 /* Support file for atexit/exit, etc. race tests (BZ #27749).
2 Copyright (C) 2021-2023 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 /* The atexit/exit, at_quick_exit/quick_exit, __cxa_atexit/exit, etc.
20 exhibited data race while calling destructors.
22 This test registers destructors from the background thread, and checks that
23 the same destructor is not called more than once. */
25 #include <stdatomic.h>
28 #include <support/check.h>
29 #include <support/xthread.h>
30 #include <support/xunistd.h>
34 static atomic_int registered
;
35 static atomic_int todo
= 100000;
40 atomic_fetch_sub (®istered
, 1);
43 FAIL_EXIT1 ("%s: %p\n", __func__
, arg
);
46 while (atomic_load (&todo
) > 0 && atomic_load (®istered
) < 100)
50 int __cxa_atexit (void (*func
) (void *), void *arg
, void *d
);
53 thread_func (void *arg
)
56 while (atomic_load (&todo
) > 0)
58 if (atomic_load (®istered
) < 10000)
61 for (int i
= 0; i
< n
; ++i
)
62 __cxa_atexit (&atexit_cb
, ++cb_arg
, 0);
63 atomic_fetch_add (®istered
, n
);
64 atomic_fetch_sub (&todo
, n
);
76 xpthread_attr_init (&attr
);
77 xpthread_attr_setdetachstate (&attr
, 1);
79 xpthread_create (&attr
, thread_func
, NULL
);
80 xpthread_attr_destroy (&attr
);
82 while (atomic_load (®istered
) == 0)
90 for (int i
= 0; i
< 20; ++i
)
92 for (int i
= 0; i
< 10; ++i
)
96 for (int i
= 0; i
< 10; ++i
)
99 xwaitpid (0, &status
, 0);
100 if (!WIFEXITED (status
))
101 FAIL_EXIT1 ("Failed iterations %d", i
);
102 TEST_COMPARE (WEXITSTATUS (status
), 0);
109 #define TEST_FUNCTION do_test
110 #include <support/test-driver.c>