OSDN Git Service

Merge branch 'master' into nptl
[uclinux-h8/uclibc-ng.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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <semaphore.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <pthreaddef.h>
30 #include <tls.h>
31
32 #define THE_SIG SIGUSR1
33
34
35 #define N 10
36 static pthread_t th[N];
37
38
39 #define CB(n) \
40 static void                                                                   \
41 cb##n (void)                                                                  \
42 {                                                                             \
43   if (th[n] != pthread_self ())                                               \
44     {                                                                         \
45       write (STDOUT_FILENO, "wrong callback\n", 15);                          \
46       _exit (1);                                                              \
47     }                                                                         \
48 }
49 CB (0)
50 CB (1)
51 CB (2)
52 CB (3)
53 CB (4)
54 CB (5)
55 CB (6)
56 CB (7)
57 CB (8)
58 CB (9)
59 static void (*cbs[]) (void) =
60 {
61   cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
62 };
63
64
65 sem_t s;
66
67
68 pthread_barrier_t b;
69
70 #define TOTAL_SIGS 1000
71 int nsigs;
72
73
74 int
75 do_test (void)
76 {
77 #if !HAVE___THREAD
78
79   puts ("No __thread support in compiler, test skipped.");
80
81   return 0;
82 #else
83
84   if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
85     {
86       puts ("initial thread's struct pthread not aligned enough");
87       exit (1);
88     }
89
90   if (pthread_barrier_init (&b, NULL, N + 1) != 0)
91     {
92       puts ("barrier_init failed");
93       exit (1);
94     }
95
96   if (sem_init (&s, 0, 0) != 0)
97     {
98       puts ("sem_init failed");
99       exit (1);
100     }
101
102   void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
103   if (h == NULL)
104     {
105       puts ("dlopen failed");
106       exit (1);
107     }
108
109   void *(*tf) (void *) = dlsym (h, "tf");
110   if (tf == NULL)
111     {
112       puts ("dlsym for tf failed");
113       exit (1);
114     }
115
116   void (*setup_tf) (pthread_barrier_t*, int*, sem_t*) = dlsym(h, "setup_tf");
117   if (setup_tf == NULL)
118     {
119       puts ("dlsym for setup_tf failed");
120       exit(1);
121     }
122
123   setup_tf (&b, &nsigs, &s);
124
125   struct sigaction sa;
126   sa.sa_handler = dlsym (h, "handler");
127   if (sa.sa_handler == NULL)
128     {
129       puts ("dlsym for handler failed");
130       exit (1);
131     }
132   sigemptyset (&sa.sa_mask);
133   sa.sa_flags = 0;
134   if (sigaction (THE_SIG, &sa, NULL) != 0)
135     {
136       puts ("sigaction failed");
137       exit (1);
138     }
139
140   pthread_attr_t a;
141
142   if (pthread_attr_init (&a) != 0)
143     {
144       puts ("attr_init failed");
145       exit (1);
146     }
147
148   if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
149     {
150       puts ("attr_setstacksize failed");
151       return 1;
152     }
153
154   int r;
155   for (r = 0; r < 10; ++r)
156     {
157       int i;
158       for (i = 0; i < N; ++i)
159         if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
160           {
161             puts ("pthread_create failed");
162             exit (1);
163           }
164
165       nsigs = 0;
166
167       pthread_barrier_wait (&b);
168
169       sigset_t ss;
170       sigemptyset (&ss);
171       sigaddset (&ss, THE_SIG);
172       if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
173         {
174           puts ("pthread_sigmask failed");
175           exit (1);
176         }
177
178       /* Start sending signals.  */
179       for (i = 0; i < TOTAL_SIGS; ++i)
180         {
181           if (kill (getpid (), THE_SIG) != 0)
182             {
183               puts ("kill failed");
184               exit (1);
185             }
186
187           if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
188             {
189               puts ("sem_wait failed");
190               exit (1);
191             }
192
193           ++nsigs;
194         }
195
196       pthread_barrier_wait (&b);
197
198       if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
199         {
200           puts ("pthread_sigmask failed");
201           exit (1);
202         }
203
204       for (i = 0; i < N; ++i)
205         if (pthread_join (th[i], NULL) != 0)
206           {
207             puts ("join failed");
208             exit (1);
209           }
210     }
211
212   if (pthread_attr_destroy (&a) != 0)
213     {
214       puts ("attr_destroy failed");
215       exit (1);
216     }
217
218   return 0;
219 #endif
220 }
221
222
223 #define TIMEOUT 5
224 #define TEST_FUNCTION do_test ()
225 #include "../test-skeleton.c"