include/
[external/binutils.git] / gdb / testsuite / gdb.threads / bp_in_thread.c
1 /* A small multi-threaded test case.
2
3    Copyright 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <time.h>
23
24 void
25 cond_wait (pthread_cond_t *cond, pthread_mutex_t *mut)
26 {
27   pthread_mutex_lock(mut);
28   pthread_cond_wait (cond, mut);
29   pthread_mutex_unlock (mut);
30 }
31
32 void
33 noreturn (void)
34 {
35   pthread_mutex_t mut;
36   pthread_cond_t cond;
37
38   pthread_mutex_init (&mut, NULL);
39   pthread_cond_init (&cond, NULL);
40
41   /* Wait for a condition that will never be signaled, so we effectively
42      block the thread here.  */
43   cond_wait (&cond, &mut);
44 }
45
46 void *
47 forever_pthread (void *unused)
48 {
49   noreturn ();
50 }
51
52 void
53 break_me (void)
54 {
55   /* Just an anchor to help putting a breakpoint.  */
56 }
57
58 int
59 main (void)
60 {
61   pthread_t forever;
62   const struct timespec ts = { 0, 10000000 }; /* 0.01 sec */
63
64   pthread_create (&forever, NULL, forever_pthread, NULL);
65   for (;;)
66     {
67       nanosleep (&ts, NULL);
68       break_me();
69     }
70
71   return 0;
72 }
73