]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.hp/start-stop.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.hp / start-stop.c
1 /* BeginSourceFile start_stop.c
2
3 This file creates and deletes threads, so that wdb
4 can be tested on thread delete.
5
6 To compile:
7
8 cc -Ae +DA1.0 -g -o start_stop -lpthread start_stop.c
9
10 To run:
11
12 start_stop --normal run
13 start_stop 1 --waits in each thread to keep it alive.
14 */
15
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <assert.h>
20 #include <pthread.h>
21
22 #define TRUE 1
23 #define FALSE 0
24 #define OUTER_LOOP_COUNT 3
25 #define N_THREADS 3
26 #define MAX_LOCAL_VAL 40
27
28 /* Uncomment to turn on debugging output */
29 /* #define START_DEBUG */
30
31 /* True if waiting for attach.
32 */
33 int wait_here;
34
35 /* Thing to check for debugging purposes.
36 */
37 int a_global = 0;
38
39 /* Thread-local storage.
40 */
41 __thread int a_thread_local;
42
43 /* Check the results of thread-local storage.
44 */
45 int thread_local_val[ N_THREADS ];
46 int val_debugger_saw[ N_THREADS ];
47
48 /* Routine for each thread to run, does nothing.
49 */
50 void *spin( vp )
51 void * vp;
52 {
53 int me = (int) vp;
54 int i;
55
56 #ifdef START_DEBUG
57 printf( "== In thread %d\n", me );
58 #endif
59
60 a_global++;
61
62 a_thread_local = 0;
63 for( i = 0; i < a_global; i++ ) {
64 a_thread_local += i;
65 }
66
67 thread_local_val[ me ] = a_thread_local; /* Line 67 */
68
69 printf( "== Thread %d, a_thread_local is %d\n",
70 (int) vp, a_thread_local );
71
72 if( wait_here ) {
73 /* Extend life of thread to extend life of thread-local var.
74 * This makes life easier for human debugging (though you'd
75 * probably want to make the delay longer).
76 */
77 sleep( 5 );
78 }
79 }
80
81 void
82 do_pass( pass )
83 int pass;
84 {
85 int i;
86 pthread_t t[ N_THREADS ];
87 int err;
88
89 for( i = 0; i < N_THREADS; i++) {
90 thread_local_val[i] = 0;
91 val_debugger_saw[i] = 0;
92 }
93
94 /* Start N_THREADS threads, then join them so
95 * that they are terminated.
96 */
97 for( i = 0; i < N_THREADS; i++ ) {
98 err = pthread_create( &t[i], NULL, spin, (void *)i );
99 if( err != 0 ) {
100 printf( "== Start/stop, error in thread %d create\n", i );
101 }
102 }
103
104 for( i = 0; i < N_THREADS; i++ ) {
105 err = pthread_join(t[i], NULL ); /* Line 105 */
106 if( err != 0 ) { /* Line 106 */
107 printf( "== Start/stop, error in thread %d join\n", i );
108 }
109 }
110
111 i = 10; /* Line 109. Null line for setting bpts on. */
112
113 /*#ifdef START_DEBUG*/
114 for( i = 0; i < N_THREADS; i++) {
115 printf( " Local in thread %d was %d, debugger saw %d\n",
116 i, thread_local_val[i], val_debugger_saw[i] );
117 }
118 printf( "== Pass %d done\n", pass );
119 /*#endif*/
120
121 }
122
123 void
124 do_it()
125 {
126 /* We want to start some threads and then
127 * end them, and then do it again and again
128 */
129 int i;
130 int dummy;
131
132 for( i = 0; i < OUTER_LOOP_COUNT; i++ ) {
133 do_pass( i );
134 dummy = i; /* Line 134, null line for setting bps on */
135 }
136 }
137
138 main( argc, argv )
139 int argc;
140 char **argv;
141 {
142 wait_here = FALSE;
143 if((argc > 1) && (0 != argv )) {
144 if( 1 == atoi( argv[1] ) )
145 wait_here = TRUE;
146 }
147
148 #ifdef START_DEBUG
149 printf( "== Test starting\n" );
150 #endif
151
152 do_it();
153
154 #ifdef START_DEBUG
155 printf( "== Test done\n" );
156 #endif
157
158 return(0);
159 }
160
161 /* EndSourceFile */