xref: /openbsd-src/gnu/usr.bin/binutils/gdb/testsuite/gdb.threads/schedlock.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1*11efff7fSkettenis /* This testcase is part of GDB, the GNU debugger.
2*11efff7fSkettenis 
3*11efff7fSkettenis    Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
4*11efff7fSkettenis 
5*11efff7fSkettenis    This program is free software; you can redistribute it and/or modify
6*11efff7fSkettenis    it under the terms of the GNU General Public License as published by
7*11efff7fSkettenis    the Free Software Foundation; either version 2 of the License, or
8*11efff7fSkettenis    (at your option) any later version.
9*11efff7fSkettenis 
10*11efff7fSkettenis    This program is distributed in the hope that it will be useful,
11*11efff7fSkettenis    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*11efff7fSkettenis    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*11efff7fSkettenis    GNU General Public License for more details.
14*11efff7fSkettenis 
15*11efff7fSkettenis    You should have received a copy of the GNU General Public License
16*11efff7fSkettenis    along with this program; if not, write to the Free Software
17*11efff7fSkettenis    Foundation, Inc., 59 Temple Place - Suite 330,
18*11efff7fSkettenis    Boston, MA 02111-1307, USA.  */
19*11efff7fSkettenis 
20b725ae77Skettenis #include <stdio.h>
21b725ae77Skettenis #include <unistd.h>
22b725ae77Skettenis #include <stdlib.h>
23b725ae77Skettenis #include <pthread.h>
24b725ae77Skettenis 
25b725ae77Skettenis void *thread_function(void *arg); /* Pointer to function executed by each thread */
26b725ae77Skettenis 
27b725ae77Skettenis #define NUM 5
28b725ae77Skettenis 
29b725ae77Skettenis unsigned int args[NUM+1];
30b725ae77Skettenis 
main()31b725ae77Skettenis int main() {
32b725ae77Skettenis     int res;
33b725ae77Skettenis     pthread_t threads[NUM];
34b725ae77Skettenis     void *thread_result;
35b725ae77Skettenis     long i;
36b725ae77Skettenis 
37b725ae77Skettenis     for (i = 0; i < NUM; i++)
38b725ae77Skettenis       {
39b725ae77Skettenis 	args[i] = 1;
40b725ae77Skettenis 	res = pthread_create(&threads[i],
41b725ae77Skettenis 		             NULL,
42b725ae77Skettenis 			     thread_function,
43b725ae77Skettenis 			     (void *) i);
44b725ae77Skettenis       }
45b725ae77Skettenis 
46b725ae77Skettenis     /* schedlock.exp: last thread start.  */
47b725ae77Skettenis     args[i] = 1;
48b725ae77Skettenis     thread_function ((void *) i);
49b725ae77Skettenis 
50b725ae77Skettenis     exit(EXIT_SUCCESS);
51b725ae77Skettenis }
52b725ae77Skettenis 
thread_function(void * arg)53b725ae77Skettenis void *thread_function(void *arg) {
54b725ae77Skettenis     int my_number =  (long) arg;
55*11efff7fSkettenis     int *myp = (int *) &args[my_number];
56b725ae77Skettenis 
57b725ae77Skettenis     /* Don't run forever.  Run just short of it :)  */
58b725ae77Skettenis     while (*myp > 0)
59b725ae77Skettenis       {
60b725ae77Skettenis 	/* schedlock.exp: main loop.  */
61b725ae77Skettenis 	(*myp) ++;
62b725ae77Skettenis       }
63b725ae77Skettenis 
64b725ae77Skettenis     pthread_exit(NULL);
65b725ae77Skettenis }
66b725ae77Skettenis 
67