1 /* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2021-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 #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> 25 26 #define THREADS 20 27 28 static volatile unsigned int global_var = 123; 29 30 /* Wrapper around pthread_create. */ 31 32 static void 33 create_thread (pthread_t *child, 34 void *(*start_routine) (void *), void *arg) 35 { 36 int rc; 37 38 while ((rc = pthread_create (child, NULL, start_routine, arg)) != 0) 39 { 40 fprintf (stderr, "unexpected error from pthread_create: %s (%d)\n", 41 strerror (rc), rc); 42 sleep (1); 43 } 44 } 45 46 /* Data passed to threads on creation. This is allocated on the heap 47 and ownership transferred from parent to child. */ 48 49 struct thread_arg 50 { 51 /* The thread's parent. */ 52 pthread_t parent; 53 54 /* Whether to call pthread_join on the parent. */ 55 int join_parent; 56 }; 57 58 /* Entry point for threads. */ 59 60 static void * 61 thread_fn (void *arg) 62 { 63 struct thread_arg *p = arg; 64 65 /* Passing no argument makes the thread exit immediately. */ 66 if (p == NULL) 67 return NULL; 68 69 if (p->join_parent) 70 assert (pthread_join (p->parent, NULL) == 0); 71 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. */ 75 { 76 pthread_t child[THREADS]; 77 int i; 78 79 /* Passing no argument makes the thread exit immediately. */ 80 for (i = 0; i < THREADS; i++) 81 create_thread (&child[i], thread_fn, NULL); 82 83 for (i = 0; i < THREADS; i++) 84 pthread_join (child[i], NULL); 85 } 86 87 /* Spawn a new thread that joins us, and exit. The idea here is to 88 not have any thread that stays around forever. */ 89 { 90 pthread_t child; 91 92 p->parent = pthread_self (); 93 p->join_parent = 1; 94 create_thread (&child, thread_fn, p); 95 } 96 97 return NULL; 98 } 99 100 int 101 main (void) 102 { 103 int i; 104 105 for (i = 0; i < 4; i++) 106 { 107 struct thread_arg *p; 108 pthread_t child; 109 110 p = malloc (sizeof *p); 111 p->parent = pthread_self (); 112 /* Only join the parent once. */ 113 if (i == 0) 114 p->join_parent = 1; 115 else 116 p->join_parent = 0; 117 create_thread (&child, thread_fn, p); 118 } 119 120 /* Exit the leader to make sure that we can access memory with the 121 leader gone. */ 122 pthread_exit (NULL); 123 } 124