]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/pthreads.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.c
1 /* Pthreads test program.
2 Copyright 1996, 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
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
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27
28 /* Under OSF 2.0 & 3.0 and HPUX 10, the second arg of pthread_create
29 is prototyped to be just a "pthread_attr_t", while under Solaris it
30 is a "pthread_attr_t *". Arg! */
31
32 #if defined (__osf__) || defined (__hpux__)
33 #define PTHREAD_CREATE_ARG2(arg) arg
34 #define PTHREAD_CREATE_NULL_ARG2 null_attr
35 static pthread_attr_t null_attr;
36 #else
37 #define PTHREAD_CREATE_ARG2(arg) &arg
38 #define PTHREAD_CREATE_NULL_ARG2 NULL
39 #endif
40
41 static int verbose = 0;
42
43 static void
44 common_routine (arg)
45 int arg;
46 {
47 static int from_thread1;
48 static int from_thread2;
49 static int from_main;
50 static int hits;
51 static int full_coverage;
52
53 if (verbose) printf("common_routine (%d)\n", arg);
54 hits++;
55 switch (arg)
56 {
57 case 0:
58 from_main++;
59 break;
60 case 1:
61 from_thread1++;
62 break;
63 case 2:
64 from_thread2++;
65 break;
66 }
67 if (from_main && from_thread1 && from_thread2)
68 full_coverage = 1;
69 }
70
71 static void *
72 thread1 (void *arg)
73 {
74 int i;
75 int z = 0;
76
77 if (verbose) printf ("thread1 (%0x) ; pid = %d\n", arg, getpid ());
78 for (i=1; i <= 10000000; i++)
79 {
80 if (verbose) printf("thread1 %d\n", pthread_self ());
81 z += i;
82 common_routine (1);
83 sleep(1);
84 }
85 return (void *) 0;
86 }
87
88 static void *
89 thread2 (void * arg)
90 {
91 int i;
92 int k = 0;
93
94 if (verbose) printf ("thread2 (%0x) ; pid = %d\n", arg, getpid ());
95 for (i=1; i <= 10000000; i++)
96 {
97 if (verbose) printf("thread2 %d\n", pthread_self ());
98 k += i;
99 common_routine (2);
100 sleep(1);
101 }
102 sleep(100);
103 return (void *) 0;
104 }
105
106 void
107 foo (a, b, c)
108 int a, b, c;
109 {
110 int d, e, f;
111
112 if (verbose) printf("a=%d\n", a);
113 }
114
115 main(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
129 #ifndef __osf__
130 if (pthread_attr_init (&attr))
131 {
132 perror ("pthread_attr_init 1");
133 exit (1);
134 }
135 #endif
136
137 #ifdef PTHREAD_SCOPE_SYSTEM
138 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
139 {
140 perror ("pthread_attr_setscope 1");
141 exit (1);
142 }
143 #endif
144
145 if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface))
146 {
147 perror ("pthread_create 1");
148 exit (1);
149 }
150 if (verbose) printf ("Made thread %d\n", tid1);
151 sleep (1);
152
153 if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef))
154 {
155 perror ("pthread_create 2");
156 exit (1);
157 }
158 if (verbose) printf("Made thread %d\n", tid2);
159
160 sleep (1);
161
162 for (j = 1; j <= 10000000; j++)
163 {
164 if (verbose) printf("top %d\n", pthread_self ());
165 common_routine (0);
166 sleep(1);
167 t += j;
168 }
169
170 exit(0);
171 }
172