]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/fork-thread-pending.c
This commit was manufactured by cvs2svn to create branch 'gdb_7_0-branch'.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / fork-thread-pending.c
CommitLineData
e58b0e63
PA
1/* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2008, 2009 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 <assert.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27
28#define NUMTHREADS 10
29
30volatile int done = 0;
31
32static void *
33start (void *arg)
34{
35 while (!done)
36 usleep (100);
37 assert (0);
38 return arg;
39}
40
41void *
42thread_function (void *arg)
43{
44 int x = * (int *) arg;
45
46 printf ("Thread <%d> executing\n", x);
47
48 while (!done)
49 usleep (100);
50
51 return NULL;
52}
53
54void *
55thread_forker (void *arg)
56{
57 int x = * (int *) arg;
58 pid_t pid;
59 int rv;
60 int i;
61 pthread_t thread;
62
63 printf ("Thread forker <%d> executing\n", x);
64
65 switch ((pid = fork ()))
66 {
67 case -1:
68 assert (0);
69 default:
70 wait (&rv);
71 done = 1;
72 break;
73 case 0:
74 i = pthread_create (&thread, NULL, start, NULL);
75 assert (i == 0);
76 i = pthread_join (thread, NULL);
77 assert (i == 0);
78
79 assert (0);
80 }
81
82 return NULL;
83}
84
85int
86main (void)
87{
88 pthread_t threads[NUMTHREADS];
89 int args[NUMTHREADS];
90 int i, j;
91
92 /* Create a few threads that do mostly nothing, and then one that
93 forks. */
94 for (j = 0; j < NUMTHREADS - 1; ++j)
95 {
96 args[j] = j;
97 pthread_create (&threads[j], NULL, thread_function, &args[j]);
98 }
99
100 args[j] = j;
101 pthread_create (&threads[j], NULL, thread_forker, &args[j]);
102
103 for (j = 0; j < NUMTHREADS; ++j)
104 {
105 pthread_join (threads[j], NULL);
106 }
107
108 return 0;
109}