[gdb/symtab] Fix gdb.base/fission-macro.exp with unix/-m32
[binutils-gdb.git] / gdb / testsuite / gdb.threads / fork-thread-pending.c
bloba9b557aba490ce89c9be81a766d7cddf5490c01c
1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2008-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 #include <pthread.h>
19 #include <assert.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
27 #define NUMTHREADS 10
29 volatile int done = 0;
30 static pthread_barrier_t barrier;
32 static void *
33 start (void *arg)
35 while (!done)
36 usleep (100);
37 assert (0);
38 return arg;
41 void *
42 thread_function (void *arg)
44 int x = * (int *) arg;
46 printf ("Thread <%d> executing\n", x);
48 pthread_barrier_wait (&barrier);
50 while (!done)
51 usleep (100);
53 return NULL;
56 void *
57 thread_forker (void *arg)
59 int x = * (int *) arg;
60 pid_t pid;
61 int rv;
62 int i;
63 pthread_t thread;
65 printf ("Thread forker <%d> executing\n", x);
67 pthread_barrier_wait (&barrier);
69 switch ((pid = fork ()))
71 case -1:
72 assert (0);
73 default:
74 wait (&rv);
75 done = 1;
76 break;
77 case 0:
78 i = pthread_create (&thread, NULL, start, NULL);
79 assert (i == 0);
80 i = pthread_join (thread, NULL);
81 assert (i == 0);
83 assert (0);
86 return NULL;
89 int
90 main (void)
92 pthread_t threads[NUMTHREADS];
93 int args[NUMTHREADS];
94 int i, j;
96 alarm (600);
98 i = pthread_barrier_init (&barrier, NULL, NUMTHREADS);
99 assert (i == 0);
101 /* Create a few threads that do mostly nothing, and then one that
102 forks. */
103 for (j = 0; j < NUMTHREADS - 1; ++j)
105 args[j] = j;
106 pthread_create (&threads[j], NULL, thread_function, &args[j]);
109 args[j] = j;
110 pthread_create (&threads[j], NULL, thread_forker, &args[j]);
112 for (j = 0; j < NUMTHREADS; ++j)
114 pthread_join (threads[j], NULL);
117 return 0;