1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2022-2023 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program 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
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <pthread.h>
19 #include <unistd.h>
20 #include <sys/syscall.h>
21 
22 #define NUM_THREADS 32
23 
24 pthread_barrier_t barrier;
25 
26 static void
27 do_exit (int exitcode)
28 {
29   /* Synchronize all threads up to here so that they all exit at
30      roughly the same time.  */
31   pthread_barrier_wait (&barrier);
32 
33   /* All threads exit with SYS_exit, even the main thread, to avoid
34      exiting with a group-exit syscall, as that syscall changes the
35      exit status of all still-alive threads, thus potentially masking
36      a bug.  */
37   syscall (SYS_exit, exitcode);
38 }
39 
40 static void *
41 start (void *arg)
42 {
43   int thread_return_value = *(int *) arg;
44 
45   do_exit (thread_return_value);
46 }
47 
48 int
49 main(void)
50 {
51   pthread_t threads[NUM_THREADS];
52   int thread_return_val[NUM_THREADS];
53   int i;
54 
55   pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1);
56 
57   for (i = 0; i < NUM_THREADS; ++i)
58     {
59       thread_return_val[i] = i + 2;
60       pthread_create (&threads[i], NULL, start, &thread_return_val[i]);
61     }
62 
63   do_exit (1);
64 }
65