binutils/
[external/binutils.git] / gdb / testsuite / gdb.threads / staticthreads.c
1 /* This test program is part of GDB, The GNU debugger.
2
3    Copyright 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4
5    Originally written by Jeff Johnston <jjohnstn@redhat.com>,
6    contributed by Red Hat
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 3 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, see <http://www.gnu.org/licenses/>.  */
22
23 #include <pthread.h>
24 #include <semaphore.h>
25 #include <stdio.h>
26 #include <limits.h>
27 #include <errno.h>
28
29 sem_t semaphore;
30
31 void *
32 thread_function (void *arg)
33 {
34   printf ("Thread executing\n");
35   while (sem_wait (&semaphore) != 0)
36     {
37       if (errno != EINTR)
38         {
39           perror ("thread_function");
40           return;
41         }
42     }
43   return NULL;
44 }
45
46 int 
47 main (int argc, char **argv)
48 {
49   pthread_attr_t attr;
50
51   pthread_attr_init (&attr);
52   pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
53
54   if (sem_init (&semaphore, 0, 0) == -1)
55     {
56       perror ("semaphore");
57       return -1;
58     }
59
60
61   /* Create a thread, wait for it to complete.  */
62   {
63     pthread_t thread;
64     pthread_create (&thread, &attr, thread_function, NULL);
65     sem_post (&semaphore);
66     pthread_join (thread, NULL);
67   }
68
69   pthread_attr_destroy (&attr);
70   return 0;
71 }