1 /* Test malloc with concurrent thread termination.
2 Copyright (C) 2015-2017 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 #include <support/support.h>
37 #include <support/xthread.h>
38 #include <support/test-driver.h>
40 static bool termination_requested
;
41 static int inner_thread_count
= 4;
42 static size_t malloc_size
= 32;
45 __attribute__ ((noinline
, noclone
))
46 unoptimized_free (void *ptr
)
52 malloc_first_thread (void * closure
)
54 pthread_barrier_t
*barrier
= closure
;
55 void *ptr
= xmalloc (malloc_size
);
56 xpthread_barrier_wait (barrier
);
57 unoptimized_free (ptr
);
62 wait_first_thread (void * closure
)
64 pthread_barrier_t
*barrier
= closure
;
65 xpthread_barrier_wait (barrier
);
66 void *ptr
= xmalloc (malloc_size
);
67 unoptimized_free (ptr
);
72 outer_thread (void *closure
)
74 pthread_t
*threads
= xcalloc (sizeof (*threads
), inner_thread_count
);
75 while (!__atomic_load_n (&termination_requested
, __ATOMIC_RELAXED
))
77 pthread_barrier_t barrier
;
78 xpthread_barrier_init (&barrier
, NULL
, inner_thread_count
+ 1);
79 for (int i
= 0; i
< inner_thread_count
; ++i
)
81 void *(*func
) (void *);
83 func
= malloc_first_thread
;
85 func
= wait_first_thread
;
86 threads
[i
] = xpthread_create (NULL
, func
, &barrier
);
88 xpthread_barrier_wait (&barrier
);
89 for (int i
= 0; i
< inner_thread_count
; ++i
)
90 xpthread_join (threads
[i
]);
91 xpthread_barrier_destroy (&barrier
);
102 /* The number of threads should be smaller than the number of
103 arenas, so that there will be some free arenas to add to the
105 enum { outer_thread_count
= 2 };
106 if (mallopt (M_ARENA_MAX
, 8) == 0)
108 printf ("error: mallopt (M_ARENA_MAX) failed\n");
112 /* Leave some room for shutting down all threads gracefully. */
114 if (timeout
> DEFAULT_TIMEOUT
)
115 timeout
= DEFAULT_TIMEOUT
- 1;
117 pthread_t
*threads
= xcalloc (sizeof (*threads
), outer_thread_count
);
118 for (long i
= 0; i
< outer_thread_count
; ++i
)
119 threads
[i
] = xpthread_create (NULL
, outer_thread
, NULL
);
121 struct timespec ts
= {timeout
, 0};
122 if (nanosleep (&ts
, NULL
))
124 printf ("error: error: nanosleep: %m\n");
128 __atomic_store_n (&termination_requested
, true, __ATOMIC_RELAXED
);
130 for (long i
= 0; i
< outer_thread_count
; ++i
)
131 xpthread_join (threads
[i
]);
137 #include <support/test-driver.c>