1 /* BeginSourceFile tls.c
3 This file creates and deletes threads. It uses thread local storage
11 #include <semaphore.h>
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. */
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
;
43 fprintf (stderr
, "EAGAIN\n");
46 fprintf (stderr
, "EINTR\n");
49 fprintf (stderr
, "EINVAL\n");
52 fprintf (stderr
, "ENOSYS\n");
55 fprintf (stderr
, "ENOENT\n");
58 fprintf (stderr
, "EDEADLK\n");
61 fprintf (stderr
, "Unknown error\n");
66 /* Routine for each thread to run, does nothing. */
77 another_thread_local
= me
;
78 for( i
= 0; i
<= me
; 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
);
92 fprintf (stderr
, "th %d post on tell main\n", me
);
98 fprintf (stderr
, "th %d start wait on tell_thread\n", me
);
100 if (sem_wait (&tell_thread
) == 0)
106 fprintf (stderr
, "th %d wait tell_thread got EINTR, rewaiting\n", me
);
112 fprintf (stderr
, "th %d wait on sem tell_thread failed\n", me
);
119 fprintf (stderr
, "th %d Wait on tell_thread\n", me
);
125 function_referencing_file2_thread_local (void)
127 file2_thread_local
= file2_thread_local
;
134 pthread_t t
[ N_THREADS
];
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");
149 if (sem_init (&tell_thread
, 0, 0) == -1)
151 fprintf (stderr
, "tell_thread semaphore init failed\n");
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
);
160 fprintf(stderr
, "Error in thread %d create\n", i
);
164 for( i
= 0; i
< N_THREADS
; i
++ )
169 fprintf (stderr
, "main %d start wait on tell_main\n", i
);
171 if (sem_wait (&tell_main
) == 0)
177 fprintf (stderr
, "main %d wait tell_main got EINTR, rewaiting\n", i
);
183 fprintf (stderr
, "main %d wait on sem tell_main failed\n", i
);
191 fprintf (stderr
, "main done waiting on tell_main\n");
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
);
205 fprintf (stderr
, "main %d post on tell_thread\n", i
);
209 for( i
= 0; i
< N_THREADS
; i
++ )
211 err
= pthread_join(t
[i
], NULL
);
214 fprintf (stderr
, "error in thread %d join\n", i
);
218 i
= 10; /* Null line for setting bpts on. */
227 return 0; /* Set breakpoint here before exit. */