Add translations for various sub-directories
[binutils-gdb.git] / gdb / testsuite / gdb.threads / tls.c
blob9a87e91baa2ca9ba09c33397eb519d23bd977e62
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;
22 class K {
23 public:
24 static __thread int another_thread_local;
27 __thread int K::another_thread_local;
29 /* psymtabs->symtabs resolving check. */
30 extern __thread int file2_thread_local;
32 /* Global variable just for info addr in gdb. */
33 int a_global;
35 /* Print the results of thread-local storage. */
36 int thread_local_val[ N_THREADS ];
37 int another_thread_local_val[ N_THREADS ];
39 /* Semaphores to make sure the threads are alive when we print the TLS
40 variables from gdb. */
41 sem_t tell_main, tell_thread;
44 void print_error ()
46 switch (errno)
48 case EAGAIN:
49 fprintf (stderr, "EAGAIN\n");
50 break;
51 case EINTR:
52 fprintf (stderr, "EINTR\n");
53 break;
54 case EINVAL:
55 fprintf (stderr, "EINVAL\n");
56 break;
57 case ENOSYS:
58 fprintf (stderr, "ENOSYS\n");
59 break;
60 case ENOENT:
61 fprintf (stderr, "ENOENT\n");
62 break;
63 case EDEADLK:
64 fprintf (stderr, "EDEADLK\n");
65 break;
66 default:
67 fprintf (stderr, "Unknown error\n");
68 break;
72 /* Routine for each thread to run, does nothing. */
73 void *spin( void *vp )
75 int me = (long) vp;
76 int i;
78 /* Use a_global. */
79 a_global++;
81 a_thread_local = 0;
82 K::another_thread_local = me;
83 for( i = 0; i <= me; i++ ) {
84 a_thread_local += i;
87 another_thread_local_val[me] = K::another_thread_local;
88 thread_local_val[ me ] = a_thread_local; /* here we know tls value */
90 if (sem_post (&tell_main) == -1)
92 fprintf (stderr, "th %d post on sem tell_main failed\n", me);
93 print_error ();
94 return NULL;
96 #ifdef START_DEBUG
97 fprintf (stderr, "th %d post on tell main\n", me);
98 #endif
100 while (1)
102 #ifdef START_DEBUG
103 fprintf (stderr, "th %d start wait on tell_thread\n", me);
104 #endif
105 if (sem_wait (&tell_thread) == 0)
106 break;
108 if (errno == EINTR)
110 #ifdef START_DEBUG
111 fprintf (stderr, "th %d wait tell_thread got EINTR, rewaiting\n", me);
112 #endif
113 continue;
115 else
117 fprintf (stderr, "th %d wait on sem tell_thread failed\n", me);
118 print_error ();
119 return NULL;
123 #ifdef START_DEBUG
124 fprintf (stderr, "th %d Wait on tell_thread\n", me);
125 #endif
127 return NULL;
130 void
131 function_referencing_file2_thread_local (void)
133 file2_thread_local = file2_thread_local;
136 void
137 do_pass()
139 int i;
140 pthread_t t[ N_THREADS ];
141 int err;
143 for( i = 0; i < N_THREADS; i++)
145 thread_local_val[i] = 0;
146 another_thread_local_val[i] = 0;
149 if (sem_init (&tell_main, 0, 0) == -1)
151 fprintf (stderr, "tell_main semaphore init failed\n");
152 return;
155 if (sem_init (&tell_thread, 0, 0) == -1)
157 fprintf (stderr, "tell_thread semaphore init failed\n");
158 return;
161 /* Start N_THREADS threads, then join them so that they are terminated. */
162 for( i = 0; i < N_THREADS; i++ )
164 err = pthread_create( &t[i], NULL, spin, (void *) (long) i );
165 if( err != 0 ) {
166 fprintf(stderr, "Error in thread %d create\n", i );
170 for( i = 0; i < N_THREADS; i++ )
172 while (1)
174 #ifdef START_DEBUG
175 fprintf (stderr, "main %d start wait on tell_main\n", i);
176 #endif
177 if (sem_wait (&tell_main) == 0)
178 break;
180 if (errno == EINTR)
182 #ifdef START_DEBUG
183 fprintf (stderr, "main %d wait tell_main got EINTR, rewaiting\n", i);
184 #endif
185 continue;
187 else
189 fprintf (stderr, "main %d wait on sem tell_main failed\n", i);
190 print_error ();
191 return;
196 #ifdef START_DEBUG
197 fprintf (stderr, "main done waiting on tell_main\n");
198 #endif
200 i = 10; /* Here all threads should be still alive. */
202 for( i = 0; i < N_THREADS; i++ )
204 if (sem_post (&tell_thread) == -1)
206 fprintf (stderr, "main %d post on sem tell_thread failed\n", i);
207 print_error ();
208 return;
210 #ifdef START_DEBUG
211 fprintf (stderr, "main %d post on tell_thread\n", i);
212 #endif
215 for( i = 0; i < N_THREADS; i++ )
217 err = pthread_join(t[i], NULL );
218 if( err != 0 )
220 fprintf (stderr, "error in thread %d join\n", i );
224 i = 10; /* Null line for setting bpts on. */
229 main()
231 do_pass ();
233 return 0; /* Set breakpoint here before exit. */
236 /* EndSourceFile */