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 /* Global variable just for info addr in gdb. */
26 /* Print the results of thread-local storage. */
27 int thread_local_val
[ N_THREADS
];
28 int another_thread_local_val
[ N_THREADS
];
30 /* Semaphores to make sure the threads are alive when we print the TLS
31 variables from gdb. */
32 sem_t tell_main
, tell_thread
;
40 fprintf (stderr
, "EAGAIN\n");
43 fprintf (stderr
, "EINTR\n");
46 fprintf (stderr
, "EINVAL\n");
49 fprintf (stderr
, "ENOSYS\n");
52 fprintf (stderr
, "ENOENT\n");
55 fprintf (stderr
, "EDEADLK\n");
58 fprintf (stderr
, "Unknown error\n");
63 /* Routine for each thread to run, does nothing. */
74 another_thread_local
= me
;
75 for( i
= 0; i
<= me
; i
++ ) {
79 another_thread_local_val
[me
] = another_thread_local
;
80 thread_local_val
[ me
] = a_thread_local
; /* here we know tls value */
82 if (sem_post (&tell_main
) == -1)
84 fprintf (stderr
, "th %d post on sem tell_main failed\n", me
);
89 fprintf (stderr
, "th %d post on tell main\n", me
);
95 fprintf (stderr
, "th %d start wait on tell_thread\n", me
);
97 if (sem_wait (&tell_thread
) == 0)
103 fprintf (stderr
, "th %d wait tell_thread got EINTR, rewaiting\n", me
);
109 fprintf (stderr
, "th %d wait on sem tell_thread failed\n", me
);
116 fprintf (stderr
, "th %d Wait on tell_thread\n", me
);
125 pthread_t t
[ N_THREADS
];
128 for( i
= 0; i
< N_THREADS
; i
++)
130 thread_local_val
[i
] = 0;
131 another_thread_local_val
[i
] = 0;
134 if (sem_init (&tell_main
, 0, 0) == -1)
136 fprintf (stderr
, "tell_main semaphore init failed\n");
140 if (sem_init (&tell_thread
, 0, 0) == -1)
142 fprintf (stderr
, "tell_thread semaphore init failed\n");
146 /* Start N_THREADS threads, then join them so that they are terminated. */
147 for( i
= 0; i
< N_THREADS
; i
++ )
149 err
= pthread_create( &t
[i
], NULL
, spin
, (void *) (long) i
);
151 fprintf(stderr
, "Error in thread %d create\n", i
);
155 for( i
= 0; i
< N_THREADS
; i
++ )
160 fprintf (stderr
, "main %d start wait on tell_main\n", i
);
162 if (sem_wait (&tell_main
) == 0)
168 fprintf (stderr
, "main %d wait tell_main got EINTR, rewaiting\n", i
);
174 fprintf (stderr
, "main %d wait on sem tell_main failed\n", i
);
182 fprintf (stderr
, "main done waiting on tell_main\n");
185 i
= 10; /* Here all threads should be still alive. */
187 for( i
= 0; i
< N_THREADS
; i
++ )
189 if (sem_post (&tell_thread
) == -1)
191 fprintf (stderr
, "main %d post on sem tell_thread failed\n", i
);
196 fprintf (stderr
, "main %d post on tell_thread\n", i
);
200 for( i
= 0; i
< N_THREADS
; i
++ )
202 err
= pthread_join(t
[i
], NULL
);
205 fprintf (stderr
, "error in thread %d join\n", i
);
209 i
= 10; /* Null line for setting bpts on. */
218 return 0; /* Set breakpoint here before exit. */