1 /* Test malloc with concurrent thread termination.
2 Copyright (C) 2015-2016 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 thread spawns a number of outer threads, equal to the arena
20 limit. The outer threads run a loop which start and join two
21 different kinds of threads: the first kind allocates (attaching an
22 arena to the thread; malloc_first_thread) and waits, the second
23 kind waits and allocates (wait_first_threads). Both kinds of
24 threads exit immediately after waiting. The hope is that this will
25 exhibit races in thread termination and arena management,
26 particularly related to the arena free list. */
36 static int do_test (void);
38 #define TEST_FUNCTION do_test ()
39 #include "../test-skeleton.c"
41 static bool termination_requested
;
42 static int inner_thread_count
= 4;
43 static size_t malloc_size
= 32;
46 __attribute__ ((noinline
, noclone
))
47 unoptimized_free (void *ptr
)
53 malloc_first_thread (void * closure
)
55 pthread_barrier_t
*barrier
= closure
;
56 void *ptr
= malloc (malloc_size
);
59 printf ("error: malloc: %m\n");
62 int ret
= pthread_barrier_wait (barrier
);
63 if (ret
!= 0 && ret
!= PTHREAD_BARRIER_SERIAL_THREAD
)
66 printf ("error: pthread_barrier_wait: %m\n");
69 unoptimized_free (ptr
);
74 wait_first_thread (void * closure
)
76 pthread_barrier_t
*barrier
= closure
;
77 int ret
= pthread_barrier_wait (barrier
);
78 if (ret
!= 0 && ret
!= PTHREAD_BARRIER_SERIAL_THREAD
)
81 printf ("error: pthread_barrier_wait: %m\n");
84 void *ptr
= malloc (malloc_size
);
87 printf ("error: malloc: %m\n");
90 unoptimized_free (ptr
);
95 outer_thread (void *closure
)
97 pthread_t
*threads
= calloc (sizeof (*threads
), inner_thread_count
);
100 printf ("error: calloc: %m\n");
104 while (!__atomic_load_n (&termination_requested
, __ATOMIC_RELAXED
))
106 pthread_barrier_t barrier
;
107 int ret
= pthread_barrier_init (&barrier
, NULL
, inner_thread_count
+ 1);
111 printf ("pthread_barrier_init: %m\n");
114 for (int i
= 0; i
< inner_thread_count
; ++i
)
116 void *(*func
) (void *);
118 func
= malloc_first_thread
;
120 func
= wait_first_thread
;
121 ret
= pthread_create (threads
+ i
, NULL
, func
, &barrier
);
125 printf ("error: pthread_create: %m\n");
129 ret
= pthread_barrier_wait (&barrier
);
130 if (ret
!= 0 && ret
!= PTHREAD_BARRIER_SERIAL_THREAD
)
133 printf ("pthread_wait: %m\n");
136 for (int i
= 0; i
< inner_thread_count
; ++i
)
138 ret
= pthread_join (threads
[i
], NULL
);
142 printf ("error: pthread_join: %m\n");
146 ret
= pthread_barrier_destroy (&barrier
);
150 printf ("pthread_barrier_destroy: %m\n");
163 /* The number of threads should be smaller than the number of
164 arenas, so that there will be some free arenas to add to the
166 enum { outer_thread_count
= 2 };
167 if (mallopt (M_ARENA_MAX
, 8) == 0)
169 printf ("error: mallopt (M_ARENA_MAX) failed\n");
173 /* Leave some room for shutting down all threads gracefully. */
175 if (timeout
> TIMEOUT
)
176 timeout
= TIMEOUT
- 1;
178 pthread_t
*threads
= calloc (sizeof (*threads
), outer_thread_count
);
181 printf ("error: calloc: %m\n");
185 for (long i
= 0; i
< outer_thread_count
; ++i
)
187 int ret
= pthread_create (threads
+ i
, NULL
, outer_thread
, NULL
);
191 printf ("error: pthread_create: %m\n");
196 struct timespec ts
= {timeout
, 0};
197 if (nanosleep (&ts
, NULL
))
199 printf ("error: error: nanosleep: %m\n");
203 __atomic_store_n (&termination_requested
, true, __ATOMIC_RELAXED
);
205 for (long i
= 0; i
< outer_thread_count
; ++i
)
207 int ret
= pthread_join (threads
[i
], NULL
);
211 printf ("error: pthread_join: %m\n");