* config/tc-arm.c (arm_cpus): Add entry for ARM Cortex-M0.
[binutils-gdb.git] / gdb / testsuite / gdb.threads / tls.c
blob8f2443c074c4cb305fc1629e031d8dc83915482d
1 /* BeginSourceFile tls.c
3 This file creates and deletes threads. It uses thread local storage
4 variables too. */
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <pthread.h>
11 #include <semaphore.h>
12 #include <errno.h>
14 #define N_THREADS 3
16 /* Uncomment to turn on debugging output */
17 /*#define START_DEBUG*/
19 /* Thread-local storage. */
20 __thread int a_thread_local;
21 __thread int another_thread_local;
23 /* psymtabs->symtabs resolving check. */
24 extern __thread int file2_thread_local;
26 /* Global variable just for info addr in gdb. */
27 int a_global;
29 /* Print the results of thread-local storage. */
30 int thread_local_val[ N_THREADS ];
31 int another_thread_local_val[ N_THREADS ];
33 /* Semaphores to make sure the threads are alive when we print the TLS
34 variables from gdb. */
35 sem_t tell_main, tell_thread;
38 void print_error ()
40 switch (errno)
42 case EAGAIN:
43 fprintf (stderr, "EAGAIN\n");
44 break;
45 case EINTR:
46 fprintf (stderr, "EINTR\n");
47 break;
48 case EINVAL:
49 fprintf (stderr, "EINVAL\n");
50 break;
51 case ENOSYS:
52 fprintf (stderr, "ENOSYS\n");
53 break;
54 case ENOENT:
55 fprintf (stderr, "ENOENT\n");
56 break;
57 case EDEADLK:
58 fprintf (stderr, "EDEADLK\n");
59 break;
60 default:
61 fprintf (stderr, "Unknown error\n");
62 break;
66 /* Routine for each thread to run, does nothing. */
67 void *spin( vp )
68 void * vp;
70 int me = (long) vp;
71 int i;
73 /* Use a_global. */
74 a_global++;
76 a_thread_local = 0;
77 another_thread_local = me;
78 for( i = 0; i <= me; i++ ) {
79 a_thread_local += i;
82 another_thread_local_val[me] = another_thread_local;
83 thread_local_val[ me ] = a_thread_local; /* here we know tls value */
85 if (sem_post (&tell_main) == -1)
87 fprintf (stderr, "th %d post on sem tell_main failed\n", me);
88 print_error ();
89 return;
91 #ifdef START_DEBUG
92 fprintf (stderr, "th %d post on tell main\n", me);
93 #endif
95 while (1)
97 #ifdef START_DEBUG
98 fprintf (stderr, "th %d start wait on tell_thread\n", me);
99 #endif
100 if (sem_wait (&tell_thread) == 0)
101 break;
103 if (errno == EINTR)
105 #ifdef START_DEBUG
106 fprintf (stderr, "th %d wait tell_thread got EINTR, rewaiting\n", me);
107 #endif
108 continue;
110 else
112 fprintf (stderr, "th %d wait on sem tell_thread failed\n", me);
113 print_error ();
114 return;
118 #ifdef START_DEBUG
119 fprintf (stderr, "th %d Wait on tell_thread\n", me);
120 #endif
124 void
125 function_referencing_file2_thread_local (void)
127 file2_thread_local = file2_thread_local;
130 void
131 do_pass()
133 int i;
134 pthread_t t[ N_THREADS ];
135 int err;
137 for( i = 0; i < N_THREADS; i++)
139 thread_local_val[i] = 0;
140 another_thread_local_val[i] = 0;
143 if (sem_init (&tell_main, 0, 0) == -1)
145 fprintf (stderr, "tell_main semaphore init failed\n");
146 return;
149 if (sem_init (&tell_thread, 0, 0) == -1)
151 fprintf (stderr, "tell_thread semaphore init failed\n");
152 return;
155 /* Start N_THREADS threads, then join them so that they are terminated. */
156 for( i = 0; i < N_THREADS; i++ )
158 err = pthread_create( &t[i], NULL, spin, (void *) (long) i );
159 if( err != 0 ) {
160 fprintf(stderr, "Error in thread %d create\n", i );
164 for( i = 0; i < N_THREADS; i++ )
166 while (1)
168 #ifdef START_DEBUG
169 fprintf (stderr, "main %d start wait on tell_main\n", i);
170 #endif
171 if (sem_wait (&tell_main) == 0)
172 break;
174 if (errno == EINTR)
176 #ifdef START_DEBUG
177 fprintf (stderr, "main %d wait tell_main got EINTR, rewaiting\n", i);
178 #endif
179 continue;
181 else
183 fprintf (stderr, "main %d wait on sem tell_main failed\n", i);
184 print_error ();
185 return;
190 #ifdef START_DEBUG
191 fprintf (stderr, "main done waiting on tell_main\n");
192 #endif
194 i = 10; /* Here all threads should be still alive. */
196 for( i = 0; i < N_THREADS; i++ )
198 if (sem_post (&tell_thread) == -1)
200 fprintf (stderr, "main %d post on sem tell_thread failed\n", i);
201 print_error ();
202 return;
204 #ifdef START_DEBUG
205 fprintf (stderr, "main %d post on tell_thread\n", i);
206 #endif
209 for( i = 0; i < N_THREADS; i++ )
211 err = pthread_join(t[i], NULL );
212 if( err != 0 )
214 fprintf (stderr, "error in thread %d join\n", i );
218 i = 10; /* Null line for setting bpts on. */
223 main()
225 do_pass ();
227 return 0; /* Set breakpoint here before exit. */
230 /* EndSourceFile */