1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/* Check if exit/quick_exit can be called concurrently by multiple threads.
Copyright (C) 2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <array_length.h>
#include <stdlib.h>
#include <support/check.h>
#include <support/xthread.h>
#include <stdio.h>
#include <support/xunistd.h>
#include <string.h>
/* A value larger than the minimum required by C/POSIX (32), to trigger a
new block memory allocation. */
#define MAX_atexit 64
static pthread_barrier_t barrier;
static void *
tf (void *closure)
{
xpthread_barrier_wait (&barrier);
EXIT (0);
return NULL;
}
static const char expected[] = "00000000000000000000000000000000000"
"00000000000000000000003021121130211";
static char crumbs[sizeof (expected)];
static int next_slot = 0;
static void
exit_with_flush (int code)
{
fflush (stdout);
/* glibc allows recursive EXIT, the ATEXIT handlers execution will be
resumed from the where the previous EXIT was interrupted. */
EXIT (code);
}
/* Take some time, so another thread potentially issue EXIT. */
#define SETUP_NANOSLEEP \
if (nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 1000L }, \
NULL) != 0) \
FAIL_EXIT1 ("nanosleep: %m")
static void
fn0 (void)
{
crumbs[next_slot++] = '0';
SETUP_NANOSLEEP;
}
static void
fn1 (void)
{
crumbs[next_slot++] = '1';
SETUP_NANOSLEEP;
}
static void
fn2 (void)
{
crumbs[next_slot++] = '2';
ATEXIT (fn1);
SETUP_NANOSLEEP;
}
static void
fn3 (void)
{
crumbs[next_slot++] = '3';
ATEXIT (fn2);
ATEXIT (fn0);
SETUP_NANOSLEEP;
}
static void
fn_final (void)
{
TEST_COMPARE_STRING (crumbs, expected);
exit_with_flush (0);
}
_Noreturn static void
child (void)
{
enum { nthreads = 8 };
xpthread_barrier_init (&barrier, NULL, nthreads + 1);
pthread_t thr[nthreads];
for (int i = 0; i < nthreads; i++)
thr[i] = xpthread_create (NULL, tf, NULL);
xpthread_barrier_wait (&barrier);
for (int i = 0; i < nthreads; i++)
{
pthread_join (thr[i], NULL);
/* It should not be reached, it means that thread did not exit for
some reason. */
support_record_failure ();
}
EXIT (2);
}
static int
do_test (void)
{
/* Register a large number of handler that will trigger a heap allocation
for the handle state. On EXIT, each block will be freed after the
handle is processed. */
int slots_remaining = MAX_atexit;
/* Register this first so it can verify expected order of the rest. */
ATEXIT (fn_final); --slots_remaining;
TEST_VERIFY_EXIT (ATEXIT (fn1) == 0); --slots_remaining;
TEST_VERIFY_EXIT (ATEXIT (fn3) == 0); --slots_remaining;
TEST_VERIFY_EXIT (ATEXIT (fn1) == 0); --slots_remaining;
TEST_VERIFY_EXIT (ATEXIT (fn2) == 0); --slots_remaining;
TEST_VERIFY_EXIT (ATEXIT (fn1) == 0); --slots_remaining;
TEST_VERIFY_EXIT (ATEXIT (fn3) == 0); --slots_remaining;
while (slots_remaining > 0)
{
TEST_VERIFY_EXIT (ATEXIT (fn0) == 0); --slots_remaining;
}
pid_t pid = xfork ();
if (pid != 0)
{
int status;
xwaitpid (pid, &status, 0);
TEST_VERIFY (WIFEXITED (status));
}
else
child ();
return 0;
}
#include <support/test-driver.c>
|