import gdb-1999-08-16 snapshot
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.hp / gdb.threads-hp / usrthbasic.c
1 #include <stdio.h>
2
3 /* #include "config.h" */
4 #define HAVE_PTHREAD_H
5 #define __hpux__
6 #define __osf__
7
8 #ifndef HAVE_PTHREAD_H
9
10 /* Don't even try to compile. In fact, cause a syntax error that we can
11 look for as a compiler error message and know that we have no pthread
12 support. In that case we can just suppress the test completely. */
13
14 #error "no posix threads support"
15
16 #else
17
18 /* OK. We have the right header. If we try to compile this and fail, then
19 there is something wrong and the user should know about it so the testsuite
20 should issue an ERROR result.. */
21
22 #ifdef __linux__
23 #define _MIT_POSIX_THREADS 1 /* Linux (or at least RedHat 4.0) needs this */
24 #endif
25
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 }
86
87 static void *
88 thread2 (void * arg)
89 {
90 int i;
91 int k = 0;
92
93 if (verbose) printf ("thread2 (%0x) ; pid = %d\n", arg, getpid ());
94 for (i=1; i <= 10000000; i++)
95 {
96 if (verbose) printf("thread2 %d\n", pthread_self ());
97 k += i;
98 common_routine (2);
99 sleep(1);
100 }
101 sleep(100);
102 }
103
104 int
105 foo (a, b, c)
106 int a, b, c;
107 {
108 int d, e, f;
109
110 if (verbose) printf("a=%d\n", a);
111 }
112
113 main(argc, argv)
114 int argc;
115 char **argv;
116 {
117 pthread_t tid1, tid2;
118 int j;
119 int t = 0;
120 void (*xxx) ();
121 pthread_attr_t attr;
122
123 if (verbose) printf ("pid = %d\n", getpid());
124
125 foo (1, 2, 3);
126
127 #ifndef __osf__
128 if (pthread_attr_init (&attr))
129 {
130 perror ("pthread_attr_init 1");
131 exit (1);
132 }
133 #endif
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_attr_default, thread1, (void *) 0xfeedface))
144 {
145 perror ("pthread_create 1");
146 exit (1);
147 }
148 if (verbose) printf ("Made thread %d\n", tid1);
149 sleep (1);
150
151 if (pthread_create (&tid2, null_attr, thread2, (void *) 0xdeadbeef))
152 {
153 perror ("pthread_create 2");
154 exit (1);
155 }
156 if (verbose) printf("Made thread %d\n", tid2);
157
158 sleep (1);
159
160 for (j = 1; j <= 10000000; j++)
161 {
162 if (verbose) printf("top %d\n", pthread_self ());
163 common_routine (0);
164 sleep(1);
165 t += j;
166 }
167
168 exit(0);
169 }
170
171 #endif /* ifndef HAVE_PTHREAD_H */
This page took 0.047485 seconds and 4 git commands to generate.