Update copyright year range in all GDB files.
[external/binutils.git] / gdb / testsuite / gdb.threads / threadapply.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2002-2019 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  
18    This file is copied from schedlock.c.  */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <pthread.h>
24
25 void *thread_function(void *arg); /* Pointer to function executed by each thread */
26
27 #define NUM 5
28
29 unsigned int args[NUM+1];
30
31 int main() {
32     int res;
33     int all_started;
34     pthread_t threads[NUM];
35     void *thread_result;
36     long i;
37
38     for (i = 0; i < NUM; i++)
39       {
40         args[i] = 1; /* Init value.  */
41         res = pthread_create(&threads[i],
42                              NULL,
43                              thread_function,
44                              (void *) i);
45       }
46
47     all_started = 0;
48     while (!all_started)
49       {
50         all_started = 1;
51         for (i = 0; i < NUM; i++)
52           {
53             if (args[i] == 1)
54               {
55                 all_started = 0;
56                 break;
57               }
58           }  
59       }
60         
61     args[i] = 1;
62     /* Break here */
63     thread_function ((void *) i);
64
65     exit(EXIT_SUCCESS);
66 }
67
68 void *thread_function(void *arg) {
69     int my_number =  (long) arg;
70     int *myp = (int *) &args[my_number];
71
72     /* Don't run forever.  Run just short of it :)  */
73     while (*myp > 0)
74       {
75         (*myp) ++;  /* Loop increment.  */
76       }
77
78     pthread_exit(NULL);
79 }
80