Target remote mode fork and exec test updates
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.c
CommitLineData
c39d7427 1/* Pthreads test program.
32d0add0 2 Copyright 1996-2015 Free Software Foundation, Inc.
c39d7427
MC
3
4 Written by Fred Fish of Cygnus Support
5 Contributed by Cygnus Support
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c39d7427 12 (at your option) any later version.
a9762ec7 13
c39d7427
MC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
a9762ec7 18
c39d7427 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c39d7427 21
c906108c 22#include <stdio.h>
22227696 23#include <stdlib.h>
c906108c 24#include <pthread.h>
37bc665e 25#include <unistd.h>
c906108c 26
32a8097b 27/* Under HPUX 10, the second arg of pthread_create
c906108c
SS
28 is prototyped to be just a "pthread_attr_t", while under Solaris it
29 is a "pthread_attr_t *". Arg! */
30
32a8097b 31#if defined (__hpux__)
c906108c
SS
32#define PTHREAD_CREATE_ARG2(arg) arg
33#define PTHREAD_CREATE_NULL_ARG2 null_attr
34static pthread_attr_t null_attr;
35#else
36#define PTHREAD_CREATE_ARG2(arg) &arg
37#define PTHREAD_CREATE_NULL_ARG2 NULL
38#endif
39
40static int verbose = 0;
41
42static void
43common_routine (arg)
44 int arg;
45{
46 static int from_thread1;
47 static int from_thread2;
48 static int from_main;
49 static int hits;
50 static int full_coverage;
51
52 if (verbose) printf("common_routine (%d)\n", arg);
53 hits++;
54 switch (arg)
55 {
56 case 0:
57 from_main++;
58 break;
59 case 1:
60 from_thread1++;
61 break;
62 case 2:
63 from_thread2++;
64 break;
65 }
66 if (from_main && from_thread1 && from_thread2)
67 full_coverage = 1;
68}
69
70static void *
71thread1 (void *arg)
72{
73 int i;
74 int z = 0;
75
4dfd5423 76 if (verbose) printf ("thread1 (%0lx) ; pid = %d\n", (long) arg, getpid ());
c906108c
SS
77 for (i=1; i <= 10000000; i++)
78 {
4dfd5423 79 if (verbose) printf("thread1 %ld\n", (long) pthread_self ());
c906108c
SS
80 z += i;
81 common_routine (1);
82 sleep(1);
83 }
258ad32d 84 return (void *) 0;
c906108c
SS
85}
86
87static void *
88thread2 (void * arg)
89{
90 int i;
91 int k = 0;
92
4dfd5423 93 if (verbose) printf ("thread2 (%0lx) ; pid = %d\n", (long) arg, getpid ());
c906108c
SS
94 for (i=1; i <= 10000000; i++)
95 {
4dfd5423 96 if (verbose) printf("thread2 %ld\n", (long) pthread_self ());
c906108c
SS
97 k += i;
98 common_routine (2);
99 sleep(1);
100 }
101 sleep(100);
258ad32d 102 return (void *) 0;
c906108c
SS
103}
104
258ad32d 105void
c906108c
SS
106foo (a, b, c)
107 int a, b, c;
108{
109 int d, e, f;
110
111 if (verbose) printf("a=%d\n", a);
112}
113
37bc665e 114int
c906108c
SS
115main(argc, argv)
116 int argc;
117 char **argv;
118{
119 pthread_t tid1, tid2;
120 int j;
121 int t = 0;
122 void (*xxx) ();
123 pthread_attr_t attr;
124
125 if (verbose) printf ("pid = %d\n", getpid());
126
127 foo (1, 2, 3);
128
c906108c
SS
129 if (pthread_attr_init (&attr))
130 {
131 perror ("pthread_attr_init 1");
132 exit (1);
133 }
c906108c
SS
134
135#ifdef PTHREAD_SCOPE_SYSTEM
136 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
137 {
138 perror ("pthread_attr_setscope 1");
139 exit (1);
140 }
141#endif
142
143 if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface))
144 {
145 perror ("pthread_create 1");
146 exit (1);
147 }
4dfd5423 148 if (verbose) printf ("Made thread %ld\n", (long) tid1);
c906108c
SS
149 sleep (1);
150
151 if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef))
152 {
153 perror ("pthread_create 2");
154 exit (1);
155 }
4dfd5423 156 if (verbose) printf("Made thread %ld\n", (long) tid2);
c906108c
SS
157
158 sleep (1);
159
160 for (j = 1; j <= 10000000; j++)
161 {
4dfd5423 162 if (verbose) printf("top %ld\n", (long) pthread_self ());
c906108c
SS
163 common_routine (0);
164 sleep(1);
165 t += j;
166 }
167
168 exit(0);
169}
170
This page took 1.562517 seconds and 4 git commands to generate.