OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / test / nptl / tst-tls3.c
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <dlfcn.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <signal.h>
23 #include <semaphore.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <pthreaddef.h>
29 #include <tls.h>
30
31 #define THE_SIG SIGUSR1
32
33
34 #define N 10
35 static pthread_t th[N];
36
37
38 #define CB(n) \
39 static void                                                                   \
40 cb##n (void)                                                                  \
41 {                                                                             \
42   if (th[n] != pthread_self ())                                               \
43     {                                                                         \
44       write (STDOUT_FILENO, "wrong callback\n", 15);                          \
45       _exit (1);                                                              \
46     }                                                                         \
47 }
48 CB (0)
49 CB (1)
50 CB (2)
51 CB (3)
52 CB (4)
53 CB (5)
54 CB (6)
55 CB (7)
56 CB (8)
57 CB (9)
58 static void (*cbs[]) (void) =
59 {
60   cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
61 };
62
63
64 sem_t s;
65
66
67 pthread_barrier_t b;
68
69 #define TOTAL_SIGS 1000
70 int nsigs;
71
72
73 int
74 do_test (void)
75 {
76 #if !HAVE___THREAD
77
78   puts ("No __thread support in compiler, test skipped.");
79
80   return 0;
81 #else
82
83   if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
84     {
85       puts ("initial thread's struct pthread not aligned enough");
86       exit (1);
87     }
88
89   if (pthread_barrier_init (&b, NULL, N + 1) != 0)
90     {
91       puts ("barrier_init failed");
92       exit (1);
93     }
94
95   if (sem_init (&s, 0, 0) != 0)
96     {
97       puts ("sem_init failed");
98       exit (1);
99     }
100
101   void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
102   if (h == NULL)
103     {
104       puts ("dlopen failed");
105       exit (1);
106     }
107
108   void *(*tf) (void *) = dlsym (h, "tf");
109   if (tf == NULL)
110     {
111       puts ("dlsym for tf failed");
112       exit (1);
113     }
114
115   void (*setup_tf) (pthread_barrier_t*, int*, sem_t*) = dlsym(h, "setup_tf");
116   if (setup_tf == NULL)
117     {
118       puts ("dlsym for setup_tf failed");
119       exit(1);
120     }
121
122   setup_tf (&b, &nsigs, &s);
123
124   struct sigaction sa;
125   sa.sa_handler = dlsym (h, "handler");
126   if (sa.sa_handler == NULL)
127     {
128       puts ("dlsym for handler failed");
129       exit (1);
130     }
131   sigemptyset (&sa.sa_mask);
132   sa.sa_flags = 0;
133   if (sigaction (THE_SIG, &sa, NULL) != 0)
134     {
135       puts ("sigaction failed");
136       exit (1);
137     }
138
139   pthread_attr_t a;
140
141   if (pthread_attr_init (&a) != 0)
142     {
143       puts ("attr_init failed");
144       exit (1);
145     }
146
147   if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
148     {
149       puts ("attr_setstacksize failed");
150       return 1;
151     }
152
153   int r;
154   for (r = 0; r < 10; ++r)
155     {
156       int i;
157       for (i = 0; i < N; ++i)
158         if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
159           {
160             puts ("pthread_create failed");
161             exit (1);
162           }
163
164       nsigs = 0;
165
166       pthread_barrier_wait (&b);
167
168       sigset_t ss;
169       sigemptyset (&ss);
170       sigaddset (&ss, THE_SIG);
171       if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
172         {
173           puts ("pthread_sigmask failed");
174           exit (1);
175         }
176
177       /* Start sending signals.  */
178       for (i = 0; i < TOTAL_SIGS; ++i)
179         {
180           if (kill (getpid (), THE_SIG) != 0)
181             {
182               puts ("kill failed");
183               exit (1);
184             }
185
186           if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
187             {
188               puts ("sem_wait failed");
189               exit (1);
190             }
191
192           ++nsigs;
193         }
194
195       pthread_barrier_wait (&b);
196
197       if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
198         {
199           puts ("pthread_sigmask failed");
200           exit (1);
201         }
202
203       for (i = 0; i < N; ++i)
204         if (pthread_join (th[i], NULL) != 0)
205           {
206             puts ("join failed");
207             exit (1);
208           }
209     }
210
211   if (pthread_attr_destroy (&a) != 0)
212     {
213       puts ("attr_destroy failed");
214       exit (1);
215     }
216
217   return 0;
218 #endif
219 }
220
221
222 #define TIMEOUT 5
223 #define TEST_FUNCTION do_test ()
224 #include "../test-skeleton.c"