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