]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/pthreads.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.c
1 #include <stdio.h>
2
3 #include "config.h"
4
5 #ifndef HAVE_PTHREAD_H
6
7 /* Don't even try to compile. In fact, cause a syntax error that we can
8 look for as a compiler error message and know that we have no pthread
9 support. In that case we can just suppress the test completely. */
10
11 #error "no posix threads support"
12
13 #else
14
15 /* OK. We have the right header. If we try to compile this and fail, then
16 there is something wrong and the user should know about it so the testsuite
17 should issue an ERROR result.. */
18
19 #ifdef __linux__
20 #define _MIT_POSIX_THREADS 1 /* Linux (or at least RedHat 4.0) needs this */
21 #endif
22
23 #include <pthread.h>
24
25 /* Under OSF 2.0 & 3.0 and HPUX 10, the second arg of pthread_create
26 is prototyped to be just a "pthread_attr_t", while under Solaris it
27 is a "pthread_attr_t *". Arg! */
28
29 #if defined (__osf__) || defined (__hpux__)
30 #define PTHREAD_CREATE_ARG2(arg) arg
31 #define PTHREAD_CREATE_NULL_ARG2 null_attr
32 static pthread_attr_t null_attr;
33 #else
34 #define PTHREAD_CREATE_ARG2(arg) &arg
35 #define PTHREAD_CREATE_NULL_ARG2 NULL
36 #endif
37
38 static int verbose = 0;
39
40 static void
41 common_routine (arg)
42 int arg;
43 {
44 static int from_thread1;
45 static int from_thread2;
46 static int from_main;
47 static int hits;
48 static int full_coverage;
49
50 if (verbose) printf("common_routine (%d)\n", arg);
51 hits++;
52 switch (arg)
53 {
54 case 0:
55 from_main++;
56 break;
57 case 1:
58 from_thread1++;
59 break;
60 case 2:
61 from_thread2++;
62 break;
63 }
64 if (from_main && from_thread1 && from_thread2)
65 full_coverage = 1;
66 }
67
68 static void *
69 thread1 (void *arg)
70 {
71 int i;
72 int z = 0;
73
74 if (verbose) printf ("thread1 (%0x) ; pid = %d\n", arg, getpid ());
75 for (i=1; i <= 10000000; i++)
76 {
77 if (verbose) printf("thread1 %d\n", pthread_self ());
78 z += i;
79 common_routine (1);
80 sleep(1);
81 }
82 }
83
84 static void *
85 thread2 (void * arg)
86 {
87 int i;
88 int k = 0;
89
90 if (verbose) printf ("thread2 (%0x) ; pid = %d\n", arg, getpid ());
91 for (i=1; i <= 10000000; i++)
92 {
93 if (verbose) printf("thread2 %d\n", pthread_self ());
94 k += i;
95 common_routine (2);
96 sleep(1);
97 }
98 sleep(100);
99 }
100
101 int
102 foo (a, b, c)
103 int a, b, c;
104 {
105 int d, e, f;
106
107 if (verbose) printf("a=%d\n", a);
108 }
109
110 main(argc, argv)
111 int argc;
112 char **argv;
113 {
114 pthread_t tid1, tid2;
115 int j;
116 int t = 0;
117 void (*xxx) ();
118 pthread_attr_t attr;
119
120 if (verbose) printf ("pid = %d\n", getpid());
121
122 foo (1, 2, 3);
123
124 #ifndef __osf__
125 if (pthread_attr_init (&attr))
126 {
127 perror ("pthread_attr_init 1");
128 exit (1);
129 }
130 #endif
131
132 #ifdef PTHREAD_SCOPE_SYSTEM
133 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
134 {
135 perror ("pthread_attr_setscope 1");
136 exit (1);
137 }
138 #endif
139
140 if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface))
141 {
142 perror ("pthread_create 1");
143 exit (1);
144 }
145 if (verbose) printf ("Made thread %d\n", tid1);
146 sleep (1);
147
148 if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef))
149 {
150 perror ("pthread_create 2");
151 exit (1);
152 }
153 if (verbose) printf("Made thread %d\n", tid2);
154
155 sleep (1);
156
157 for (j = 1; j <= 10000000; j++)
158 {
159 if (verbose) printf("top %d\n", pthread_self ());
160 common_routine (0);
161 sleep(1);
162 t += j;
163 }
164
165 exit(0);
166 }
167
168 #endif /* ifndef HAVE_PTHREAD_H */