No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / testsuite / gdb.threads / pthreads.c
blobabedb248b3eef8128bd658cf926216714662accb
1 /* Pthreads test program.
2 Copyright 1996, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 Written by Fred Fish of Cygnus Support
6 Contributed by Cygnus Support
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <pthread.h>
29 /* Under OSF 2.0 & 3.0 and HPUX 10, the second arg of pthread_create
30 is prototyped to be just a "pthread_attr_t", while under Solaris it
31 is a "pthread_attr_t *". Arg! */
33 #if defined (__osf__) || defined (__hpux__)
34 #define PTHREAD_CREATE_ARG2(arg) arg
35 #define PTHREAD_CREATE_NULL_ARG2 null_attr
36 static pthread_attr_t null_attr;
37 #else
38 #define PTHREAD_CREATE_ARG2(arg) &arg
39 #define PTHREAD_CREATE_NULL_ARG2 NULL
40 #endif
42 static int verbose = 0;
44 static void
45 common_routine (arg)
46 int arg;
48 static int from_thread1;
49 static int from_thread2;
50 static int from_main;
51 static int hits;
52 static int full_coverage;
54 if (verbose) printf("common_routine (%d)\n", arg);
55 hits++;
56 switch (arg)
58 case 0:
59 from_main++;
60 break;
61 case 1:
62 from_thread1++;
63 break;
64 case 2:
65 from_thread2++;
66 break;
68 if (from_main && from_thread1 && from_thread2)
69 full_coverage = 1;
72 static void *
73 thread1 (void *arg)
75 int i;
76 int z = 0;
78 if (verbose) printf ("thread1 (%0x) ; pid = %d\n", arg, getpid ());
79 for (i=1; i <= 10000000; i++)
81 if (verbose) printf("thread1 %d\n", pthread_self ());
82 z += i;
83 common_routine (1);
84 sleep(1);
86 return (void *) 0;
89 static void *
90 thread2 (void * arg)
92 int i;
93 int k = 0;
95 if (verbose) printf ("thread2 (%0x) ; pid = %d\n", arg, getpid ());
96 for (i=1; i <= 10000000; i++)
98 if (verbose) printf("thread2 %d\n", pthread_self ());
99 k += i;
100 common_routine (2);
101 sleep(1);
103 sleep(100);
104 return (void *) 0;
107 void
108 foo (a, b, c)
109 int a, b, c;
111 int d, e, f;
113 if (verbose) printf("a=%d\n", a);
116 main(argc, argv)
117 int argc;
118 char **argv;
120 pthread_t tid1, tid2;
121 int j;
122 int t = 0;
123 void (*xxx) ();
124 pthread_attr_t attr;
126 if (verbose) printf ("pid = %d\n", getpid());
128 foo (1, 2, 3);
130 #ifndef __osf__
131 if (pthread_attr_init (&attr))
133 perror ("pthread_attr_init 1");
134 exit (1);
136 #endif
138 #ifdef PTHREAD_SCOPE_SYSTEM
139 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM))
141 perror ("pthread_attr_setscope 1");
142 exit (1);
144 #endif
146 if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface))
148 perror ("pthread_create 1");
149 exit (1);
151 if (verbose) printf ("Made thread %d\n", tid1);
152 sleep (1);
154 if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef))
156 perror ("pthread_create 2");
157 exit (1);
159 if (verbose) printf("Made thread %d\n", tid2);
161 sleep (1);
163 for (j = 1; j <= 10000000; j++)
165 if (verbose) printf("top %d\n", pthread_self ());
166 common_routine (0);
167 sleep(1);
168 t += j;
171 exit(0);