1 /* Test program for non-stop debugging.
2    Copyright 1996-2015 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <pthread.h>
22 
23 /* Under HPUX 10, the second arg of pthread_create
24    is prototyped to be just a "pthread_attr_t", while under Solaris it
25    is a "pthread_attr_t *".  Arg! */
26 
27 #if defined (__hpux__)
28 #define PTHREAD_CREATE_ARG2(arg) arg
29 #define PTHREAD_CREATE_NULL_ARG2 null_attr
30 static pthread_attr_t null_attr;
31 #else
32 #define PTHREAD_CREATE_ARG2(arg) &arg
33 #define PTHREAD_CREATE_NULL_ARG2 NULL
34 #endif
35 
36 int exit_first_thread = 0;
37 
38 void break_at_me (int id, int i)
39 {
40 }
41 
42 void *
43 worker (void *arg)
44 {
45   int id = *(int *)arg;
46   int i = 0;
47 
48   /* When gdb is running, it sets hidden breakpoints in the thread
49      library.  The signals caused by these hidden breakpoints can
50      cause system calls such as 'sleep' to return early.  Pay attention
51      to the return value from 'sleep' to get the full sleep.  */
52   for (;;++i)
53     {
54       int unslept = 1;
55       while (unslept > 0)
56 	unslept = sleep (unslept);
57 
58       if (exit_first_thread && id == 0)
59 	return NULL;
60 
61       break_at_me (id, i);
62     }
63 }
64 
65 pthread_t
66 create_thread (int id)
67 {
68   pthread_t tid;
69   /* This memory will be leaked, we don't care for a test.  */
70   int *id2 = malloc (sizeof (int));
71   *id2 = id;
72 
73   if (pthread_create (&tid, PTHREAD_CREATE_NULL_ARG2, worker, (void *) id2))
74     {
75       perror ("pthread_create 1");
76       exit (1);
77     }
78   return tid;
79 }
80 
81 int
82 main (int argc, char *argv[])
83 {
84   pthread_t tid;
85   create_thread (0);
86   sleep (1);
87   tid = create_thread (1);
88   pthread_join (tid, NULL);
89 
90   return 0;
91 }
92 
93