[gdb/symtab] Fix gdb.base/fission-macro.exp with unix/-m32
[binutils-gdb.git] / gdb / testsuite / gdb.threads / access-mem-running-thread-exit.c
blobaf05b13c76398e3e826876b17f00192f45c98a82
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2021-2024 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/>. */
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <pthread.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
26 #define THREADS 20
28 static volatile unsigned int global_var = 123;
30 /* Wrapper around pthread_create. */
32 static void
33 create_thread (pthread_t *child,
34 void *(*start_routine) (void *), void *arg)
36 int rc;
38 while ((rc = pthread_create (child, NULL, start_routine, arg)) != 0)
40 fprintf (stderr, "unexpected error from pthread_create: %s (%d)\n",
41 strerror (rc), rc);
42 sleep (1);
46 /* Data passed to threads on creation. This is allocated on the heap
47 and ownership transferred from parent to child. */
49 struct thread_arg
51 /* The thread's parent. */
52 pthread_t parent;
54 /* Whether to call pthread_join on the parent. */
55 int join_parent;
58 /* Entry point for threads. */
60 static void *
61 thread_fn (void *arg)
63 struct thread_arg *p = arg;
65 /* Passing no argument makes the thread exit immediately. */
66 if (p == NULL)
67 return NULL;
69 if (p->join_parent)
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];
77 int i;
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. */
90 pthread_t child;
92 p->parent = pthread_self ();
93 p->join_parent = 1;
94 create_thread (&child, thread_fn, p);
97 return NULL;
101 main (void)
103 int i;
105 global_var++;
107 for (i = 0; i < 4; i++)
109 struct thread_arg *p;
110 pthread_t child;
112 p = malloc (sizeof *p);
113 p->parent = pthread_self ();
114 /* Only join the parent once. */
115 if (i == 0)
116 p->join_parent = 1;
117 else
118 p->join_parent = 0;
119 create_thread (&child, thread_fn, p);
122 /* Exit the leader to make sure that we can access memory with the
123 leader gone. */
124 pthread_exit (NULL);