m68k: suppress -Wframe-address warning
[glibc.git] / malloc / tst-malloc-thread-exit.c
blobfa6ebf98bfc6792dbe95ae48bdf954e618301ec7
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. */
28 #include <errno.h>
29 #include <malloc.h>
30 #include <pthread.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
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;
45 static void
46 __attribute__ ((noinline, noclone))
47 unoptimized_free (void *ptr)
49 free (ptr);
52 static void *
53 malloc_first_thread (void * closure)
55 pthread_barrier_t *barrier = closure;
56 void *ptr = malloc (malloc_size);
57 if (ptr == NULL)
59 printf ("error: malloc: %m\n");
60 abort ();
62 int ret = pthread_barrier_wait (barrier);
63 if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
65 errno = ret;
66 printf ("error: pthread_barrier_wait: %m\n");
67 abort ();
69 unoptimized_free (ptr);
70 return NULL;
73 static void *
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)
80 errno = ret;
81 printf ("error: pthread_barrier_wait: %m\n");
82 abort ();
84 void *ptr = malloc (malloc_size);
85 if (ptr == NULL)
87 printf ("error: malloc: %m\n");
88 abort ();
90 unoptimized_free (ptr);
91 return NULL;
94 static void *
95 outer_thread (void *closure)
97 pthread_t *threads = calloc (sizeof (*threads), inner_thread_count);
98 if (threads == NULL)
100 printf ("error: calloc: %m\n");
101 abort ();
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);
108 if (ret != 0)
110 errno = ret;
111 printf ("pthread_barrier_init: %m\n");
112 abort ();
114 for (int i = 0; i < inner_thread_count; ++i)
116 void *(*func) (void *);
117 if ((i % 2) == 0)
118 func = malloc_first_thread;
119 else
120 func = wait_first_thread;
121 ret = pthread_create (threads + i, NULL, func, &barrier);
122 if (ret != 0)
124 errno = ret;
125 printf ("error: pthread_create: %m\n");
126 abort ();
129 ret = pthread_barrier_wait (&barrier);
130 if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
132 errno = ret;
133 printf ("pthread_wait: %m\n");
134 abort ();
136 for (int i = 0; i < inner_thread_count; ++i)
138 ret = pthread_join (threads[i], NULL);
139 if (ret != 0)
141 ret = errno;
142 printf ("error: pthread_join: %m\n");
143 abort ();
146 ret = pthread_barrier_destroy (&barrier);
147 if (ret != 0)
149 ret = errno;
150 printf ("pthread_barrier_destroy: %m\n");
151 abort ();
155 free (threads);
157 return NULL;
160 static int
161 do_test (void)
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
165 arena free list. */
166 enum { outer_thread_count = 2 };
167 if (mallopt (M_ARENA_MAX, 8) == 0)
169 printf ("error: mallopt (M_ARENA_MAX) failed\n");
170 return 1;
173 /* Leave some room for shutting down all threads gracefully. */
174 int timeout = 3;
175 if (timeout > TIMEOUT)
176 timeout = TIMEOUT - 1;
178 pthread_t *threads = calloc (sizeof (*threads), outer_thread_count);
179 if (threads == NULL)
181 printf ("error: calloc: %m\n");
182 abort ();
185 for (long i = 0; i < outer_thread_count; ++i)
187 int ret = pthread_create (threads + i, NULL, outer_thread, NULL);
188 if (ret != 0)
190 errno = ret;
191 printf ("error: pthread_create: %m\n");
192 abort ();
196 struct timespec ts = {timeout, 0};
197 if (nanosleep (&ts, NULL))
199 printf ("error: error: nanosleep: %m\n");
200 abort ();
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);
208 if (ret != 0)
210 errno = ret;
211 printf ("error: pthread_join: %m\n");
212 abort ();
215 free (threads);
217 return 0;