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
;
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. */
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
;
49 fprintf (stderr
, "EAGAIN\n");
52 fprintf (stderr
, "EINTR\n");
55 fprintf (stderr
, "EINVAL\n");
58 fprintf (stderr
, "ENOSYS\n");
61 fprintf (stderr
, "ENOENT\n");
64 fprintf (stderr
, "EDEADLK\n");
67 fprintf (stderr
, "Unknown error\n");
72 /* Routine for each thread to run, does nothing. */
73 void *spin( void *vp
)
82 K::another_thread_local
= me
;
83 for( i
= 0; i
<= me
; 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
);
97 fprintf (stderr
, "th %d post on tell main\n", me
);
103 fprintf (stderr
, "th %d start wait on tell_thread\n", me
);
105 if (sem_wait (&tell_thread
) == 0)
111 fprintf (stderr
, "th %d wait tell_thread got EINTR, rewaiting\n", me
);
117 fprintf (stderr
, "th %d wait on sem tell_thread failed\n", me
);
124 fprintf (stderr
, "th %d Wait on tell_thread\n", me
);
131 function_referencing_file2_thread_local (void)
133 file2_thread_local
= file2_thread_local
;
140 pthread_t t
[ N_THREADS
];
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");
155 if (sem_init (&tell_thread
, 0, 0) == -1)
157 fprintf (stderr
, "tell_thread semaphore init failed\n");
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
);
166 fprintf(stderr
, "Error in thread %d create\n", i
);
170 for( i
= 0; i
< N_THREADS
; i
++ )
175 fprintf (stderr
, "main %d start wait on tell_main\n", i
);
177 if (sem_wait (&tell_main
) == 0)
183 fprintf (stderr
, "main %d wait tell_main got EINTR, rewaiting\n", i
);
189 fprintf (stderr
, "main %d wait on sem tell_main failed\n", i
);
197 fprintf (stderr
, "main done waiting on tell_main\n");
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
);
211 fprintf (stderr
, "main %d post on tell_thread\n", i
);
215 for( i
= 0; i
< N_THREADS
; i
++ )
217 err
= pthread_join(t
[i
], NULL
);
220 fprintf (stderr
, "error in thread %d join\n", i
);
224 i
= 10; /* Null line for setting bpts on. */
233 return 0; /* Set breakpoint here before exit. */
This page took 0.033729 seconds and 4 git commands to generate.