1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2021-2022 Free Software Foundation, Inc.
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.
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.
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/>. */
28 static volatile unsigned int global_var = 123;
30 /* Wrapper around pthread_create. */
33 create_thread (pthread_t *child,
34 void *(*start_routine) (void *), void *arg)
38 while ((rc = pthread_create (child, NULL, start_routine, arg)) != 0)
40 fprintf (stderr, "unexpected error from pthread_create: %s (%d)\n",
46 /* Data passed to threads on creation. This is allocated on the heap
47 and ownership transferred from parent to child. */
51 /* The thread's parent. */
54 /* Whether to call pthread_join on the parent. */
58 /* Entry point for threads. */
63 struct thread_arg *p = arg;
65 /* Passing no argument makes the thread exit immediately. */
70 assert (pthread_join (p->parent, NULL) == 0);
72 /* Spawn a number of threads that exit immediately, and then join
73 them. The idea is to maximize the time window when we mostly
74 have threads exiting. */
76 pthread_t child[THREADS];
79 /* Passing no argument makes the thread exit immediately. */
80 for (i = 0; i < THREADS; i++)
81 create_thread (&child[i], thread_fn, NULL);
83 for (i = 0; i < THREADS; i++)
84 pthread_join (child[i], NULL);
87 /* Spawn a new thread that joins us, and exit. The idea here is to
88 not have any thread that stays around forever. */
92 p->parent = pthread_self ();
94 create_thread (&child, thread_fn, p);
105 for (i = 0; i < 4; i++)
107 struct thread_arg *p;
110 p = malloc (sizeof *p);
111 p->parent = pthread_self ();
112 /* Only join the parent once. */
117 create_thread (&child, thread_fn, p);
120 /* Exit the leader to make sure that we can access memory with the