]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/staticthreads.c
57defefcae4b8e2d385d082f1f1a62d26c2eac78
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / staticthreads.c
1 /* This test program is part of GDB, The GNU debugger.
2
3 Copyright 2004 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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25 #include <pthread.h>
26 #include <semaphore.h>
27 #include <stdio.h>
28 #include <limits.h>
29 #include <errno.h>
30
31 sem_t semaphore;
32
33 void *
34 thread_function (void *arg)
35 {
36 printf ("Thread executing\n");
37 while (sem_wait (&semaphore) != 0)
38 {
39 if (errno != EINTR)
40 {
41 perror ("thread_function");
42 return;
43 }
44 }
45 return NULL;
46 }
47
48 int
49 main (int argc, char **argv)
50 {
51 pthread_attr_t attr;
52
53 pthread_attr_init (&attr);
54 pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
55
56 if (sem_init (&semaphore, 0, 0) == -1)
57 {
58 perror ("semaphore");
59 return -1;
60 }
61
62
63 /* Create a thread, wait for it to complete. */
64 {
65 pthread_t thread;
66 pthread_create (&thread, &attr, thread_function, NULL);
67 sem_post (&semaphore);
68 pthread_join (thread, NULL);
69 }
70
71 pthread_attr_destroy (&attr);
72 return 0;
73 }