]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/pthreads.c
Update copyright year range in all GDB files.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.c
CommitLineData
c39d7427 1/* Pthreads test program.
b811d2c2 2 Copyright 1996-2020 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
c906108c
SS
27static int verbose = 0;
28
29static void
30common_routine (arg)
31 int arg;
32{
33 static int from_thread1;
34 static int from_thread2;
35 static int from_main;
36 static int hits;
37 static int full_coverage;
38
39 if (verbose) printf("common_routine (%d)\n", arg);
40 hits++;
41 switch (arg)
42 {
43 case 0:
44 from_main++;
45 break;
46 case 1:
47 from_thread1++;
48 break;
49 case 2:
50 from_thread2++;
51 break;
52 }
53 if (from_main && from_thread1 && from_thread2)
54 full_coverage = 1;
55}
56
57static void *
58thread1 (void *arg)
59{
60 int i;
61 int z = 0;
62
4dfd5423 63 if (verbose) printf ("thread1 (%0lx) ; pid = %d\n", (long) arg, getpid ());
c906108c
SS
64 for (i=1; i <= 10000000; i++)
65 {
4dfd5423 66 if (verbose) printf("thread1 %ld\n", (long) pthread_self ());
c906108c
SS
67 z += i;
68 common_routine (1);
69 sleep(1);
70 }
258ad32d 71 return (void *) 0;
c906108c
SS
72}
73
74static void *
75thread2 (void * arg)
76{
77 int i;
78 int k = 0;
79
4dfd5423 80 if (verbose) printf ("thread2 (%0lx) ; pid = %d\n", (long) arg, getpid ());
c906108c
SS
81 for (i=1; i <= 10000000; i++)
82 {
4dfd5423 83 if (verbose) printf("thread2 %ld\n", (long) pthread_self ());
c906108c
SS
84 k += i;
85 common_routine (2);
86 sleep(1);
87 }
88 sleep(100);
258ad32d 89 return (void *) 0;
c906108c
SS
90}
91
258ad32d 92void
c906108c
SS
93foo (a, b, c)
94 int a, b, c;
95{
96 int d, e, f;
97
98 if (verbose) printf("a=%d\n", a);
99}
100
37bc665e 101int
c906108c
SS
102main(argc, argv)
103 int argc;
104 char **argv;
105{
106 pthread_t tid1, tid2;
107 int j;
108 int t = 0;
109 void (*xxx) ();
110 pthread_attr_t attr;
111
112 if (verbose) printf ("pid = %d\n", getpid());
113
114 foo (1, 2, 3);
115
c906108c
SS
116 if (pthread_attr_init (&attr))
117 {
118 perror ("pthread_attr_init 1");
119 exit (1);
120 }
c906108c
SS
121
122#ifdef PTHREAD_SCOPE_SYSTEM
123 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
124 {
125 perror ("pthread_attr_setscope 1");
126 exit (1);
127 }
128#endif
129
3ca22649 130 if (pthread_create (&tid1, &attr, thread1, (void *) 0xfeedface))
c906108c
SS
131 {
132 perror ("pthread_create 1");
133 exit (1);
134 }
4dfd5423 135 if (verbose) printf ("Made thread %ld\n", (long) tid1);
c906108c
SS
136 sleep (1);
137
3ca22649 138 if (pthread_create (&tid2, NULL, thread2, (void *) 0xdeadbeef))
c906108c
SS
139 {
140 perror ("pthread_create 2");
141 exit (1);
142 }
4dfd5423 143 if (verbose) printf("Made thread %ld\n", (long) tid2);
c906108c
SS
144
145 sleep (1);
146
147 for (j = 1; j <= 10000000; j++)
148 {
4dfd5423 149 if (verbose) printf("top %ld\n", (long) pthread_self ());
c906108c
SS
150 common_routine (0);
151 sleep(1);
152 t += j;
153 }
154
155 exit(0);
156}
157