Commit | Line | Data |
---|---|---|
c39d7427 | 1 | /* Pthreads test program. |
ecd75fc8 | 2 | Copyright 1996-2014 Free Software Foundation, Inc. |
c39d7427 MC |
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 | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c39d7427 | 12 | (at your option) any later version. |
a9762ec7 | 13 | |
c39d7427 MC |
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. | |
a9762ec7 | 18 | |
c39d7427 | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c39d7427 | 21 | |
c906108c | 22 | #include <stdio.h> |
22227696 | 23 | #include <stdlib.h> |
c906108c SS |
24 | #include <pthread.h> |
25 | ||
32a8097b | 26 | /* Under HPUX 10, the second arg of pthread_create |
c906108c SS |
27 | is prototyped to be just a "pthread_attr_t", while under Solaris it |
28 | is a "pthread_attr_t *". Arg! */ | |
29 | ||
32a8097b | 30 | #if defined (__hpux__) |
c906108c SS |
31 | #define PTHREAD_CREATE_ARG2(arg) arg |
32 | #define PTHREAD_CREATE_NULL_ARG2 null_attr | |
33 | static pthread_attr_t null_attr; | |
34 | #else | |
35 | #define PTHREAD_CREATE_ARG2(arg) &arg | |
36 | #define PTHREAD_CREATE_NULL_ARG2 NULL | |
37 | #endif | |
38 | ||
39 | static int verbose = 0; | |
40 | ||
41 | static void | |
42 | common_routine (arg) | |
43 | int arg; | |
44 | { | |
45 | static int from_thread1; | |
46 | static int from_thread2; | |
47 | static int from_main; | |
48 | static int hits; | |
49 | static int full_coverage; | |
50 | ||
51 | if (verbose) printf("common_routine (%d)\n", arg); | |
52 | hits++; | |
53 | switch (arg) | |
54 | { | |
55 | case 0: | |
56 | from_main++; | |
57 | break; | |
58 | case 1: | |
59 | from_thread1++; | |
60 | break; | |
61 | case 2: | |
62 | from_thread2++; | |
63 | break; | |
64 | } | |
65 | if (from_main && from_thread1 && from_thread2) | |
66 | full_coverage = 1; | |
67 | } | |
68 | ||
69 | static void * | |
70 | thread1 (void *arg) | |
71 | { | |
72 | int i; | |
73 | int z = 0; | |
74 | ||
4dfd5423 | 75 | if (verbose) printf ("thread1 (%0lx) ; pid = %d\n", (long) arg, getpid ()); |
c906108c SS |
76 | for (i=1; i <= 10000000; i++) |
77 | { | |
4dfd5423 | 78 | if (verbose) printf("thread1 %ld\n", (long) pthread_self ()); |
c906108c SS |
79 | z += i; |
80 | common_routine (1); | |
81 | sleep(1); | |
82 | } | |
258ad32d | 83 | return (void *) 0; |
c906108c SS |
84 | } |
85 | ||
86 | static void * | |
87 | thread2 (void * arg) | |
88 | { | |
89 | int i; | |
90 | int k = 0; | |
91 | ||
4dfd5423 | 92 | if (verbose) printf ("thread2 (%0lx) ; pid = %d\n", (long) arg, getpid ()); |
c906108c SS |
93 | for (i=1; i <= 10000000; i++) |
94 | { | |
4dfd5423 | 95 | if (verbose) printf("thread2 %ld\n", (long) pthread_self ()); |
c906108c SS |
96 | k += i; |
97 | common_routine (2); | |
98 | sleep(1); | |
99 | } | |
100 | sleep(100); | |
258ad32d | 101 | return (void *) 0; |
c906108c SS |
102 | } |
103 | ||
258ad32d | 104 | void |
c906108c SS |
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 | ||
c906108c SS |
127 | if (pthread_attr_init (&attr)) |
128 | { | |
129 | perror ("pthread_attr_init 1"); | |
130 | exit (1); | |
131 | } | |
c906108c SS |
132 | |
133 | #ifdef PTHREAD_SCOPE_SYSTEM | |
134 | if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM)) | |
135 | { | |
136 | perror ("pthread_attr_setscope 1"); | |
137 | exit (1); | |
138 | } | |
139 | #endif | |
140 | ||
141 | if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface)) | |
142 | { | |
143 | perror ("pthread_create 1"); | |
144 | exit (1); | |
145 | } | |
4dfd5423 | 146 | if (verbose) printf ("Made thread %ld\n", (long) tid1); |
c906108c SS |
147 | sleep (1); |
148 | ||
149 | if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef)) | |
150 | { | |
151 | perror ("pthread_create 2"); | |
152 | exit (1); | |
153 | } | |
4dfd5423 | 154 | if (verbose) printf("Made thread %ld\n", (long) tid2); |
c906108c SS |
155 | |
156 | sleep (1); | |
157 | ||
158 | for (j = 1; j <= 10000000; j++) | |
159 | { | |
4dfd5423 | 160 | if (verbose) printf("top %ld\n", (long) pthread_self ()); |
c906108c SS |
161 | common_routine (0); |
162 | sleep(1); | |
163 | t += j; | |
164 | } | |
165 | ||
166 | exit(0); | |
167 | } | |
168 |