Remove obsolete test from gdb.cp/var-tag.exp
[binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.c
blob74d37cf3e7d29ec84abe29ea9af0ccdafa6e78cd
1 /* Pthreads test program.
2 Copyright 1996-2024 Free Software Foundation, Inc.
4 Written by Fred Fish of Cygnus Support
5 Contributed by Cygnus Support
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
29 static int verbose = 0;
31 static void
32 common_routine (int arg)
34 static int from_thread1;
35 static int from_thread2;
36 static int from_main;
37 static int hits;
38 static int full_coverage;
40 if (verbose)
41 printf ("common_routine (%d)\n", arg);
42 hits++;
43 switch (arg)
45 case 0:
46 from_main++;
47 break;
48 case 1:
49 from_thread1++;
50 break;
51 case 2:
52 from_thread2++;
53 break;
55 if (from_main && from_thread1 && from_thread2)
56 full_coverage = 1;
59 static void *
60 thread1 (void *arg)
62 int i;
63 int z = 0;
65 if (verbose)
66 printf ("thread1 (%0lx) ; pid = %d\n", (long) arg, getpid ());
67 for (i = 1; i <= 10000000; i++)
69 if (verbose)
70 printf ("thread1 %ld\n", (long) pthread_self ());
71 z += i;
72 common_routine (1);
73 sleep (1);
75 return (void *) 0;
78 static void *
79 thread2 (void * arg)
81 int i;
82 int k = 0;
84 if (verbose)
85 printf ("thread2 (%0lx) ; pid = %d\n", (long) arg, getpid ());
86 for (i = 1; i <= 10000000; i++)
88 if (verbose)
89 printf ("thread2 %ld\n", (long) pthread_self ());
90 k += i;
91 common_routine (2);
92 sleep (1);
94 sleep (100);
95 return (void *) 0;
98 void
99 foo (int a, int b, int c)
101 int d, e, f;
103 if (verbose)
104 printf ("a=%d\n", a);
107 /* Similar to perror, but use ERR instead of errno. */
109 static void
110 print_error (const char *ctx, int err)
112 fprintf (stderr, "%s: %s (%d)\n", ctx, strerror (err), err);
116 main (int argc, char **argv)
118 pthread_t tid1, tid2;
119 int j;
120 int t = 0;
121 void (*xxx) ();
122 pthread_attr_t attr;
123 int res;
125 if (verbose)
126 printf ("pid = %d\n", getpid ());
128 foo (1, 2, 3);
130 res = pthread_attr_init (&attr);
131 if (res != 0)
133 print_error ("pthread_attr_init 1", res);
134 exit (1);
137 #ifdef PTHREAD_SCOPE_SYSTEM
138 res = pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
139 if (res != 0 && res != ENOTSUP)
141 print_error ("pthread_attr_setscope 1", res);
142 exit (1);
144 #endif
146 res = pthread_create (&tid1, &attr, thread1, (void *) 0xfeedface);
147 if (res != 0)
149 print_error ("pthread_create 1", res);
150 exit (1);
152 if (verbose)
153 printf ("Made thread %ld\n", (long) tid1);
154 sleep (1);
156 res = pthread_create (&tid2, NULL, thread2, (void *) 0xdeadbeef);
157 if (res != 0)
159 print_error ("pthread_create 2", res);
160 exit (1);
162 if (verbose)
163 printf ("Made thread %ld\n", (long) tid2);
165 sleep (1);
167 for (j = 1; j <= 10000000; j++)
169 if (verbose)
170 printf ("top %ld\n", (long) pthread_self ());
171 common_routine (0);
172 sleep (1);
173 t += j;
176 exit (0);