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