1 /* Pthreads test program. 2 Copyright 1996, 2002, 2003, 2004 3 Free Software Foundation, Inc. 4 5 Written by Fred Fish of Cygnus Support 6 Contributed by Cygnus Support 7 8 This file is part of GDB. 9 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. 14 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. 19 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. */ 24 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <pthread.h> 28 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! */ 32 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 41 42 static int verbose = 0; 43 44 static void 45 common_routine (arg) 46 int arg; 47 { 48 static int from_thread1; 49 static int from_thread2; 50 static int from_main; 51 static int hits; 52 static int full_coverage; 53 54 if (verbose) printf("common_routine (%d)\n", arg); 55 hits++; 56 switch (arg) 57 { 58 case 0: 59 from_main++; 60 break; 61 case 1: 62 from_thread1++; 63 break; 64 case 2: 65 from_thread2++; 66 break; 67 } 68 if (from_main && from_thread1 && from_thread2) 69 full_coverage = 1; 70 } 71 72 static void * 73 thread1 (void *arg) 74 { 75 int i; 76 int z = 0; 77 78 if (verbose) printf ("thread1 (%0x) ; pid = %d\n", arg, getpid ()); 79 for (i=1; i <= 10000000; i++) 80 { 81 if (verbose) printf("thread1 %d\n", pthread_self ()); 82 z += i; 83 common_routine (1); 84 sleep(1); 85 } 86 return (void *) 0; 87 } 88 89 static void * 90 thread2 (void * arg) 91 { 92 int i; 93 int k = 0; 94 95 if (verbose) printf ("thread2 (%0x) ; pid = %d\n", arg, getpid ()); 96 for (i=1; i <= 10000000; i++) 97 { 98 if (verbose) printf("thread2 %d\n", pthread_self ()); 99 k += i; 100 common_routine (2); 101 sleep(1); 102 } 103 sleep(100); 104 return (void *) 0; 105 } 106 107 void 108 foo (a, b, c) 109 int a, b, c; 110 { 111 int d, e, f; 112 113 if (verbose) printf("a=%d\n", a); 114 } 115 116 main(argc, argv) 117 int argc; 118 char **argv; 119 { 120 pthread_t tid1, tid2; 121 int j; 122 int t = 0; 123 void (*xxx) (); 124 pthread_attr_t attr; 125 126 if (verbose) printf ("pid = %d\n", getpid()); 127 128 foo (1, 2, 3); 129 130 #ifndef __osf__ 131 if (pthread_attr_init (&attr)) 132 { 133 perror ("pthread_attr_init 1"); 134 exit (1); 135 } 136 #endif 137 138 #ifdef PTHREAD_SCOPE_SYSTEM 139 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM)) 140 { 141 perror ("pthread_attr_setscope 1"); 142 exit (1); 143 } 144 #endif 145 146 if (pthread_create (&tid1, PTHREAD_CREATE_ARG2(attr), thread1, (void *) 0xfeedface)) 147 { 148 perror ("pthread_create 1"); 149 exit (1); 150 } 151 if (verbose) printf ("Made thread %d\n", tid1); 152 sleep (1); 153 154 if (pthread_create (&tid2, PTHREAD_CREATE_NULL_ARG2, thread2, (void *) 0xdeadbeef)) 155 { 156 perror ("pthread_create 2"); 157 exit (1); 158 } 159 if (verbose) printf("Made thread %d\n", tid2); 160 161 sleep (1); 162 163 for (j = 1; j <= 10000000; j++) 164 { 165 if (verbose) printf("top %d\n", pthread_self ()); 166 common_routine (0); 167 sleep(1); 168 t += j; 169 } 170 171 exit(0); 172 } 173 174