]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/pthreads.c
2003-08-07 Elena Zannoni <ezannoni@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / pthreads.c
1 #include <stdio.h>
2 #include <pthread.h>
3
4 /* Under OSF 2.0 & 3.0 and HPUX 10, the second arg of pthread_create
5 is prototyped to be just a "pthread_attr_t", while under Solaris it
6 is a "pthread_attr_t *". Arg! */
7
8 #if defined (__osf__) || defined (__hpux__)
9 #define PTHREAD_CREATE_ARG2(arg) arg
10 #define PTHREAD_CREATE_NULL_ARG2 null_attr
11 static pthread_attr_t null_attr;
12 #else
13 #define PTHREAD_CREATE_ARG2(arg) &arg
14 #define PTHREAD_CREATE_NULL_ARG2 NULL
15 #endif
16
17 static int verbose = 0;
18
19 static void
20 common_routine (arg)
21 int arg;
22 {
23 static int from_thread1;
24 static int from_thread2;
25 static int from_main;
26 static int hits;
27 static int full_coverage;
28
29 if (verbose) printf("common_routine (%d)\n", arg);
30 hits++;
31 switch (arg)
32 {
33 case 0:
34 from_main++;
35 break;
36 case 1:
37 from_thread1++;
38 break;
39 case 2:
40 from_thread2++;
41 break;
42 }
43 if (from_main && from_thread1 && from_thread2)
44 full_coverage = 1;
45 }
46
47 static void *
48 thread1 (void *arg)
49 {
50 int i;
51 int z = 0;
52
53 if (verbose) printf ("thread1 (%0x) ; pid = %d\n", arg, getpid ());
54 for (i=1; i <= 10000000; i++)
55 {
56 if (verbose) printf("thread1 %d\n", pthread_self ());
57 z += i;
58 common_routine (1);
59 sleep(1);
60 }
61 return (void *) 0;
62 }
63
64 static void *
65 thread2 (void * arg)
66 {
67 int i;
68 int k = 0;
69
70 if (verbose) printf ("thread2 (%0x) ; pid = %d\n", arg, getpid ());
71 for (i=1; i <= 10000000; i++)
72 {
73 if (verbose) printf("thread2 %d\n", pthread_self ());
74 k += i;
75 common_routine (2);
76 sleep(1);
77 }
78 sleep(100);
79 return (void *) 0;
80 }
81
82 void
83 foo (a, b, c)
84 int a, b, c;
85 {
86 int d, e, f;
87
88 if (verbose) printf("a=%d\n", a);
89 }
90
91 main(argc, argv)
92 int argc;
93 char **argv;
94 {
95 pthread_t tid1, tid2;
96 int j;
97 int t = 0;
98 void (*xxx) ();
99 pthread_attr_t attr;
100
101 if (verbose) printf ("pid = %d\n", getpid());
102
103 foo (1, 2, 3);
104
105 #ifndef __osf__
106 if (pthread_attr_init (&attr))
107 {
108 perror ("pthread_attr_init 1");
109 exit (1);
110 }
111 #endif
112
113 #ifdef PTHREAD_SCOPE_SYSTEM
114 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
115 {
116 perror ("pthread_attr_setscope 1");
117 exit (1);
118 }
119 #endif
120
121 if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface))
122 {
123 perror ("pthread_create 1");
124 exit (1);
125 }
126 if (verbose) printf ("Made thread %d\n", tid1);
127 sleep (1);
128
129 if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef))
130 {
131 perror ("pthread_create 2");
132 exit (1);
133 }
134 if (verbose) printf("Made thread %d\n", tid2);
135
136 sleep (1);
137
138 for (j = 1; j <= 10000000; j++)
139 {
140 if (verbose) printf("top %d\n", pthread_self ());
141 common_routine (0);
142 sleep(1);
143 t += j;
144 }
145
146 exit(0);
147 }
148