1/* Pthreads test program. 2 Copyright 1996-2020 Free Software Foundation, Inc. 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 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 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. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 22#include <stdio.h> 23#include <stdlib.h> 24#include <pthread.h> 25#include <unistd.h> 26 27static int verbose = 0; 28 29static void 30common_routine (arg) 31 int arg; 32{ 33 static int from_thread1; 34 static int from_thread2; 35 static int from_main; 36 static int hits; 37 static int full_coverage; 38 39 if (verbose) printf("common_routine (%d)\n", arg); 40 hits++; 41 switch (arg) 42 { 43 case 0: 44 from_main++; 45 break; 46 case 1: 47 from_thread1++; 48 break; 49 case 2: 50 from_thread2++; 51 break; 52 } 53 if (from_main && from_thread1 && from_thread2) 54 full_coverage = 1; 55} 56 57static void * 58thread1 (void *arg) 59{ 60 int i; 61 int z = 0; 62 63 if (verbose) printf ("thread1 (%0lx) ; pid = %d\n", (long) arg, getpid ()); 64 for (i=1; i <= 10000000; i++) 65 { 66 if (verbose) printf("thread1 %ld\n", (long) pthread_self ()); 67 z += i; 68 common_routine (1); 69 sleep(1); 70 } 71 return (void *) 0; 72} 73 74static void * 75thread2 (void * arg) 76{ 77 int i; 78 int k = 0; 79 80 if (verbose) printf ("thread2 (%0lx) ; pid = %d\n", (long) arg, getpid ()); 81 for (i=1; i <= 10000000; i++) 82 { 83 if (verbose) printf("thread2 %ld\n", (long) pthread_self ()); 84 k += i; 85 common_routine (2); 86 sleep(1); 87 } 88 sleep(100); 89 return (void *) 0; 90} 91 92void 93foo (a, b, c) 94 int a, b, c; 95{ 96 int d, e, f; 97 98 if (verbose) printf("a=%d\n", a); 99} 100 101int 102main(argc, argv) 103 int argc; 104 char **argv; 105{ 106 pthread_t tid1, tid2; 107 int j; 108 int t = 0; 109 void (*xxx) (); 110 pthread_attr_t attr; 111 112 if (verbose) printf ("pid = %d\n", getpid()); 113 114 foo (1, 2, 3); 115 116 if (pthread_attr_init (&attr)) 117 { 118 perror ("pthread_attr_init 1"); 119 exit (1); 120 } 121 122#ifdef PTHREAD_SCOPE_SYSTEM 123 if (pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM)) 124 { 125 perror ("pthread_attr_setscope 1"); 126 exit (1); 127 } 128#endif 129 130 if (pthread_create (&tid1, &attr, thread1, (void *) 0xfeedface)) 131 { 132 perror ("pthread_create 1"); 133 exit (1); 134 } 135 if (verbose) printf ("Made thread %ld\n", (long) tid1); 136 sleep (1); 137 138 if (pthread_create (&tid2, NULL, thread2, (void *) 0xdeadbeef)) 139 { 140 perror ("pthread_create 2"); 141 exit (1); 142 } 143 if (verbose) printf("Made thread %ld\n", (long) tid2); 144 145 sleep (1); 146 147 for (j = 1; j <= 10000000; j++) 148 { 149 if (verbose) printf("top %ld\n", (long) pthread_self ()); 150 common_routine (0); 151 sleep(1); 152 t += j; 153 } 154 155 exit(0); 156} 157 158